如何解决DiscordJS消息属性“查找”未定义
我需要您的代码帮助:我正在尝试创建Discord机器人,但是我的消息事件有问题。
当用户发送不存在的命令时,检查命令是否存在(然后可能执行)的步骤向我返回错误“ TypeError:无法读取未定义的属性'find'”。
据我了解,他无法在client.commands上执行find方法,因为它将是未定义的,除了我已经检查了console.log和client.commands具有所有的命令,并且不是`undefined` ...
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)
一点帮助就好了:) 谢谢
解决方法
看起来像第 11 行的拼写错误。命令是用 3x "m" 写的
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。