TypeError:无法读取discord.js中未定义的属性“ execute”

时间:2020-09-21 23:17:41

标签: javascript node.js discord.js

编辑:此问题已解决

尝试在不一致中执行!fish命令时遇到以下错误:

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

所有其他命令似乎运行正常。只是“ fishCommand”给出了错误。 fishCommand是在fish.js中定义的

Main.js:

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('DiscordBot 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);
    } else if (command == 'youtube'){
        client.commands.get('youtube').execute(message, args);
    } else if (command == 'yeet'){
        client.commands.get('yeet').execute(message, args);
    } else if (command == 'modtoggle'){
        client.commands.get('modtoggle').execute(message, args);
    } else if (command == 'fish'){
        client.commands.get('fishCommand').execute(message, args);
    } else if (command == 'backpack'){
        client.commands.get('backpack').execute(message, args);
    }
});

client.login('Discord Bot token here');

fish.js:

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

// embeds that are picked at random
const rawFish = new Discord.MessageEmbed()
.setColor('#0077be')
.setTitle('You caught a raw fish!')
.setDescription('It has been added to your backpack.')
.setThumbnail('https://vignette.wikia.nocookie.net/minecraft-computer/images/7/77/Raw_Fish.png/revision/latest/scale-to-width-down/340?cb=20130815172634')

const rawSalmon = new Discord.MessageEmbed()
.setColor('#0077be')
.setTitle('You caught a raw salmon!')
.setDescription('It has been added to your backpack.')
.setThumbnail('https://vignette.wikia.nocookie.net/minecraft/images/f/fd/RawSalmonOldTexture.png/revision/latest/window-crop/width/200/x-offset/0/y-offset/0/window-width/201/window-height/200?cb=20200519072953')

const pufferfish = new Discord.MessageEmbed()
.setColor('#0077be')
.setTitle('You caught a pufferfish!')
.setDescription('It has been added to your backpack.')
.setThumbnail('https://static.wikia.nocookie.net/minecraft_gamepedia/images/0/02/Pufferfish_%28item%29_JE5_BE2.png/revision/latest/scale-to-width-down/150?cb=20191230044452')

const tropicalFish = new Discord.MessageEmbed()
.setColor('#0077be')
.setTitle('You caught a tropical fish!')
.setDescription('It has been added to your backpack.')
.setThumbnail('https://static.wikia.nocookie.net/minecraft_gamepedia/images/a/ad/Tropical_Fish_JE2_BE2.png/revision/latest/scale-to-width-down/150?cb=20191230044408')

const fishTypes = [rawFish, rawSalmon, pufferfish, tropicalFish];

module.exports = {
    name: 'fishCommand',
    description: "Lets the user fish",
    execute(message, args){

        //sends a random embed from fishTypes
        message.channel.send(fishTypes[Math.floor(Math.random() * fishTypes.length)]);
        
       
        }
    }

    var backpack = 0;

 module.exports = {
    name: 'backpack',
    description: "Lets the user see their backpack",
    execute(message, args){

        message.channel.send('Your backpack has ' + backpack + ' fish in it.');
           
            }
        }

1 个答案:

答案 0 :(得分:0)

您的命令文件(fish.js)的名称应与客户端获取的文件名称相同。

client.commands.get('fishCommand').execute(message, args);

文件名应为'fishCommand.js',或者您应将其写为get('fish')

client.commands.get('fish').execute(message, args);

execute未定义,因为您的客户不知道它位于何处。