0

I'm trying to figure out how to get a specific word from an entire message. Like using a profanity filter.

    for (let x = 0; x < profanities.length; x++) {
    if (message.content.toUpperCase().includes(profanities[x].toUpperCase())) {
        message.channel.send('Oooooooh you said a bad word!');
        client.channels.get('484375912389935126').send(`Message was deleted due to use of a blocked word:\n\n"${message.content}"`);
        message.delete();
        return;
    }
}

Now this works except for the fact that if a word was said inside another word it finds it too because of the .includes like if I were to block "bum" and someone says "bumble" it would delete "bumble" as well. That's all well and good for profanity filters but I wanted to do a fun one for a member:

    const words = message.content.toLowerCase();
    if (words.includes('bum')) {
        setTimeout(function() {
          message.channel.send('Are we talking about <@memberID>?!');
        }, 1500);
    }

I used "memberID" instead of the real ID. But this finds "bum" in "bumble" but I only want it to find "bum" as a separate word within the message. Like "This bum is weird" or something like that.

Arnav Borborah
  • 11,357
  • 8
  • 43
  • 88
QuazArxx
  • 150
  • 2
  • 15
  • 2
    Does this answer your question? [JavaScript/jQuery - How to check if a string contain specific words](https://stackoverflow.com/questions/5388429/javascript-jquery-how-to-check-if-a-string-contain-specific-words) – SuperStormer Apr 05 '20 at 21:53
  • @SuperStormer Yup, specifically [this answer](https://stackoverflow.com/a/21081760/6525260) should solve the issue (Most of the other answers are blatantly wrong). – Arnav Borborah Apr 05 '20 at 21:58
  • @ArnavBorborah the accepted answer is the same thing – SuperStormer Apr 05 '20 at 22:00
  • @SuperStormer the answer I linked is more generalized. – Arnav Borborah Apr 05 '20 at 22:01
  • While these are basically what I wanted, I'm not really sure how they work since I'm a bit new to js. I probably should've mentioned that earlier – QuazArxx Apr 05 '20 at 22:04

1 Answers1

0

Like the other answers say, use regex:

if (/\bbum\b/i.test(message.content)) {
  setTimeout(function() {
    message.channel.send('Are we talking about <@memberID>?!');
  }, 1500)
}

The \b checks for a word boundary and the i makes it case-insensitive.

Lauren Yim
  • 12,700
  • 2
  • 32
  • 59