The issue is message.content.includes == 'bannedWord1' ||'bannedWord2' || 'bannedWord3' || 'bannedWord4'
really means (message.content.includes == 'bannedWord1') ||'bannedWord2' || 'bannedWord3' || 'bannedWord4'
. Non-empty strings are truthy so this is always true.
Also, I assume message.content
is a string. So you need to handle things like 'assumptions' which contains ass
but isn't a bad word.
Demo solution:
var badWords = [
'bannedWord1',
'bannedWord2',
'bannedWord3',
'bannedWord4'
];
var contents = [
'This is a test.',
'bannedWord1 tester.',
'bannedWord2!',
'OkaywordthatcontainsbannedWord1.'
];
var allowed = contents.filter(content => {
var words = content.toLowerCase().trim().match(/\w+|\s+|[^\s\w]+/g);
return !words.find(word => {
return badWords.includes(word);
});
});
console.log(allowed);
Use in your case:
var badWords = [
'bannedWord1',
'bannedWord2',
'bannedWord3',
'bannedWord4'
];
bot.on('message', message => {
var words = message.content.toLowerCase().trim().match(/\w+|\s+|[^\s\w]+/g);
var containsBadWord = words.some(word => {
return badWords.includes(word);
});
if (containsBadWord) {
message.delete(1);
message.channel.send("That word's not appropriate!");
}
});
Regex explanation from regex101.com
- 1st Alternative
\w+
\w+
matches any word character (equal to [a-zA-Z0-9_]
)
+
Quantifier — Matches between one and unlimited times, as many times as possible, giving back as needed (greedy)
- 2nd Alternative
\S+
\S+
matches any whitespace character (equal to [\r\n\t\f\v ]
)
+
Quantifier — Matches between one and unlimited times, as many times as possible, giving back as needed (greedy)
- 3rd Alternative
[^\s\w]+
- Match a single character not present in the list below
[^\s\w]+
+
Quantifier — Matches between one and unlimited times, as many times as possible, giving back as needed (greedy) \s matches any whitespace character (equal to [\r\n\t\f\v]
)