首页 > 解决方案 > 如果机器人无法识别命令(命令处理程序)discord.js,如何让机器人响应未知命令

问题描述

我正在努力做到这一点,所以如果有人执行 &hep 或 &asdsw 之类的操作,它会说未知命令,因为没有与用户输入的命令匹配的命令。基本上相同的未知命令。我看到了另一篇文章,但那不是针对命令处理程序的。我没有写出任何代码。谢谢!

标签: node.jsdiscord.js

解决方案


由于命令存储在集合中,您可以使用Collection.get()并检查命令是否存在。

const Collection = new Discord.Collection();

// This part should be already done by your command handler.
Collection.set("commandName", "commandFile");

// Getting the command (assuming the key is the name of the command)
const Command = Collection.get("commandName");
if (!Command) return message.channel.send("The command does not exist.");

// The command exists.

推荐阅读