Discord漫游器:响应“未知命令"使用拼写错误的命令时 [英] Discord bot: Respond "Unknown command" when using an incorrectly spelled command
问题描述
我想让discord.js机器人响应
I want to have my discord.js bot respond
未知命令,在执行c!hep(拼写错误)或未实现的另一种命令(例如c!youtube)之类的命令时,请对可用命令使用c!help.rgoiw.
Unknown command, use c!help for available commands" when doing something like, c!hep (misspelled), or a different type of command not implemented, like, c!youtube, or just flat out random letters like c!rgoiw.
如果他们的消息与任何可用命令都不匹配,则基本上只是一个响应.
Basically just a response if their message doesn't match any commands available.
我没有任何特定的代码,我只是使用 const PREFIX ='c!';
和 let args = message.content.substring(PREFIX.length).split(")
并在 switch(args [0]){
块中设置所有命令.
I don't have any specific code, I'm just using the const PREFIX = 'c!';
with let args = message.content.substring(PREFIX.length).split(" ")
and setting all the commands in a switch(args[0]){
block.
我对编码一无所知,到目前为止,我所做的一切只是写出来的自我解释,但我不知道从头开始编写该怎么做.
I don't know really anything about coding, all that i've done so far is pretty self explanatory once wrote out, but I don't know what to go for when writing it from scratch.
还没有在线上看到有关未知命令响应的任何线程,因此我认为这可能是不可能的.谢谢
Haven't seen any threads online about an unknown command response so I'm assuming it might be impossible to do. Thanks
const PREFIX = 'c!';
bot.on('message', message=>{
let args = message.content.substring(PREFIX.length).split(" ")
switch(args[0]){
case 'example':
break;
//Code to respond to the prefix with no matching case
}
})
推荐答案
https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Statements/switch
默认子句;如果提供,则如果expression的值与任何case子句都不匹配,则执行此子句.
A default clause; if provided, this clause is executed if the value of expression doesn't match any of the case clauses.
示例:
switch (expr) {
case 'Oranges':
console.log('Oranges are $0.59 a pound.');
break;
case 'Mangoes':
case 'Papayas':
console.log('Mangoes and papayas are $2.79 a pound.');
// expected output: "Mangoes and papayas are $2.79 a pound."
break;
default:
console.log('Sorry, we are out of ' + expr + '.');
}
如上所示,如果没有匹配项(转换为您的用例-如果未识别命令),则执行在 default
As shown above, if nothing matches (to translate to your use case - if no command is recognized) then execute commands found under default
换句话说,您的代码应该是
In other words, your code should be
switch(args[0]){
case 'example':
break;
default: console.log(`Unknown command, use c!help for available commands`);
}
})
这篇关于Discord漫游器:响应“未知命令"使用拼写错误的命令时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!