Can a Discord bot censor profanity in messages? (JavaScript)

I’m working on a Discord bot for my server and want it to filter out bad language. Is it possible to make the bot replace swear words with a custom message? For example, if someone writes ‘You are an idiot’, the bot would change it to ‘You are a [Nice try, but no swearing allowed]’.

If that’s not doable, could the bot delete the message and post a warning instead? Like ‘@user Please watch your language!’

I’ve given the bot all the permissions, including message deletion. But I’m not sure how to implement this feature. Here’s a basic setup I have so far:

const Discord = require('discord.js');
const bot = new Discord.Client();

bot.on('ready', () => {
  console.log('Bot is up and running!');
});

bot.on('message', msg => {
  if (msg.content === '!hello') {
    msg.reply('Hi there!');
  }
});

bot.login('YOUR_BOT_TOKEN_HERE');

Any ideas on how to add word filtering to this code? Thanks for your help!

Yes, it’s definitely possible to implement profanity filtering in a Discord bot using JavaScript. I have used a similar approach on my own server by maintaining a collection of banned words and checking each incoming message to detect any prohibited content. When an offending word is identified, you can either replace it with a custom message or delete the message entirely. Regular expressions help with matching various word forms and avoiding false positives. Testing thoroughly and adjusting your filter are important steps to improve accuracy over time.

I have implemented a similar profanity filter in my Discord server and found that it is quite manageable to censor inappropriate language. In my experience, you can approach this by maintaining a set of banned words and checking every incoming message against that set. When a match is found, you can choose to replace the offending word using regular expressions or simply delete the message by invoking the delete() method. It is important to handle edge cases such as differences in case and inserted special characters, as well as avoiding false positives where non-offensive words might trigger the filter. This method, while straightforward, provides a solid foundation on which you can build and refine your censorship features.

yea u can do that with discord.js. jst add a list of bad words and check messages against it. use regex to replace or delete. like this:

bot.on(‘message’, msg => {
if (badWords.some(word => msg.content.includes(word))) {
msg.delete();
msg.channel.send(‘watch ur language @’ + msg.author);
}
});

works pretty good for me