首页 > 解决方案 > TypeError:无法读取未定义的属性“执行”

问题描述

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

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)
    })
  }```

标签: node.jsdiscord.js

解决方案


你忘了实际设置命令。此块仅适用于没有子文件夹的命令。

// 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);
}

推荐阅读