TypeError:无法读取未定义Discord,js的属性“ execute”

时间:2020-10-04 16:20:30

标签: javascript node.js discord discord.js

我有问题,我的代码正在显示

TypeError:无法读取暂停命令的undefined属性“ execute”。其他所有命令都可以正常工作。

读取文件。

client.commands = new Discord.Collection();
const commandFIles = fs.readdirSync('./commands/').filter(file => file.endsWith('.js'));
for(const file of commandFIles){
    const command = require(`./commands/${file}`);
    client.commands.set(command.name, command);
}
client.on('message', msg => {
    if (!msg.content.startsWith(prefix) || msg.author.bot) {
        return;
    }
    const args = msg.content.slice(prefix.length).trim().split(/ +/);
    const command = args.shift().toLowerCase();

    if (command === 'ping') {
        client.commands.get('ping').execute(msg, args);
    } else if (command === 'userinfo') {
        client.commands.get('userinfo').execute(msg, args);
    } else if (command === 'delete') {
        const amount = parseInt(args[0]) + 1;
        if (isNaN(amount)) {
            return msg.reply('Enter a valid number.');
        } else if (amount <= 1 || amount > 100) {
            return msg.reply('Enter a number between 1 and 99.');
        } else {
            msg.channel.bulkDelete(amount, true).catch(err => {
                console.error(err);
                msg.channel.send('There was an error trying to delete messages');
            });
            msg.channel.send(`Deleted ${args} messages.`);
        }
    } else if (command === 'p' || command === 'play') {
        client.commands.get('play').execute(msg, args);
    } else if (command === 'pause') {
        client.commands.get('pause').execute(msg);
    }
});

pause.js代码:

module.exports = {
    title: 'pause',
    description: "Pause the current song.",
    execute(message){
       const queue = message.client.queue.get(message.guild.id);
       if(!queue) return message.reply("There is nothing playing").catch(console.error);
       if(queue.playing){
           queue.playing = false;
           queue.connection.dispatcher.pause(true);
           return queue.textChannel.send(`⏸ Paused.`).catch(console.error);
       }
    }
};

如果我复制代码并将相同的代码粘贴到Client.on中,则可以正常工作,但在与module.exports一起使用时显示错误。是否有解决方法?

3 个答案:

答案 0 :(得分:0)

查看您的pause.js导出-execute不是对象的属性。试试这个:

module.exports = {
    title: 'pause',
    description: "Pause the current song.",
    execute: function (message) {
       const queue = message.client.queue.get(message.guild.id);
       if(!queue) return message.reply("There is nothing playing").catch(console.error);
       if(queue.playing){
           queue.playing = false;
           queue.connection.dispatcher.pause(true);
           return queue.textChannel.send(`⏸ Paused.`).catch(console.error);
       }
    }
};

创建一个名为execute的属性,并将其值分配给您拥有的功能。

答案 1 :(得分:0)

您正在使用以下代码添加命令:

client.commands = new Discord.Collection();
const commandFIles = fs.readdirSync('./commands/').filter(file => file.endsWith('.js'));
for(const file of commandFIles){
    const command = require(`./commands/${file}`);
    client.commands.set(command.name, command);
}

因此命令名称是文件export .name。在expore文件中,没有name属性。相反,您写了标题。试试这个:

module.exports = {
    name: 'pause',
    description: "Pause the current song.",
    execute(message){
       const queue = message.client.queue.get(message.guild.id);
       if(!queue) return message.reply("There is nothing playing").catch(console.error);
       if(queue.playing){
           queue.playing = false;
           queue.connection.dispatcher.pause(true);
           return queue.textChannel.send(`⏸ Paused.`).catch(console.error);
       }
    }
};

答案 2 :(得分:-1)

因此,您将命令设置为按名称执行,但在导出中没有名称,您在.get in事件侧调用了该名称,因此为什么它返回undefined,希望它有用