-1

so im making a bot that will delete any blacklisted words. but the problem is , the bot wont regonize the word if the word contain upper case. For example hi. if i typed hi the bot will delete the text. but if i typed Hi or hI the bot wont regonize the word

bot.on("message", (message) => {
    const swearword =["hi"]
    if( swearword.some(word => message.content.includes(word)) ) {
        message.delete();
        message.reply("That word is blacklisted").then(m => m.delete({timeout: 6000}));
        console.log(message.author + (" said the blacklisted word"))
      }});
new
  • 11
  • 5

3 Answers3

0

You can use a regular expression or lower/uppercase the word before checking.

let swearWord = "damned";
let inputString = "I'll be dAmnEd";

// lowercase the word before check
console.log(inputString.toLowerCase().includes(swearWord));

// regular expression
console.log(/\bdamned\b/i.test(swearWord));
// or
console.log( RegExp(`\\b${swearWord}\\b`, `i`).test(swearWord));
KooiInc
  • 119,216
  • 31
  • 141
  • 177
0

Did you try to use toLowerCase()?

bot.on("message", (message) => {
    const swearword =["hi"]
    if( swearword.some(word => message.content.toLowerCase().includes(word)) ) {
        message.delete();
        message.reply("That word is blacklisted").then(m => m.delete({timeout: 6000}));
        console.log(message.author + (" said the blacklisted word"))
      }});
myscode
  • 75
  • 1
  • 3
  • This is of course a [huge dupe](https://www.google.com/search?q=javascript+case+insensitive+compare+site:stackoverflow.com) – mplungjan Jul 09 '20 at 07:18
0

It is important to understand that, if you are comparing / checking for words from a swearWords collection, you should convert both the words in the same format.

I would suggest looking into toLowerCase() functionality to convert both the comparision operators toLowerCase().

You may update your code as below:

bot.on("message", (message) => {
    const swearword =["hi"]
    
    // storing the incoming message content in lowercase in another variable.
    const incomingMessage = message.content.toLowerCase()

    if( swearword.some(word => incomingMessage.includes( word.toLowerCase() )) ) {
        message.delete();
        message.reply("That word is blacklisted").then(m => m.delete({timeout: 6000}));
        console.log(message.author + (" said the blacklisted word"))
      }});
Dhruv Shah
  • 1,611
  • 1
  • 8
  • 22
  • Converting only the `incoming message content` to lower case might work only if you have all the words in your `swearWords array` in lowercase. But it would be a good practice to compare words in the same `case` – Dhruv Shah Jul 09 '20 at 07:17