0
votes

so I'm trying to make my discord bot filter specific words out which I have set in my Array:

let Blacklist = ['bad1', 'bad2', 'bad3']

But the problem is that I don't really know how to have this in the Message.content, I've looked it up all over the internet but couldn't find it. The code I have right now:

 else if (message.content.toLowerCase().includes(Blacklist))
  message.delete() && message.author.send("Keep the use of Profanity out of our server!");

How would I go by entering the array into the message's content?

3

3 Answers

1
votes

We could very simply use a forEach() function in order to scroll through every element in the array, and check if the message's content includes that element.

This could be done by typing:

const blacklist = ['bad word 1', 'bad word 2', 'bad word 3']
    blacklist.forEach(word => {
        // 'word' represents an element inside of the array. 
        // Everytime the client is finished walking through an element, the value of 'word' changes to the next one!
        if (message.content.toLowerCase().includes(word)) message.delete() && message.author.send("Keep the use of Profanity out of our server!")
    })
1
votes

you can use Array.prototype.some() to see if message content has atleast one blacklist word

const blacklist = ['bad word 1', 'bad word 2', 'bad word 3']
const containsProfanity =  blacklist.some(word => message.content.includes(word))
if(containsProfanity){
  message.delete() && message.author.send("Keep the use of Profanity out of our server!")
}

1
votes

You could loop through the blacklist and check for each individual word like so:

let Blacklist=["bad","dumb"]
for(let word of Blacklist){
  if(message.content.includes(word)){
    message.delete()
    message.author.send("warning")
    break;
    }
}

I would break out of the loop if there was a bad word in the text so that the user isn't warned multiple times