类型错误:无法读取未定义的属性“执行”

时间:2021-03-08 11:13:51

标签: javascript node.js discord discord.js

最近我面临着编写自己的 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:\用户...\桌面\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 个答案:

答案 0 :(得分:0)

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

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

编辑:

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

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