Discord 机器人:响应“未知命令";使用拼写错误的命令时 [英] Discord bot: Respond "Unknown command" when using an incorrectly spelled command

查看:17
本文介绍了Discord 机器人:响应“未知命令";使用拼写错误的命令时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想让我的 discord.js 机器人响应

I want to have my discord.js bot respond

未知命令,使用 c!help 获取可用命令"在执行类似 c!hep(拼写错误)或未实现的不同类型命令(如 c!youtube)或只是将随机字母(如 c!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/en-US/docs/Web/JavaScript/Reference/Statements/switch

默认子句;如果提供,则如果表达式的值与任何 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆