【问题标题】:TypeError: Cannot read property 'execute' of undefined (Discord.js)TypeError:无法读取未定义的属性“执行”(Discord.js)
【发布时间】:2021-04-30 19:35:29
【问题描述】:

目前有一个问题,我收到错误“TypeError: Cannot read property of 'execute' of undefined”。我目前正在学习 Javascript,并从测试视频中复制了代码的 pastebin,似乎连他的代码都无法在我的编译器上运行。

index.js

const fs = require('fs');
const Discord = require('discord.js');
const bot = new Discord.Client({ partials: ["MESSAGE", "CHANNEL", "REACTION" ]});
const { prefix, token } = require('./config.json');

bot.commands = new Discord.Collection();

//File Reading
const commandFiles = fs.readdirSync('./commands/').filter(file => file.endsWith('.js'));
for (const file of commandFiles) {
    const command = require(`./commands/${file}`);

    bot.commands.set(command.name, command);
}

//Command Callbacks
bot.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 === 'queue') {
        bot.commands.get('queue').execute(message, args, Discord, bot);
    }
})

//Bot is Running
bot.once('ready', () => {
    console.log('Good Noodle Bot is online!');
});

//Login
bot.login(token);

queue.js

module.exports = {
    name: 'queue',
    description: "Begins a Competitive Queue",
    async execute(message, args, Discord, bot) {
        const channel = '802325198191067146';

        const silverRank = ':silver:803746337468186665';
        const novaRank = ':nova:803746340009279538';

        let embed = new Discord.MessageEmbed()
            .setColor('#e42643')
            .setTitle('Choose a rank to play with!')
            .setDescription('Choosing a rank will begin a queue for that rank!\n\n'
                + `${silverRank} for Silver\n`,
                + `${novaRank} for Nova`);

        let messageEmbed = await message.channel.send(embed);
        messageEmbed.react(silverRank);
        messageEmbed.react(novaRank);
    }
}

错误:

TypeError: Cannot read property 'execute' of undefined
    at Client.<anonymous> (E:\Visual Studio\Discord\index.js:25:34)
    at Client.emit (node:events:379:20)
    at MessageCreateAction.handle (E:\Visual Studio\Discord\node_modules\discord.js\src\client\actions\MessageCreate.js:31:14)
    at Object.module.exports [as MESSAGE_CREATE] (E:\Visual Studio\Discord\node_modules\discord.js\src\client\websocket\handlers\MESSAGE_CREATE.js:4:32)
    at WebSocketManager.handlePacket (E:\Visual Studio\Discord\node_modules\discord.js\src\client\websocket\WebSocketManager.js:384:31)
    at WebSocketShard.onPacket (E:\Visual Studio\Discord\node_modules\discord.js\src\client\websocket\WebSocketShard.js:444:22)
    at WebSocketShard.onMessage (E:\Visual Studio\Discord\node_modules\discord.js\src\client\websocket\WebSocketShard.js:301:10)
    at WebSocket.onMessage (E:\Visual Studio\Discord\node_modules\ws\lib\event-target.js:132:16)
    at WebSocket.emit (node:events:379:20)
    at Receiver.receiverOnMessage (E:\Visual Studio\Discord\node_modules\ws\lib\websocket.js:825:20)

【问题讨论】:

  • 打印出commandFiles变量的值。

标签: javascript node.js discord.js


【解决方案1】:

我错误地将命令放在另一个子文件夹 (commands/folder/command.js) 中

【讨论】:

    猜你喜欢
    • 2021-07-03
    • 1970-01-01
    • 1970-01-01
    • 2023-01-14
    • 2020-07-14
    • 2019-07-11
    • 2021-05-29
    • 2020-08-17
    • 1970-01-01
    相关资源
    最近更新 更多