How do I check if message content includes any items in an array?

前端 未结 4 1387
一生所求
一生所求 2021-01-03 11:25

I\'m making a discord bot and I\'m trying to make a forbidden words list using arrays. I can\'t seem to find out how to make this work. Here\'s my current code:



        
4条回答
  •  旧时难觅i
    2021-01-03 12:23

    The code you posted checks if the entire message's content is a member of your array. To accomplish what you want, loop over the array and check if the message contains each item:

    for (var i = 0; i < forbidenWords.length; i++) {
      if (message.content.includes(forbidenWords[i])) {
        // message.content contains a forbidden word;
        // delete message, log, etc.
        break;
      }
    }
    

    (By the way, you misspelled "forbidden" in your variable name)

提交回复
热议问题