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

时间:2020-09-22 17:41:12

标签: node.js discord.js

在开始之前,我真的很抱歉发布新帖子,但是我觉得旧帖子很乱,并且没有提供解决此抱歉问题所需的适当信息。 因此,我已经开始制作自己的discord机器人,并且运行得很好,但是一天之后,每次尝试使用命令时,我都会收到一条错误消息,以版本命令为例(问题出在每个命令上)机器人启动后,我收到一条启动消息,但是每次我尝试使用命令时,控制台都会响应

TypeError: Cannot read property 'execute' of undefined
    at Client.<anonymous> (C:\Users\Reven\Desktop\DiscordBot\index.js:42:39)
    at Client.emit (events.js:310:20)
    at MessageCreateAction.handle (C:\Users\Reven\Desktop\DiscordBot\node_modules\discord.js\src\client\actions\MessageCreate.js:31:14)
    at Object.module.exports [as MESSAGE_CREATE] (C:\Users\Reven\Desktop\DiscordBot\node_modules\discord.js\src\client\websocket\handlers\MESSAGE_CREATE.js:4:32)
    at WebSocketManager.handlePacket (C:\Users\Reven\Desktop\DiscordBot\node_modules\discord.js\src\client\websocket\WebSocketManager.js:384:31)
    at WebSocketShard.onPacket (C:\Users\Reven\Desktop\DiscordBot\node_modules\discord.js\src\client\websocket\WebSocketShard.js:444:22)
    at WebSocketShard.onMessage (C:\Users\Reven\Desktop\DiscordBot\node_modules\discord.js\src\client\websocket\WebSocketShard.js:301:10)
    at WebSocket.onMessage (C:\Users\Reven\Desktop\DiscordBot\node_modules\ws\lib\event-target.js:125:16)
    at WebSocket.emit (events.js:310:20)
    at Receiver.receiverOnMessage (C:\Users\Reven\Desktop\DiscordBot\node_modules\ws\lib\websocket.js:797:20)

这是我当前拥有的代码

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'));

const welcome = require('./welcome')

client.once('ready', () =>{
    console.log('Aiko is working!');
    client.user.setActivity(' you!', { type: "LISTENING" });
});

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 === 'version'){
        client.commands.get('version').execute(message, args);

client.login('MyToken');

在version.js文件中,我有

module.exports = {
    name: 'version',
    description: "current version",
    execute(message, args){
        message.channel.send('Version 1.0.0 ALPHA build');
    }
}```

and the bot doesn't send welcome messages anymore this is the code that i use for them ```module.exports = (client) => {
    const channelId = '757493821251649608' // welcome channel
    const targetChannelId = '757521186929246219' // rules and info
  
    client.on('guildMemberAdd', (member) => {
      const message = `Hi, hope you enjoy your stay <@${
        member.id
      }> , Oh i almost forgot to tell you, check out! ${member.guild.channels.cache
        .get(targetChannelId)
        .toString()}`
  
      const channel = member.guild.channels.cache.get(channelId)
      channel.send(message)
    })
  }```

1 个答案:

答案 0 :(得分:1)

您忘记实际设置命令了。此块仅适用于不带子文件夹的命令。

// 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
    bot.commands.set(command.name, command);
}