首页 > 解决方案 > UnhandledPromiseRejectionWarning:TypeError:无法读取未定义的属性“声音”。在 play.js 中尝试运行命令时

问题描述

我收到错误 UnhandledPromiseRejectionWarning: TypeError: Cannot read property 'voice' of undefined in my code。是依赖关系的问题还是代码中的错误。这是我的代码。

const Discord = require('discord.js');

module.exports = {
    name: 'play',
    aliases: ['p'],
    description: 'plays a song/nasheed',

    async execute (client, message, args) {
        if(!message.member.voice.channel) return message.reply('Pleases be in a vc to use this command.');

        const music = args.join(" "); //&play song name
        if(!music) return message.reply("Invalid song/nasheed name.");

        await client.distube.play(message, music);
    }

}

这是我的 bot.js 代码

const fs = require('fs');
const Discord = require("discord.js");
const { prefix, token } = require('./config.json');



const client = new Discord.Client();
client.commands = new Discord.Collection();
client.cooldowns = new Discord.Collection();


const commandFolders = fs.readdirSync('./src/commands');

for (const folder of commandFolders) {
    const commandFiles = fs.readdirSync(`./src/commands/${folder}`).filter(file => file.endsWith('.js'));
    for (const file of commandFiles) {
        const command = require(`./src/commands/${folder}/${file}`);
        client.commands.set(command.name, command);
    }
    
}



client.once('ready', () => {
    console.log('bot is online');


client.user.setPresence({
    status: 'available',
    activity: {
        name: 'Answering &help',
        type: 'WATCHING',
        url: 'https://www.youtube.com/channel/UC1RUkzjpWtp4w3OoMKh7pGg'
    }
});
});
 
client.on('message', message => {
        if (!message.content.startsWith(prefix) || message.author.bot) return;
    
        const args = message.content.slice(prefix.length).trim().split(/ +/);
        const commandName = args.shift().toLowerCase();
    
        const command = client.commands.get(commandName)
            || client.commands.find(cmd => cmd.aliases && cmd.aliases.includes(commandName));
    
        if (!command) return;
    
        if (command.guildOnly && message.channel.type === 'dm') {
            return message.reply('I can\'t execute that command inside DMs!');
        }
    
        if (command.permissions) {
            const authorPerms = message.channel.permissionsFor(message.author);
            if (!authorPerms || !authorPerms.has(command.permissions)) {
                return message.reply('You can not do this!');
            }
        }
        try {
            command.execute(message, args);
        } catch (error) {
            console.error(error);
            message.reply('there was an error trying to execute that command!');
        }
    });

const distube = require('distube');
client.distube = new distube(client, { searchSongs: false, emitNewSongOnly: true });
client.distube
    .on('playSong', (message, queue, song) => message.channel.send(
        `Playing \`${song.name}\` - \`${song.formattedDuration}\`\nRequested by: ${song.user}\n${status(queue)}`,
    ))
    .on('addSong', (message, queue, song) => message.channel.send(
        `Added ${song.name} - \`${song.formattedDuration}\` to the queue by ${song.user}`,
    ))
    .on('error', (message, e) => {
        //console.error(e)
        message.channel.send(`An error encountered: ${e}`)
    })

client.login(token);

这是我正在尝试制作的音乐命令,要求您在不和谐的语音频道中工作。

标签: javascriptnode.jsdiscord.js

解决方案


您遇到的问题是您传递以执行命令的变量错位。在您的/play命令文件中,您必须更改此行:

async execute (client, message, args)

async execute (client, message, args, Discord)

你可以摆脱

const Discord = require('discord.js');

因为您现在将从命令提取器中传递 Discord 变量。但要真正传入变量,您必须转到bot.js文件并更改以下行:

        try {
            command.execute(message, args);
        } catch (error) {
            console.error(error);
            message.reply('there was an error trying to execute that command!');
        }

对此:

        try {
            command.execute(client, message, args, Discord);
        } catch (error) {
            console.error(error);
            message.reply('there was an error trying to execute that command!');
        }

您将传递的变量是(client, message, args, Discord),这意味着您只需为您将创建的每个命令添加它们,否则该命令将不起作用。

您当前的命令不起作用的原因是您client在执行命令后没有调用该变量,这意味着该变量message位于您的客户端变量的位置,话虽如此,您始终要牢记(client, message, args, Discord)准确放置这些变量与它们在 bot.js 文件中的顺序相同,否则,该命令将始终引发问题,因为它们都必须以相同的顺序说明。

我希望这有帮助!祝你的项目好运。


推荐阅读