1

I am making a chatbot for a website I use. I am in the process of adding a banned words list. I already have it working to where certain users are not allowed to post link. Here is the code for that.

if (text.indexOf("http://") > -1 || text.indexOf("https://") > -1) {
      if (isOwner || isStaff || user === "user") {}
      else if (!isSub) {
        moderation.timeout(name, 5);
        channel.sendMessage(name + ", only subs can post links.");
}

So what I did was pretty much duplicated the above but without the user restrictions and added new words. Here is the code.

if (text.indexOf("http://") > -1 || text.indexOf("https://") > -1) {
      if (isOwner || isStaff || user === "user") {}
      else if (!isSub) {
        moderation.timeout(name, 5);
        channel.sendMessage(name + ", only subs can post links.");
  }
}
 if (text.indexOf("word1") > -1 || text.indexOf("word2") > -1 || text.indexOf("word3") > -1) {
      moderation.timeout(name, 5);
      channel.sendMessage(name + ", please don't be rude.");
}

The link removal works perfectly fine. I don't like that indexof is case sensitive for the word ban list however. Is there a better way of doing this with a function that is not case sensitive?

Layne Mayo
  • 39
  • 10
  • You seem to be missing a `}` in your first block, so your second block has ended up inside it. – Niet the Dark Absol Jun 18 '16 at 11:34
  • I literally slapped myself in the face when I saw that. Thank you very much. Is there a function you could recommend that isn't case sensitive by the way? or is this about as good as I am going to get? – Layne Mayo Jun 18 '16 at 11:44
  • Could use regex - `if( text.match(/word1|word2|word3/i))` but you're getting into a losing battle. There will always be ways around what you filter. – Niet the Dark Absol Jun 18 '16 at 11:49
  • Thank you, I will try that. Also, I know there will be ways around it, but I have never had any trolls in the chat room. If someone says the word, and then tries to get around the filter, I will just ban them. I am a live streamer and this is for my live stream chat, so it will only need to be monitored when I am live :) it just helps keep me from having to do most of the dirty work. – Layne Mayo Jun 18 '16 at 11:52
  • Ah, cool. Yeah, I've been there XD Honestly nothing beats a human moderator who has your back. – Niet the Dark Absol Jun 18 '16 at 12:12
  • I tried what you gave me there and it works perfectly! Thank you! Very simple and easy. Yes I agree that a human mod would help lol, but I am going to have a bot for other things anyway, so why not at least make mine and the mods job a bit easier right? hahaha plus it gives me a chance to learn something new and I am having fun with it ^_^ – Layne Mayo Jun 18 '16 at 12:17

2 Answers2

2

You need to normalize the case of both strings, then do indexOf.

function indexOfCaseInsenstive(a, b) {
  a = a.toLowerCase();
  b = b.toLowerCase();

  return a.indexOf(b);
}
  • I would like to make sure that I am understanding the logic correctly. so the first line makes it read the words even if they are not cased correctly and then the 2nd line makes them appear all lower case in the console? Also, thank you for the edits :) – Layne Mayo Jun 18 '16 at 12:05
  • `toLowerCase` returns a new string that is the same as the argument but in lowercase. So, `a = a.toLowerCase()`, creates a new lowercase string and stores it in `a`. (JavaScript variables are [passed by value](http://stackoverflow.com/questions/7744611/pass-variables-by-reference-in-javascript/7744623#7744623) not reference so the original strings that you passed to the function are not altered; `a` and `b` are essentially local to the function). The same is done with `b`. This means `indexOf` is comparing two lowercase strings, effectively ignoring the case. – Useless Code Jun 18 '16 at 12:33
1

Make a list of words, create regexp for it and test the regexp against incoming messages:

const forbiddenWords = ['http://', 'word1', 'word2'];
const regexp = new RegExp(forbiddenWords.join('|'), 'i');

if(regexp.test(message)) {
    // you have a forbidden message.
}
Azamantes
  • 1,435
  • 10
  • 15