-1

So I'm new to JavaScript and I'm wondering how I would blacklist multiple words in one line of code?

client.on('message', message => {
    if(message.content === "test, test2, test3")}
        message.delete()

But the bot will only delete all of those keywords together and not separately. I heard that I need an Array but how would I do this?

Alexander Staroselsky
  • 37,209
  • 15
  • 79
  • 91
  • Tutorial about arrays in JavaScript: https://javascript.info/array – Jesper Jul 30 '20 at 22:10
  • 1
    Does this answer your question? [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) – Heretic Monkey Jul 30 '20 at 22:10

3 Answers3

1
client.on('message', message => {
    if( ["test", "test2", "test3"].includes(message.content) ){
        message.delete()
    }
}
        
Raphael Serota
  • 2,157
  • 10
  • 17
  • This is checking if the string of combined bad words string(s) contains all of `message.content`. Isn't the OP asking to check if instead `message.content` contains the bad words string(s)? For example if the `message.content` was `'foo bar test, test2, test3 bar foo'`, as in containing the bad words, this would not delete the message. – Alexander Staroselsky Jul 30 '20 at 22:21
  • @AlexanderStaroselsky yeah, you're right. It's fixed now. – Raphael Serota Aug 02 '20 at 19:37
1

Based on the comments, it looks like you are trying to edit message.content to remove bad words. You can use an array of bad words in combination with something like Array.prototype.reduce and RegExp to remove each bad word from message.content and use message.edit to replace the message with the "clean" version:

const badWords = ['test2', 'test4'];

client.on('message', message => {
  // Create a message with all bad words replaced with empty string
  const clean = badWords.reduce((acc, badWord) => {
    const regex = new RegExp(`\\b${badWord}\\b`, 'ig');
    return acc.replace(regex, '');
  }, message.content);
  // Edit the message to be the clean version
  messsage.edit(clean);
}

const badWords = ['TeSt2', 'bass'];

const message = {
  content: 'foo bar bass TeSt2 baz bigbass test4'
};

// Check if bad words are contained in string
const shouldDelete = badWords.some(badWord => message.content.toLowerCase().includes(badWord))
console.log(shouldDelete);

// Remove bad word exact matches
const clean = badWords.reduce((acc, badWord) => {
 const regex = new RegExp(`\\b${badWord}\\b`, 'ig');
 return acc.replace(regex, '');
}, message.content)
console.log(clean);

Hopefully that helps!

Alexander Staroselsky
  • 37,209
  • 15
  • 79
  • 91
  • @Myles124 great to hear. Be sure to mark the answer if helped resolve your issue. Happy coding! – Alexander Staroselsky Jul 30 '20 at 22:25
  • Oh actually, I am wondering if I can make it so it doesn't delete words that include the key words but just the delete the keys words themselves. Does that make sense? I want lets say "ass" to be deleted but of course I don't want "class" to be deleted. – Myles124 Jul 30 '20 at 22:34
  • @Myles124 That intention was not clear in your question. I updated the answer to show how at a basic level you can remove each bad word from `message.content` and the replace `message.content` using `message.edit()` – Alexander Staroselsky Jul 30 '20 at 23:02
0

You can use array.prototype.some method.

Discord solution

const blackListWord = ['lol', 'word2']
client.on('message', message => {
        
    let isBlacklisted = blackListWord.some(checkInclude)
    if (isBlacklisted) {
        message.delete()
    }
});

function checkInclude(element, index, array) {
    return message.content.toUpperCase().includes(element.toUpperCase());
}

Online snippet

      const message = {
       content: 'word1 is gonna word2'
  }
    const blackListWord = ['lol', 'word2']
  
  let isBlacklisted = blackListWord.some(checkInclude)
  console.log(isBlacklisted)
  
function checkInclude(element, index, array) {
  return message.content.toUpperCase().includes(element.toUpperCase());
}
Cipher
  • 2,702
  • 1
  • 6
  • 17