discord.js“ TypeError:无法读取未定义的属性'execute'”

时间:2020-11-03 15:02:10

标签: node.js discord.js

我正在尝试使用discord.js制作机器人,并遇到了这个错误,我不知道该如何解决,我已经找了几个小时,却找不到答案,当我运行bot,它可以成功登录,但是当您运行命令时,powershell控制台会引发错误

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

这是我的主要代码

module.exports = {
    name: "purge",
    description: "Deletes input amout of messages.",
    async execute(message, args) {
        if (message.member.hasPermission(MANAGE_MESSAGES)) {
            const deleteCount = parseInt(args[0], 10);
            const deleteMessage = `Deleted ${deleteCount} messages.`;

            if (!deleteCount || deleteCount > 500 || deleteCount < 2) return message.reply(`${message.author} please input a number between 2 - 500.`);

            const fetched = await message.channel.fetchMessages({
                limit: deleteCount
            });
            
            message.channel.bulkDelete(fetched)
                .catch(err => console.log(`Cannot delete message because of ${err}`))
                .then(message.reply(deleteMessage))
                .catch(err => {
                    console.log(err);
                })
        } else {
            message.reply('You do not have permissions to purge.')
        }
    }

}

这是一个命令.js文件,该命令用于清除500到2之间的x条消息

TypeError: Cannot read property 'execute' of undefined
    at Client.<anonymous> (C:\Users\ryssu\source\repos\SCP\079\app.js:24:11)
    at Client.emit (events.js:315:20)
    at MessageCreateAction.handle (C:\Users\ryssu\source\repos\SCP\079\node_modules\discord.js\src\client\actions\MessageCreate.js:31:14)
    at Object.module.exports [as MESSAGE_CREATE] (C:\Users\ryssu\source\repos\SCP\079\node_modules\discord.js\src\client\websocket\handlers\MESSAGE_CREATE.j
s:4:32)
    at WebSocketManager.handlePacket (C:\Users\ryssu\source\repos\SCP\079\node_modules\discord.js\src\client\websocket\WebSocketManager.js:384:31)
    at WebSocketShard.onPacket (C:\Users\ryssu\source\repos\SCP\079\node_modules\discord.js\src\client\websocket\WebSocketShard.js:444:22)
    at WebSocketShard.onMessage (C:\Users\ryssu\source\repos\SCP\079\node_modules\discord.js\src\client\websocket\WebSocketShard.js:301:10)
    at WebSocket.onMessage (C:\Users\ryssu\source\repos\SCP\079\node_modules\ws\lib\event-target.js:125:16)
    at WebSocket.emit (events.js:315:20)
    at Receiver.receiverOnMessage (C:\Users\ryssu\source\repos\SCP\079\node_modules\ws\lib\websocket.js:797:20)

错误消息是:

data = json.decode(response.body) as Map;
  if (pick(data, "media_details", "sizes", "thumbnail", "source_url")

如果有人可以帮助我,我将非常感谢。

1 个答案:

答案 0 :(得分:0)

您实际上并没有在commands集合中设置任何命令,因此,任何获取命令的尝试都将返回未定义的内容。

// create the collection
client.commands = new Discord.Collection();

// get an array of every file in the commands folder
const commandFiles = fs.readdirSync('./commands').filter(file => file.endsWith('.js'));

// iterate a function through every file
for (file of commandFiles) {
 const command = require(`./commands/${file}`);
 
 // map the command to the collection with the key as the command name, 
 // and the value as the whole exported object
 client.commands.set(command.name, command);
};