Can a Discord bot remove messages containing specific words anywhere in the sentence?

I’m working on a Discord bot and I’m trying to figure out how to delete messages that contain certain words even if they’re in the middle of a sentence. I’ve tried a couple of approaches but they’re not working as expected.\n\nHere’s what I’ve attempted so far:\n\npython\[email protected]\nasync def on_message(message):\n if 'bad_word' in message.content:\n await message.delete()\n await message.channel.send('Message deleted due to inappropriate content.')\n\n\nI also tried using a regular expression:\n\npython\nimport re\n\[email protected]\nasync def on_message(message):\n if re.search(r'\bbad_word\b', message.content, re.IGNORECASE):\n await message.delete()\n await message.channel.send('Message removed for containing prohibited words.')\n\n\nBut I’m getting a warning and it’s not working correctly. Any ideas on how to make this work? I’d really appreciate some help!

I’ve been using Discord bots for a while now, and I can confirm that removing messages with specific words is definitely doable. One thing I’ve found particularly effective is using a combination of regex and a scoring system. Here’s the gist:

Create a dictionary of banned words, each with a ‘severity score’. Use regex to search for these words in messages, accounting for common obfuscation tactics (like replacing ‘a’ with ‘@’). Sum up the severity scores for any matches. If the total exceeds a threshold, delete the message.

This approach allows for more nuanced moderation. It can differentiate between mild swears and truly offensive content, reducing false positives. It’s also more resistant to users trying to circumvent the filter.

Just remember to keep your word list updated and be prepared for the occasional false positive. No system is perfect, but this has worked well for me in practice.

Your approach is on the right track, but there’s room for improvement. Consider creating a list of prohibited words and using a case-insensitive check. Here’s an optimized version:

bad_words = ['word1', 'word2', 'word3']

@bot.event
async def on_message(message):
    if any(word in message.content.lower() for word in bad_words):
        await message.delete()
        await message.channel.send('Message removed due to prohibited content.')

This method is more efficient and flexible. You can easily update the bad_words list as needed. It also catches variations in capitalization. Remember to handle exceptions and consider implementing a warning system before outright deletion.

yea, u can totally do that! just use the in operator to check if any bad word is in the message. like this:

bad_words = [‘word1’, ‘word2’, ‘word3’]
if any(word in message.content.lower() for word in bad_words):
await message.delete()

this’ll catch words even in the middle of sentences. ez pz!