I'm adding an auto-mod for swearing, I want the bot to look for any word from the list in config.json named "badwords" and delete it, which works, but if the member adds " "(space) or "_" or anything like that, it bypasses the check, so I added .replace(//s/g,'') which works for space, but for dash and other stuff, I wanted to use a list in config.json, but I can't seem to get the bot to run thru the list, there are no errors, so how can I fix this problem?
here is my code:
const config = require('../../config');
module.exports = async (client, message) => {
if (!message.guild) return;
if(!message.author.bot) {
var badwords = config.badwords;
var thingstoremove = config.thingstoremove;
for (var i = 0; i < badwords.length; i++) {
if (message.content.toLowerCase().replace(thingstoremove[8],'').includes(badwords[i])) {
message.delete()
message.reply("Watch your language!").then(m => m.delete({timeout: 10000}))
break;
}
}
}
}
config.json:
{
"badwords": ["test1", "test2", "test3", "test4", "test5"],
"thingstoremove": ["-", "_", ".", ",", "`", "~", "@", "#"]
}
Thanks.
Use this simple one-liner to get the fully replaced string
let newStr = thingstoremove.reduce((a, c) => a.replaceAll(c, ""), message.content)
And then a simple check with this:
if (badwords.some(b => newStr.includes(b))) {
message.delete()
message.reply("Watch your language!").then(m => m.delete({ timeout: 10000 }))
}
The issue is:
thingstoremove[8].replace — instead of a single element.Therefore, you should create a set of characters from the array on the regex, to capture any of the characters, and replace them:
const regex = new RegExp(`[${thingstoremove.join('')}]`, 'g')
And then use the regex on .replace:
if (message.content.toLowerCase().replace(regex, '').includes(badwords[i]))
Resulting code:
const config = require('../../config');
module.exports = async (client, message) => {
if (!message.guild) return;
if (!message.author.bot) {
var badwords = config.badwords;
var thingstoremove = config.thingstoremove;
const regex = new RegExp(`[${thingstoremove.join('')}]`, 'g')
console.log(regex)
for (var i = 0; i < badwords.length; i++) {
if (message.content.toLowerCase().replace(regex, '').includes(badwords[i])) {
message.delete()
message.reply("Watch your language!").then(m => m.delete({timeout: 10000}))
break;
}
}
}
}