问题
I want to have my discord.js bot respond
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.
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
}
})
回答1:
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/switch
A default clause; if provided, this clause is executed if the value of expression doesn't match any of the case clauses.
Example:
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 + '.');
}
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`);
}
})
来源:https://stackoverflow.com/questions/58336435/discord-bot-respond-unknown-command-when-using-an-incorrectly-spelled-command