【问题标题】:Multiple Prefixes in discord.js botdiscord.js 机器人中的多个前缀
【发布时间】:2021-04-09 22:11:41
【问题描述】:

我试图让我的不和谐机器人有多个前缀,但这是我为我的botconfig.json 运行的代码

{
    "token":"<bot token>",
    "prefix": "p!"
}

我想做的是有多个前缀:p!、P!、!p、!P 和 ;

我该怎么做?

【问题讨论】:

    标签: javascript discord discord.js bots


    【解决方案1】:

    解决此问题的另一种方法是检查使用的前缀是否包含在前缀数组中。这将避免笨重的 for 循环,当您继续向机器人添加更多功能时,这可能会导致稍后出现问题。一个简单的例子说明如何做到这一点:

    const cfg = require('botconfig.json');
    
    if(cfg.prefixes.includes(msg.content.startsWith)) {
        //Your Code
    }
    

    【讨论】:

      【解决方案2】:

      尝试使用如下数组:

      prefixes = ["p!", "!p", ";"]
      
      //check if prefix
      for(let i=0;i<prefixes.length;i++){
         if (msg.content.startsWith(prefixes[i].toLowerCase() || msg.content.startsWith(prefixes[i]).toUpperCase){
              // your code
         }
      }
      

      或者像你做的那样,你可以做

      {
          'token' : '<token>',
          'prefixes': ['p!', '!p', ';']
      }
      

      【讨论】:

      • 我尝试使用数组,但它没有响应我的任何代码。使用任何前缀。
      • 哦,你能不能展示一下代码,以便我们调试它
      猜你喜欢
      • 2020-12-05
      • 1970-01-01
      • 2020-12-26
      • 2021-11-04
      • 2021-04-07
      • 1970-01-01
      • 2020-11-25
      • 2021-05-02
      • 2021-02-02
      相关资源
      最近更新 更多