MCK
MCK

Reputation: 645

Give my javascript array access to includes()

Before everything, I have been making a word filter program for my discord.js bot so excuse me for the bad words!

Since you can't add extra parameters in includes() I decided to make a var line:

var filteredwords = ['asshole', 'fuck']

But now I want to place these words (further more will be added) in the following code line:

if (message.content.includes('asshole'));

So instead of 'asshole' I want to place the array? How can I do that? Since I'm a beginner in JS I could not understand the other topics with a similar question. It would be fine if you explain it in noob language. :)

If useful, this is my full code:

const Discord = require('discord.js');
const client = new Discord.Client();
var filteredwords = ['asshole', 'fuck']

function commandIs(str, msg) {
    return msg.content.toLowerCase().startsWith('--' + str);
}

client.on('ready', () => {
    console.log('The bot is started succesfully')
});

client.on('message', message => {
    if (commandIs('trump', message)) {
        message.reply('He is the president of the United States of America!');
    }
    if (commandIs('putin', message)) {
        message.reply('He is the president of Russia!');
    }
    if (commandIs('spacetaco', message)) {
        message.reply('He is the first user who joined Arcanews!');
    }
    if (message.content.includes('asshole')); {
        message.reply('Do not swear please');
        message.delete();
        var colors = require('colors/safe');
        console.log(colors.red(`The following message got deleted: 
${message.content}`));
    }
    });

Upvotes: 0

Views: 664

Answers (3)

Mayank Raj
Mayank Raj

Reputation: 1624

You can use regex matching here. A much faster implementation

var filteredwords = ['bad-word', 'another-bad-word']
var message = 'Hello this string has a bad-word in it'; // Use your messsage.content here

// In a regex - '\b' start or end of a word
// " .join('\\b|\\b') " - gives me a regex string with each of the values from filteredwords array concatinated to for a 'OR' expression
// Here is th regex exp taht is generated `\basshole\b|\bfuck\b`
if( (new RegExp( '\\b' + filteredwords.join('\\b|\\b') + '\\b') ).test(message) ){
    alert('match'); // Here the word were matched
}else{
    alert('no match'); // Wooho, a safe message
}

Pro tip : RegEx solution stands out in a way that you do the match that are case-insensitive and for bad-words that appear as a part of another word eg 'dambad-Word' would give a match for bad-word

EDIT: Updating answer to the full-code posted by const Discord = require('discord.js'); const client = new Discord.Client(); var filteredwords = ['asshole', 'fuck']

function commandIs(str, msg) {
  return msg.content.toLowerCase().startsWith('--' + str);
}

client.on('ready', () => {
  console.log('The bot is started succesfully')
});

client.on('message', message => {
  if (commandIs('trump', message)) {
    message.reply('He is the president of the United States of America!');
  }
  if (commandIs('putin', message)) {
    message.reply('He is the president of Russia!');
  }
  if (commandIs('spacetaco', message)) {
    message.reply('He is the first user who joined Arcanews!');
  }


  if ( (new RegExp('\\b' + filteredwords.join('\\b|\\b') + '\\b')).test(message.content ) ) {
    message.reply('Do not swear please');
    message.delete();
    var colors = require('colors/safe');
    console.log(colors.red(`The following message got deleted:${message.content}`));
  }

});

If you want to do the matching for not whole words like say curseWord should be detected in the sentence hello there youcurseword (case insensitive as well), you can replace the last IF condition with :

// We can get rid of '\b' to make the search not limited to whole-word matching
if ((new RegExp(filteredwords.join('|'))).test(message.content))

// We can use 'i' flag to make the matching case insensitive
if ((new RegExp(filteredwords.join('|') , 'i')).test(message.content))

Upvotes: 0

Wei
Wei

Reputation: 372

Because of the "includes" method you used, I think the type of "message.content" is an array.

Therefore, the problem can be regard as compare two arrays. You can simply apply two loops for checking, or you can use reduce method for checking.

var messageA = {
  content: "Here is a asshole in it.".split(" "),
};

var messageB = {
  content: "Here is a hole in it.".split(" "),
};

var filteredwords = ['asshole', 'fuck'];

var isValidA = messageA.content.reduce(function(pre, cur) {
  if (pre) return !!filteredwords.indexOf(cur);
  return pre;
}, true);

var isValidB = messageB.content.reduce(function(pre, cur) {
  if (pre) return !!filteredwords.indexOf(cur);
  return pre;
}, true);

console.log(isValidA); // false
console.log(isValidB); // true

Upvotes: 0

Jonas Wilms
Jonas Wilms

Reputation: 138267

if(filterwords.some(badword=>message.content.includes(badword))){
  alert("BAD!");
}

Array.prototype.some iterates over the array and returns true if one of the given function called with the array elem as argument is true, therefore if it contains at least one bad word...

Upvotes: 1

Related Questions