sfddfgsdff
sfddfgsdff

Reputation: 13

Discord Bot on Python: if message contains

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

Answers (2)

NiniKo
NiniKo

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

stfwn
stfwn

Reputation: 362

The documentation states message.content is a string, so you are probably looking for:

foo = 'Here is an example string that contains bar.'
if 'bar' in foo:
    print('bar is in foo')

Upvotes: 2

Related Questions