首页 > 解决方案 > Is there is a way to check a word is same to another word?

问题描述

I am making bot that check if there is a user who said bad words. For some reason, it didn't do anything

    var badWord = "not"
    let splittedMessage = message.content.split(" ")
    
    for (let i=0; i < splittedMessage.length; i++) {
      for (let u=0; u < badWords.length; u++) {
        if (splittedMessage[i] === badWords[u]) {
          badWord = "yes"
        }
      }
    }
    
    if (badWord === "yes") {
      console.log("Deleting message")
      message.delete()
      message.channel.send(`${message.author} has been warned. Reason: Bad word usage`)
      message.author.send(`You has been warned in DoGame. Reason: Bad word usage`)
    }

Bad words array

const badWords = ["fuck","hell","shit","bitch","asshole","bastard"]

标签: javascriptdiscord.js

解决方案


An Idiot's Guide Has a great example of how to detect swear words in the message without the need of loops.

const swearWords = ["darn", "shucks", "frak", "shite"];
if (swearWords.some(word => message.content.toLowerCase().includes(word))) {
  message.reply("Oh no you said a bad word!!!");
}

推荐阅读