Based on the comments, it looks like you are trying to edit message.content
to remove bad words. You can use an array of bad words in combination with something like Array.prototype.reduce
and RegExp
to remove each bad word from message.content
and use message.edit
to replace the message with the "clean" version:
const badWords = ['test2', 'test4'];
client.on('message', message => {
// Create a message with all bad words replaced with empty string
const clean = badWords.reduce((acc, badWord) => {
const regex = new RegExp(`\\b${badWord}\\b`, 'ig');
return acc.replace(regex, '');
}, message.content);
// Edit the message to be the clean version
messsage.edit(clean);
}
const badWords = ['TeSt2', 'bass'];
const message = {
content: 'foo bar bass TeSt2 baz bigbass test4'
};
// Check if bad words are contained in string
const shouldDelete = badWords.some(badWord => message.content.toLowerCase().includes(badWord))
console.log(shouldDelete);
// Remove bad word exact matches
const clean = badWords.reduce((acc, badWord) => {
const regex = new RegExp(`\\b${badWord}\\b`, 'ig');
return acc.replace(regex, '');
}, message.content)
console.log(clean);
Hopefully that helps!