【问题标题】:Cannot read property 'execute' of undefined while trying to run a command in discord尝试在不和谐中运行命令时无法读取未定义的属性“执行”
【发布时间】:2021-06-03 18:17:58
【问题描述】:

我正在尝试在一个单独的文件中为我的机器人创建一个 ping 命令,我直接复制了其他人的代码,代码对他们有用,但代码对我来说仍然失败(我对这一切都是新手) (出于隐私原因,我隐藏了 client.login 代码,这是正确的代码。)

const Discord = require('discord.js');
const client = new Discord.Client();
 
const prefix = '!';
 
const fs = require('fs');
 
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.once('ready', () => {
    console.log('Codelyon is online!');
});
 
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 == 'ping'){
        client.commands.get('ping').execute(message, args);
    } 
});

client.login(mycode);

这是我的 ping.js 文件,它位于我的命令文件夹中

module.exports.execute = {
    name: 'ping',
    description: "this is a ping command!",
    execute(message, args){
        message.channel.send('pong!');
    }
}

这是我尝试在我的不和谐服务器中运行命令时弹出的错误

C:\Users\kkeho\Desktop\GenesisBot\Genesis.js:29
        client.commands.get('ping').execute(message, args);
                                   ^

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

【问题讨论】:

    标签: node.js discord.js


    【解决方案1】:

    ping.js 模块正在导出

    {
      execute: {
        name: 'ping',
        description: 'this is a ping command!',
        execute(message, args) {
          message.channel.send('pong!')
        },
      },
    }
    

    因为module.exports.execute = {...}

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

    command.nameundefined,因为required 模块只有一个execute 属性,而namedescriptionexecute 属性嵌套在其中。因此,ping 命令不会添加到client.commands 集合中,从而导致client.commands.get('ping') 返回undefined。修复导入,您应该一切顺利。

    module.exports = {
      name: 'ping',
      description: 'this is a ping command!',
      execute(message, args) {
        message.channel.send('pong!')
      },
    }
    

    if (command == 'ping') {
      client.commands.get('ping').execute(message, args)
    }
    

    当您添加更多命令时,此模式会导致您添加更多 if 条件。您可以改为使用Map.prototype.has 来检查command 键是否存在于client.commands 集合中。

    if (client.commands.has(command)) {
      client.commands.get(command).execute(message, args)
    }
    

    【讨论】:

      【解决方案2】:

      如果我们再把它分解一下,可能会更容易理解。

      当您在节点中导出模块时,您正在导入属于module.exports 属性的任何内容。

      您的ping.js 文件如下所示。

      module.exports.execute = {
        name: 'ping',
        description: "this is a ping command!",
        execute(message, args){
            message.channel.send('pong!');
        }
      }
      

      这相当于...

      module.exports = {
        execute: {
          name: 'ping',
          description: "this is a ping command!",
          execute(message, args){
              message.channel.send('pong!');
          }
        }
      }
      

      注意名称、描述和执行函数是如何嵌套在对象execute 中的,它是module.exports 的属性?我们不希望这样,因为这极大地改变了我们导入模块的方式。

      namedescriptionexecute 都应该直接暴露给 module.exports 对象。像这样。

      module.exports = {
        name: 'ping',
        description: "this is a ping command!",
        execute(message, args){
            message.channel.send('pong!');
        }
      }
      

      【讨论】:

        猜你喜欢
        • 2021-01-25
        • 1970-01-01
        • 2018-12-11
        • 2021-01-23
        • 2019-01-27
        • 2018-02-04
        • 2021-10-03
        • 2020-11-02
        • 2020-05-22
        相关资源
        最近更新 更多