【问题标题】:DiscordJS Message property 'find' of undefinedDiscordJS 消息属性“查找”未定义
【发布时间】:2020-12-07 14:41:05
【问题描述】:


我希望您对我的代码提供帮助:我正在尝试创建一个 Discord 机器人,但我的消息事件有问题。

当用户发送一个不存在的命令时,检查该命令是否存在(然后可能执行它)的步骤会返回一个错误“TypeError: Cannot read property 'find' of undefined”。
据我了解,他无法在 `client.commands` 上执行 `find` 方法,因为它将是 `undefined`,除了我已经检查了 `console.log` 并且 `client.commands` 有所有命令并且不是“未定义”...


main.js(message.js 中使用的 `client` 变量来自这里)
const { Client, Collection } = require('discord.js');
const { loadCommands, loadEvents } = require('./utils/loader');
const { TOKEN } = require('./config');

const client = new Client();
['commands', 'cooldowns'].forEach(x => client[x] = new Collection());

loadCommands(client);
loadEvents(client);

client.login(TOKEN);

message.js(消息事件的文件)
const { Collection } = require('discord.js'); // Retrieving DiscordJS functionalities
const { PREFIX } = require('../../config'); // Retrieving the information contained in the file 'config.js'

module.exports = (client, message) => {
    
    if (!message.content.startsWith(PREFIX) || message.author.bot) return; // Ignore the message if it's not a command
    const args = message.content.slice(PREFIX.length).split(/ +/); // Arguments can be found within the command
    const commandName = args.shift().toLowerCase(); // We establish the command that is requested
    const user = message.mentions.users.first();

    // Check if the requested command exists
    // THE ERROR IS ON THE LINE BELOW
    const command = client.commands.get(commandName) || client.commmands.find(cmd => cmd.help.aliases && cmd.help.aliases.includes(commandName));
    if (!command) return;

    [...]

    // Execution of the command
    command.run(client, message, args);
    
}

控制台中返回的错误
D:\Developpement\Chamiro\events\client\message.js:11
    const command = client.commands.get(commandName) || client.commmands.find(cmd => cmd.help.aliases && cmd.help.aliases.includes(commandName));
                                                                         ^

TypeError: Cannot read property 'find' of undefined
    at module.exports (D:\Developpement\Chamiro\events\client\message.js:11:74)
    at Client.emit (events.js:315:20)
    at MessageCreateAction.handle (D:\Developpement\Chamiro\node_modules\discord.js\src\client\actions\MessageCreate.js:31:14)
    at Object.module.exports [as MESSAGE_CREATE] (D:\Developpement\Chamiro\node_modules\discord.js\src\client\websocket\handlers\MESSAGE_CREATE.js:4:32)
    at WebSocketManager.handlePacket (D:\Developpement\Chamiro\node_modules\discord.js\src\client\websocket\WebSocketManager.js:386:31)
    at WebSocketShard.onPacket (D:\Developpement\Chamiro\node_modules\discord.js\src\client\websocket\WebSocketShard.js:436:22)
    at WebSocketShard.onMessage (D:\Developpement\Chamiro\node_modules\discord.js\src\client\websocket\WebSocketShard.js:293:10)
    at WebSocket.onMessage (D:\Developpement\Chamiro\node_modules\ws\lib\event-target.js:125:16)
    at WebSocket.emit (events.js:315:20)
    at Receiver.receiverOnMessage (D:\Developpement\Chamiro\node_modules\ws\lib\websocket.js:797:20)

有点帮助会很好:) 谢谢

【问题讨论】:

  • 您使用了 commmands 和 3 m 而不是 2。
  • 我想打自己...谢谢Titus,我已经尝试了好几个小时试图理解,但我没有看到,我是个混蛋^^

标签: javascript discord.js undefined


【解决方案1】:

第 11 行中似乎有拼写错误。那里的命令是用 3x "m" 写的

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-09-06
    • 2019-06-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-02-05
    相关资源
    最近更新 更多