-1

How to delete the word that does not contain alphanumeric characters?

but user can bypass this with bann.edWord1 or similar...

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!");
  }
});
jiwopene
  • 3,077
  • 17
  • 30
Tanki
  • 13
  • 2
  • on https://regexr.com website /[^\s\w]/g seems to work.... then i'l test, nothing happen...On discord with bot => writing test => message deleted, writing te.st nothing... – Tanki Feb 19 '19 at 09:09
  • You'll find that this is a never ending arms race. People will always get around these sorts of filters, in creative ways. See https://blog.codinghorror.com/obscenity-filters-bad-idea-or-incredibly-intercoursing-bad-idea/. – ceejayoz Feb 25 '19 at 15:01

1 Answers1

0

You can use the solution suggested here to remove non-alphanumeric chars from your string and check for "bad words" afterwards.

Example:

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

bot.on('message', message => {
  var textOnly = message.content.replace(/\W/g, '');

  for (i=0; i<badWords.length; i++) {
    if (textOnly.indexOf(badWords[i].replace(/\W/g, '')) !== -1) {
      message.delete(1);
      message.channel.send("That word's not appropriate!");
      break;
    }
  }
});

// bannedWord1 fill be filtered
// ban.ne.d Word 1 will be filtered, too
devios1
  • 36,899
  • 45
  • 162
  • 260
Ishidres
  • 135
  • 2
  • 11
  • 1
    You're awesome, thanks very much, it work perfectly :) <3 – Tanki Feb 25 '19 at 14:01
  • Hello, i don't know what happen but now, all message was removed... https://hastebin.com/okorahiral.coffeescript – Tanki Apr 17 '19 at 21:05
  • Using the method above all messages *including* a specific word *somewhere* will be deleted. For example, if you block the word "**nux**" and someone sends the message "I use Li**nux**" the word "Linux" contains "**nux**". I assume you added a word to your blacklist (called "NoSeek" in the link above) which acts the same way. Keep in mind that all kind of non-alphabetic characters (including spaces) are removed: "I use L i **n u x**" would also get blocked. – Ishidres Apr 18 '19 at 22:11
  • 1
    ```js var NoSeek = [ "solo", "duo", "trio", "cherche", "seek", "recrute", "rct", "cherche", ]; ``` ive missed... if i write anything, its deleted, so i need to learn more so... :( Thanks for the tip – Tanki Apr 19 '19 at 10:20