【问题标题】:I keep on getting the error: cannot read property 'execute' of undefined我不断收到错误:无法读取未定义的属性“执行”
【发布时间】:2021-10-23 12:32:21
【问题描述】:
const Discord = require('discord.js');

const client = new Discord.Client();

const prefix = '-';

const fs = require('fs');

client.command = new Discord.Collection();

const commandFiles = fs.readdirSync('./commands/').filter(file => file.endsWith('.js'));
for (const file of commandFiles) {
    const command = require(`./commands/${file}`);

    client.command.set(command.name, command);
}

client.once('ready', () => {
    console.log('Primio Has Activated :)');
});

client.on('message', message => {
    if(!message.content.startsWith(prefix) || message.author.bot) return;

const args = message.content.slice(prefix.length).split(/ +/);
const command = args.shift().toLowerCase();

    if (command === 'flip') {
        client.command.get('flip').execute(message, args,);

    } else if (command === 'help') {
      client.command.get('help').execute(message, args,);
}
});


client.login('token');

我不断收到错误提示:

TypeError: Cannot read property 'execute' of undefined
    at Client.<anonymous> (C:\Users\Pokey\Desktop\Primio\main.js:29:35)
    at Client.emit (node:events:394:28)
    at MessageCreateAction.handle (C:\Users\Pokey\Desktop\Primio\node_modules\discord.js\src\client\actions\MessageCreate.js:31:14)
    at Object.module.exports [as MESSAGE_CREATE] (C:\Users\Pokey\Desktop\Primio\node_modules\discord.js\src\client\websocket\handlers\MESSAGE_CREATE.js:4:32)
    at WebSocketManager.handlePacket (C:\Users\Pokey\Desktop\Primio\node_modules\discord.js\src\client\websocket\WebSocketManager.js:384:31)
    at WebSocketShard.onPacket (C:\Users\Pokey\Desktop\Primio\node_modules\discord.js\src\client\websocket\WebSocketShard.js:444:22)
    at WebSocketShard.onMessage (C:\Users\Pokey\Desktop\Primio\node_modules\discord.js\src\client\websocket\WebSocketShard.js:301:10)
    at WebSocket.onMessage (C:\Users\Pokey\Desktop\Primio\node_modules\ws\lib\event-target.js:132:16)
    at WebSocket.emit (node:events:394:28)
    at Receiver.receiverOnMessage (C:\Users\Pokey\Desktop\Primio\node_modules\ws\lib\websocket.js:833:20)

【问题讨论】:

  • 看起来client.command.get('flip') 返回未定义。

标签: javascript node.js discord.js


【解决方案1】:

嘿,如果您使用这样的命令处理程序,您需要向我们展示 flip.js。开始时,flip.js 需要位于文件夹名称 commands 中,然后在文件中您需要添加以下代码: module.exports ={ name:'flip' desciption: 'your comment' execute(message,args,Discord){your code here}} 您犯错的第二件事是执行中的 , 到目前为止,您的代码应该看起来像这样if (command === 'flip') { client.commands.get('flip').execute(message, args, Discord); 如果您仍然收到错误,这可能意味着您的执行(消息、参数、不和谐)与 flip.js 中的不同。如果问题解决了给我反馈:D

【讨论】:

    猜你喜欢
    • 2021-02-12
    • 1970-01-01
    • 2019-04-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-10-08
    • 2021-10-01
    • 2021-03-03
    相关资源
    最近更新 更多