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

Posted

技术标签:

【中文标题】TypeError:无法读取未定义的属性“执行”【英文标题】:TypeError: Cannot read property 'execute' of undefined 【发布时间】:2021-06-06 06:32:25 【问题描述】:

最近我面临编写自己的 Discord 机器人的挑战,我想编写一个显示机器人 ping 的命令。如果我的主文件中有命令,它可以工作,但如果我把它放在一个单独的文件中,它会显示错误。 那是我的主要文件。 快速更新!显然问题仅针对该特定行,因为下一个命令完全相同

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`);
 // Define each file inside /commands/ folder
 for(const file of commandFiles)
   // Set the command equal to the file
    const command = require(`./commands/$file`);

     // Add the command to the collection
     client.commands.set(command.name, command);




   client.once('ready', () => 
      console.log('Bor has entered the Bar')
);
        
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); 
      
      
      if(command =='clan')
         client.commands.get('clan').execute(message, args); 
      

      else if(command =='invite')
         message.channel.send('here is a invite link :) https://discord.gg/yPKDtzBv7s')   //discord invite
      

      else if(command =='random')
         message.channel.send('https://www.youtube.com/watch?v=pct1uEhAqBQ')      //random Vid
      

      else if(command =='rick')                                                // rick Astley never gonna give u aup
         message.channel.send('https://www.youtube.com/watch?v=dQw4w9WgXcQ')
      
   )

client.login('MY LOGIN');

这是 ping 码

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

module.exports = 
    description: "Get the latency of the bot.",
    usage: ,
    examples: ,
    aliases: [ "pong", "latency", "uptime" ],
    permissionRequired: 0,
    checkArgs: (args) => !args.length
  

module.exports.execute = async function (client, message, args, config, gdb, prefix, permissionLevel, db) 
    let botMsg = await message.channel.send("Pinging")
                    botMsg.edit( 
                    embed: 
                    name: "ping",
                    title: "???? Ping",
                    color: 0x2ed32e,
                    description: [
                        "**Server**: `" + (message.createdAt - message.createdAt) + "ms`",
                        "**API**: `" + Math.round(client.ws.ping) + "ms`",
                        "**Uptime**: `" + msToTime(client.uptime) + "`"
                    ].join("\n"),
                    footer:  text: "Requested by " + message.author.tag, icon_url: message.author.displayAvatarURL 
                
            )
        

        function msToTime(ms) 

还有错误: C:\用户...\桌面\DiscordBot\main.js:37 client.commands.get('ping').execute(message, args); ^

在客户端。 (C:\Users...\Desktop\DiscordBot\main.js:37:37) 在 Client.emit (events.js:315:20) 在 MessageCreateAction.handle (C:\Users...\Desktop\DiscordBot\node_modules\discord.js\src\client\actions\MessageCreate.js:31:14) 在 Object.module.exports [as MESSAGE_CREATE] (C:\Users...\Desktop\DiscordBot\node_modules\discord.js\src\client\websocket\handlers\MESSAGE_CREATE.js:4:32) 在 WebSocketManager.handlePacket (C:\Users...\Desktop\DiscordBot\node_modules\discord.js\src\client\websocket\WebSocketManager.js:384:31) 在 WebSocketShard.onPacket (C:\Users...\Desktop\DiscordBot\node_modules\discord.js\src\client\websocket\WebSocketShard.js:444:22) 在 WebSocketShard.onMessage (C:\Users...\Desktop\DiscordBot\node_modules\discord.js\src\client\websocket\WebSocketShard.js:301:10) 在 WebSocket.onMessage (C:\Users...\Desktop\DiscordBot\node_modules\ws\lib\event-target.js:132:16) 在 WebSocket.emit (events.js:315:20) 在 Receiver.receiverOnMessage (C:\Users...\Desktop\DiscordBot\node_modules\ws\lib\websocket.js:825:20)

【问题讨论】:

【参考方案1】:

您的filter 函数不正确。您当前的 endsWith 实现将始终返回 false,因为您没有传递任何参数。要更改此设置,请将 ".js" 添加为参数,以便它查找 javascript 文件:

const commandFiles = fs.readdirSync('./commands/').filter(file => file.endsWith(".js"));

编辑:

另一个问题是您的module.exports 缺少名称属性。它应该是这样的:

module.exports = 
    name: "ping",
    description: "Get the latency of the bot.",
    usage: ,
    examples: ,
    aliases: [ "pong", "latency", "uptime" ],
    permissionRequired: 0,
    checkArgs: (args) => !args.length
 

现在当你这样做时,它应该可以工作了:

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

之前,command.name 只是 undefined

【讨论】:

我仍然收到错误 从 vsc 终端看来,点 client.commands.get('ping').execute(message, args);在 ('ping') 后面

以上是关于TypeError:无法读取未定义的属性“执行”的主要内容,如果未能解决你的问题,请参考以下文章

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

[karma-server]:TypeError:无法读取未定义的属性“范围”-CI 环境中的 Angular 单元测试

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

(节点:47028)UnhandledPromiseRejectionWarning:TypeError:无法读取未定义的属性“表情符号”

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

TypeError:无法使用 TSLint 和 TypeScript 读取未定义的属性“getFullWidth”