【发布时间】:2021-10-01 03:29:21
【问题描述】:
我正在通过制作不和谐机器人来学习节点,我的目标是通过制作不和谐机器人来了解数据库如何与客户端交互。我在命令处理程序中的机器人中遇到了错误。
TypeError:无法读取未定义的属性“执行” client.commands.get('ping').execute(message, args);**
发生错误的索引文件部分。
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.on('message', message =>{
if(!message.content.startsWith(prefix) || message.author.bot) return;
const args = message.content.slice(prefix.length).split(/ +/);
const command = args.shift().toLocaleLowerCase();
if(command === 'ping'){
client.commands.get('ping').execute(message, args);
}
});
下一部分是实际的命令
name: 'ping',
description: "Simple Ping Command",
execute(message, args){
message.channel.send(`Latency is ${Date.now() - message.createdTimestamp}ms. Bot Latency is ${Math.round(client.ws.ping)}ms`);
}
}
【问题讨论】:
-
这意味着
client.commands.get('ping')未定义。您的 ping 文件是否在commands文件夹中? -
在请求和加载命令之前,您是否使用
client.commands = new Discord.Collection()初始化了一个新集合? -
是的,我已经通过使用“使用”而不是执行解决了这个问题,
标签: javascript node.js discord discord.js