Reputation: 13
Hi there i want to create a discord bot that delete messages that contains bad words. I found only a function startswith, but i want to delete message also if it contains bad words.
Here is my code:
@bot.event
async def on_message(message):
args = message.content.split(" ")[1:]
if message.content.startswith("bad_word"):
await message.delete()
await bot.send_message(message.channel, " ".join(args))
Thank you for any help
Upvotes: 1
Views: 5447
Reputation: 21
If you need to sort in several bad words you can do like this
@bot.event
async def on_message(message):
args = message.content.split(" ")[1:]
bad_words = ("bad_word1", "bad_word2", "bad_word3"...)
if any(bad_word in content for bad_word in bad_words):
await message.delete()
await bot.send_message(message.channel, " ".join(args))
I also advise to convert the content to lower case to avoid the possibility of bypassing the system with a capital letter.
any(bad_word in content.lower() for bad_word in bad_words)
Upvotes: 2