我试图让我的机器人检测命令何时无效(在列表中)并返回一条消息,告诉用户该命令无效,我尝试了很多方法,并且一直在说无效每条消息的命令消息,甚至是有效命令。
if (message.content.startsWith(`${prefix}`)) {
let commands = ["help", "mod", "ping", "serverinfo", "8ball", "suggest", "coinflip", "botinfo", "avatar", "info", "serverlogo", "ssu", "close"];
if (!message.content.includes(commands)) {
return message.channel.send(`Sorry it looks like you have entered an invalid command, Please use ${prefix}help to get a list of useable commands`)
}
}
答案 0 :(得分:2)
您正在测试错误的方法。您应该测试数组是否包含消息字符串,而不是如果消息字符串包含数组。
if (!commands.includes(message.content)) { // This doesn't work exactly
您需要做的另一件事是从邮件内容和所有后续参数中删除前缀。
if (!commands.includes(message.content.replace(prefix, "").split(' ')[0])) { // This first removes the first instance of the prefix, then gets the first argument, which is the command
答案 1 :(得分:0)
您需要从message.content中删除前缀,并且检查方式不正确:
update=TRUE