0
votes

I have the following code running in my bot, as a command that deletes cuss words. However, it seems to make the bot delete any message that's sent no matter what! HELP!

bot.on('message', message => {
    if (message.content.includes == 'bannedWord1' ||'bannedWord2' || 'bannedWord3' || 'bannedWord4') {
       message.delete(1);
       message.channel.send("That word's not appropriate!");
    }
 });
3
Thanks, Max Vollmer. I didn't notice I hadn't changed the code.SuperNunb

3 Answers

3
votes

The issue is message.content.includes == 'bannedWord1' ||'bannedWord2' || 'bannedWord3' || 'bannedWord4' really means (message.content.includes == 'bannedWord1') ||'bannedWord2' || 'bannedWord3' || 'bannedWord4'. Non-empty strings are truthy so this is always true.

Also, I assume message.content is a string. So you need to handle things like 'assumptions' which contains ass but isn't a bad word.

Demo solution:

var badWords = [
  'bannedWord1',
  'bannedWord2',
  'bannedWord3',
  'bannedWord4'
];

var contents = [
  'This is a test.',
  'bannedWord1 tester.',
  'bannedWord2!',
  'OkaywordthatcontainsbannedWord1.'
];

var allowed = contents.filter(content => {
  var words = content.toLowerCase().trim().match(/\w+|\s+|[^\s\w]+/g);
  return !words.find(word => {
    return badWords.includes(word);
  });
});

console.log(allowed);

Use in your case:

var badWords = [
  'bannedWord1',
  'bannedWord2',
  'bannedWord3',
  'bannedWord4'
];

bot.on('message', message => {
  var words = message.content.toLowerCase().trim().match(/\w+|\s+|[^\s\w]+/g);
  var containsBadWord = words.some(word => {
    return badWords.includes(word);
  });
  if (containsBadWord) {
    message.delete(1);
    message.channel.send("That word's not appropriate!");
  }
});

Regex explanation from regex101.com

  1. 1st Alternative \w+
    • \w+ matches any word character (equal to [a-zA-Z0-9_])
    • + Quantifier — Matches between one and unlimited times, as many times as possible, giving back as needed (greedy)
  2. 2nd Alternative \S+
    • \S+ matches any whitespace character (equal to [\r\n\t\f\v ])
    • + Quantifier — Matches between one and unlimited times, as many times as possible, giving back as needed (greedy)
  3. 3rd Alternative [^\s\w]+
    • Match a single character not present in the list below [^\s\w]+
    • + Quantifier — Matches between one and unlimited times, as many times as possible, giving back as needed (greedy) \s matches any whitespace character (equal to [\r\n\t\f\v])
1
votes

Because you're trying to compare pointer to function with expression string OR string OR string which always is true so the if executed for each new message

0
votes

This is what I have.. feel free to mess around with it!

bot.on('message', async message => {


  //1 blacklisted words
  let blacklisted = ['bannedWord1','bannedWord2,','bannedWord3','bannedWord4','bannedWord5','bannedWord6','bannedWord7','bannedWord8','bannedWord9','bannedWord10','bannedWord11'] //words

  //2 looking for words
  let foundInText = false;
  for (var i in blacklisted) { // loops through the blacklisted list
    if (message.content.toLowerCase().includes(blacklisted[i].toLowerCase())) foundInText = true;
  }
  // checks casesensitive words

  //3 deletes and send message
    if (foundInText) {
      message.delete();
      message.channel.send('Hey! That word is not allowed!! :rage:').then(msg => msg.delete(5000));
  }
});