【问题标题】:How to switch() variable with `x.some(word=>y.includes(word))` cases如何使用 `x.some(word=>y.includes(word))` 情况切换()变量
【发布时间】:2020-05-07 16:54:06
【问题描述】:

我有一个接受变量command 的机器人,这是用户发送的消息。我想switch(command) 并让每个案例返回true,只要command 的内容 myArr 中找到。我尝试了以下方法,但没有奏效。我的问题是什么是更好的切换方式command

const myArr = ['hello']
//const command will be the message sent by the user

switch(command){

   case myArr.some(word=>command.includes(word)):

      console.log('You said hello')

   break;

}

【问题讨论】:

  • 为什么要使用 switch 语句?您在这里的主要要求是什么?
  • 我打算让这成为一种助手。所以会有几十个数组检查用户消息以知道要执行哪个命令。 “你好”只是一个例子
  • 好吧,但是你为什么不使用简单的switch(command) case 'hello'..这里使用myArr的目的是什么?
  • 机器人识别命令的方式是通过用户消息中找到的关键字。例如,对于一个给出时间的命令,我希望有很多方法来询问时间,而不是一个固定的“给时间”命令。只要在消息中找到关键字“时间”,就会执行命令stackoverflow.com/questions/59128785/…
  • 好的,因此您将有多个数组来保存每个命令的所有可能值。所以,就像一个数组表示你好,另一个数组表示时间..

标签: javascript discord.js


【解决方案1】:

您可以在此处为当前场景使用if.. else if 逻辑,例如:

const myArr = ['Hello', 'Hi', 'Sup']
const timeArr = ['time', 'get time', 'clock']

// Get action based on command passed by user
function GetAction(command) {
  if (myArr.some(word => command.includes(word))) {
    console.log('You said hello')

  } else if (timeArr.some(word => command.includes(word))) {
    console.log('You said time')
  }
}

// Check Hello
GetAction('Hello')

setTimeout(function() {
  // Check get time after 2 seconds
  GetAction('get time')

  setTimeout(function() {
    // Check Hi after 2 seconds
    GetAction('Hi')
  }, 2000);

}, 2000);

另外,如果你想让它不区分大小写,即使用户输入HELLOhelloheLLo 等,但我们仍然应该执行命令,那么你可以使用 .toLowerCase() 方法,如:

const myArr = ['Hello', 'Hi', 'Sup']
const timeArr = ['time', 'get time', 'clock']

// Get action based on command passed by user
function GetAction(command) {
  if (myArr.some(word => command.toLowerCase().includes(word.toLowerCase()))) {
    console.log('You said hello')

  } else if (timeArr.some(word => command.toLowerCase().includes(word.toLowerCase()))) {
    console.log('You said time')
  }
}

// Check Hello
GetAction('HELLO')

setTimeout(function() {
  GetAction('hello')

  setTimeout(function() {
    GetAction('heLLo')
  }, 2000);
}, 2000);

【讨论】:

    【解决方案2】:

    您好 Elitezen!,这是实现您目标的更简单的代码:

       // your array
        const myArr = ['hello','bye']
        //const command will be the message sent by the user
        const command = 'hello'
        
        // function to get boolean value and inside the function print the value of the command
        function HelloBot(){
         
          for(let i = 0; i < myArr.length; i++){
            // condition
            if(command === myArr[i]){
              // print the value
              console.log(`You said ${myArr[i]}`)
              // if found return true
              return true;
            }
          }
        
          // else return false
          return false;
        }
        
        const result = HelloBot()
        
        console.log(result)

    我认为这将帮助您解决问题:)

    【讨论】:

      猜你喜欢
      • 2010-10-14
      • 2020-04-30
      • 2013-11-08
      • 2023-03-11
      • 2011-10-24
      • 1970-01-01
      • 1970-01-01
      • 2011-04-23
      • 1970-01-01
      相关资源
      最近更新 更多