【问题标题】:Discord Bot crashes when a command executed in discord.js在 discord.js 中执行命令时,Discord Bot 崩溃
【发布时间】:2021-04-07 03:03:48
【问题描述】:

我正在制作一个 Discord Bot。现在我添加了命令处理。 Bot 启动正常,但如果我输入命令,它会出现以下错误代码:

C:\Users\Matteo\sudo_\sudocanary\index.js:24
        client.commands.get('ping').execute(message, args);
                                   ^

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

Process finished with exit code 1

这是我在 index.js 中的代码:

const fs = require('fs')
const Discord = require('discord.js');
const { prefix, token } = require('./config.json');

const client = new Discord.Client();
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.on('ready', () => {
    console.log('Ready!');
});

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



client.login(token);

现在是来自 ping.js 的代码:

module.exports.execute = {
    name: 'ping',
    description: 'Ping!',
    execute(message) {
        message.channel.send('Pong.');
    },
};

谢谢! (我使用的是 discord.js v12)

【问题讨论】:

  • module.exports 而不是module.exports.execute。此外,您不会向 client.commands 集合添加任何内容。

标签: node.js discord discord.js bots


【解决方案1】:

在您的 index.js 中,您需要将命令添加到您的 client.commands 才能实际使用它们。您还在 ping.js 中使用了module.exports.execute,而您应该只使用module.exportsexecute() 方法已在您的module.exports 中定义为方法execute(message))。

这是您代码中的修复。

index.js:

const client = new Discord.Client();
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);
}

ping.js:

module.exports = {
    name: 'ping',
    description: 'Ping!',
    execute(message) {
        message.channel.send('Pong.');
    },
};

请仔细查看您尝试使用的命令处理程序的示例,因为它们显示了执行此操作的正确方法并解释了命令处理程序的每个部分的作用。我建议通过 the discordjs.guide command handler examples 了解一切是如何运作的。

【讨论】:

    猜你喜欢
    • 2020-10-30
    • 2020-04-07
    • 2020-09-10
    • 2020-09-03
    • 2021-04-02
    • 2017-06-04
    • 2021-01-06
    • 2018-01-03
    • 1970-01-01
    相关资源
    最近更新 更多