Reputation: 490
I was trying to make a bad words filter with my bot in discord.js v13 and node.js v16.
When I send a 'word1' message, It works correctly and deletes the message. but when I send for example: "Hey word1", It does not delete the message.
What I've tried:
const args = message.content.split(/ +/);
if ((message.guild.id = 'GUILD_ID')) {
const bad = ['word1', 'word2'];
if (bad.includes(args[0].join(' '))) {
message.delete();
} else {
return;
}
}
Upvotes: 0
Views: 769
Reputation: 23161
It's because you check if the array contains the string "Hello word1"
.
You can use Array#some()
with a callback function to check if the string contains any of those words in the array:
function containsBadWords(str) {
const badWords = ['word1', 'word2']
if (badWords.some(word => str.toLowerCase().includes(word)))
return 'String contains bad words'
else
return 'No bad words found'
}
console.log(containsBadWords('word1'))
console.log(containsBadWords('Some other text that includes word2 here'))
console.log(containsBadWords('No bad words here'))
// make sure you don't use single '=' here!
if (message.guild.id === 'GUILD_ID') {
const bad = ['word1', 'word2']
if (bad.some(word => message.content.includes(word)))
message.delete()
else
return
}
Upvotes: 1