So this is the current code I have for the word filter. I have an array of the censored words in a JSON file. Whenever I try to run the bot and type in a swear word I get no response and no error msgs in Powershell. I'm quite a beginner so excuse any mistakes and excuse the train wreck of a code this is. Just trying to learn.
const { Client, Collection, Intents } = require('discord.js');
const { wordFilter} = require("./filter.json");
const {clientId, guildId, token, prefix} = require('./config.json');
const Discord = require('discord.js')
const client = new Client({ intents: [
Intents.FLAGS.GUILDS,
Intents.FLAGS.GUILD_MESSAGES,
Intents.FLAGS.GUILD_MEMBERS,
Intents.FLAGS.GUILD_BANS,
Intents.FLAGS.GUILD_MESSAGE_REACTIONS,
Intents.FLAGS.GUILD_MESSAGES
] });
const fs = require('fs');
const giveMeaJoke = require('discord-jokes');
client.commands = new Discord.Collection();
const commandFiles = fs.readdirSync("./Commands/").filter(file => file.endsWith(".js"));
for(const file of commandFiles){
const command = require(`./Commands/${file}`);
client.commands.set(command.name, command);
}
client.once('ready', () => {
console.log('Bot is now available!');
});
client.on('messageCreate', message => {
const command = message.content;
//Profanity Filter
if(message.author.id = "909209531811242044"){
return
}
let foundinText = true;
for(const word in wordFilter){
if(message.content.toLowerCase().includes(wordFilter[word].toLowerCase())) foundinText = true;
}
if(foundinText){
message.delete();
message.reply('Be polite, no swearing please');
}
// End of profanity Filter
if(message.content == 'Hello')message.reply("Hello!");
if(command == prefix + 'Ping'){
client.commands.get('Ping').execute(message, command);
}
if(!message.content.startsWith(prefix)) return;
if(message.content.toLowerCase()== prefix + 'hello'){
message.reply('Hello!');
}if(command == prefix + 'Website'){
client.commands.get('Website').execute(message, command);
}
if(message.content.toLowerCase()== prefix + 'joke'){
giveMeaJoke.getRandomDadJoke(function(joke){
message.reply(joke);
});
}
});
client.login(token);
Try using Array#some() to reduce the number of steps in your procedure. Ensure wordFilter is a string array.
const hasProfanity = wordFilter
.some(word => message.content.toLowerCase().includes(word.toLowerCase()));
if (hasProfanity && message.author.id !== '909209531811242044') {
message.delete();
message.reply('Be polite, no swearing please');
}
This is one of many ways to check for words, but it has it's issues. For example if the word "ass" is a keyword, the word "assure" would checkout and hasProfanity would equal true
A better method would be to first split message.content by a space into an array and check each individual word.
const words = message.content.split(/ +/);
const hasProfanity = words
.some(word => wordFilter.includes(word.toLowerCase());
// Remaining Code
It's best if the words in wordFilter are all lowercased, this way you don't have to call .toLowerCase() on them on each iteration