0
bot.on('message', message => {
    var str = (message.content.indexOf("swear"))
        if (str != -1){

            var Pottymouth = message.guild.roles.find('name', 'Pottymouth')

            message.channel.send("Woah there, " + message.author + ", you can't use that kind of language here!");
            message.member.addRole(Pottymouth);
                      }

I'm trying to code Jesus as a super cool bot, and one thing he needs to do is make sure that nobody's swearing. So far, this code works for when someone says "swear", but it won't detect them saying "SWEAR" or any other capitalisation. How do I make Jesus not bother with listening to case, and instead focus on the actual content of the message?

1 Answers1

0

Simply lowercase the whole message before checking, or else consider using a case-insensitive regular expression.

Using a regular expression allows you to enforce word boundaries as well, which means you can avoid flagging false positives like "menswear".

if (/\bswear\b/.test(message)) {
    // Do things
}
Platinum Azure
  • 45,269
  • 12
  • 110
  • 134