Ulas GokDenîz Ulas GokDenîz - 2 months ago 6
Node.js Question

Using includes() to get a search value from a var

The below code is my attempt to gain the searchvalue from the variable "BannedWords", it doesn't work. This code uses the discord.js library. Here is it's documentation.

var BannedWords = [
"TEST1",
"TEST2",
"TEST3",
"TEST4"
];

if(msg.content.includes(BannedWords[msg.content.toUpperCase()])) {
msg.reply("Successful");
}

Answer

You can achieve this with a regular expression:

if (new RegExp(BannedWords.join("|"), "i").test(msg.content)) {
    msg.reply("Successful");
}

Or using some:

if (BannedWords.some((v) => msg.content.toUpperCase().indexOf(v) >= 0)) {
    msg.reply("Successful");
}

Note: this is adapted to your (case insensitive) scenario from this very good answer. Refer to it for a better understanding.

Comments