【问题标题】:Overlapping commands?重叠命令?
【发布时间】:2019-09-23 03:24:33
【问题描述】:

我正在尝试使用 JavaScript 和 node.js 制作一个有趣的小不和谐聊天机器人,我想输入一个特定的命令,而不影响我已经设置的另一个命令。

她在我拥有的所有服务器上都表现出色,而且我已经对其进行了设置,因此当服务器中有人说“rei are”时,她会从 areResponses 中返回一个常量。

//const!!!
const areResponses = ["HELL yeah!", "Yep!", "I'm pretty sure that's true!", "I\'m not gonna put all the responses here because then it'd be too long..."];

//theres some other bot stuff (console log, now playing) under here but it isn't relevant until...

//the basics...
      if (message.content.toLowerCase().includes("rei are")) {
          var response = areResponses [Math.floor(Math.random()*areResponses.length)];

          message.channel.send(response).then().catch(console.error);
      }

我希望发生的事情是,最好让这个命令在不触发我编码的“rei are”命令的情况下运行。

if(message.content.toLowerCase().includes("rei are you happy")) {
           message.channel.send("Yes, absolutely.");
       }

截至目前,每当我尝试输入上述命令时,它只会触发“rei are”命令和“rei are you happy”命令,并带有两条消息......

【问题讨论】:

  • String.prototype.includes() 将匹配任何包含“rei are”的字符串。所以你在里面写的任何东西都会触发第一个找到的测试用例。您可以将函数移到“rei are”测试上方,以便首先对其进行评估,或者简单地比较完全匹配。
  • @NickLeBlanc 仍然有两个命令同时触发,它只是将“rei are you gay”响应放在“rei are”响应之前。有没有办法阻止“rei are”测试完全触发这个命令?
  • 返回函数以停止执行流程或将两个测试放在 If() {} else if() {} 语句上。
  • @NickLeBlanc 您可能应该考虑将您的 cmets 作为帖子的答案。
  • @NickLeBlanc 效果很好!感谢您的输入!

标签: javascript node.js discord discord.js


【解决方案1】:

else/if 链实际上可以很好地工作!!!

      if(message.content.toLowerCase().includes("rei do you like girls")) {
            message.channel.send("Yes, absolutely. Girls are just... yeah...");
         }
      else if (message.content.toLowerCase().includes("rei are")) {
          var response = areResponses [Math.floor(Math.random()*areResponses.length)];

          message.channel.send(response).then().catch(console.error);
        }

您需要做的就是将与较大命令重叠的命令放在 else if 链的最底部,然后就可以了!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多