0

I have an image command for my bot. This is used for memes and other funny pictures. My bot just scrapes Google for the best result. But on the other side, you can also look up, well, NSWF images. Since I don't want people sending those, I am trying to build a profanity filter. It now works like this:

$image cheese 

If the word cheese is chosen, nothing should happen, but if there is something NSWF, the bot should detect that and delete the $image NSFW-word command. I already have a list of profanity list, now just the implementation. My command now, that doesn't work is:

    const blacklist = require("./../Other/profanity.js");

    blacklist.forEach((word) => {
      if (message.content.toLowerCase().includes(word));
      message.delete();
      message.channel.send("Let's try to keep it family friendly!");
      console.log(`${message.author} used the word ${word} in an image search.`)
    }); 

My profanity list (profanity.js) looks like this:

module.exports = [
"NSFW1",
"NSFW2",
"NSWF3",
"Etc"
]

Also, I should note, that piece of code is broken. Even if someone sends cheese, it spams (infinite times, untill I shut-down the bot) the 'lets-keep-it-family-friendly' phrase. I searched the whole internet, but couldn't find the right fit.

So can anyone help me?

  • Clbuttic [Scunthorpe](https://en.wikipedia.org/wiki/Scunthorpe_problem) BTW… – deceze Jan 25 '22 at 08:53
  • Possible duplicate of [How do I check if an array includes a value in JavaScript?](https://stackoverflow.com/questions/237104/how-do-i-check-if-an-array-includes-a-value-in-javascript) – MrMythical Jan 25 '22 at 12:46

1 Answers1

3

It doesn't work because you have a mistake in the "if" part (if something end). And actually the "if" doesn't work and it executes the next functions for each word in the array.

Maybe something like this:

blacklist.forEach(word => {
    if (message.content.toLowerCase().includes(word.toLowerCase())){
        message.delete();
        message.channel.send("Let's try to keep it family friendly!");
        console.log(`${message.author} used the word ${word} in an image search.`)

        return; // stop forEach
    }
});

You can also use includes() method to check if the word is in the string array blacklist.includes(word). Note that it will look for exact same string so it might not be useful in this case.

spect423
  • 215
  • 3
  • 8