Zakaria Taleb
Zakaria Taleb

Reputation: 31

Why my on_message gets called after my command?

my code is working. There is only one issue, which is that after calling any command, my on_message get called right after(which leads to some side effects)

async def delete_on_swear(message):
    if not message:
        return
    db = sqlite3.connect('main.sqlite')
    cursor = db.cursor()
    try:
        guild_id = message.guild.id
    except AttributeError:
        return
    cursor.execute(f'SELECT word FROM badwords WHERE guild_id={guild_id}')
    swears = [swear[0] for swear in cursor.fetchall()]
    if not swears:
        return
    if bot.user == message.author:
        return
    for swearword in swears:
        if not message.channel.is_nsfw() and is_substring(message.content.lower(), swearword):
            await message.delete()
            await message.author.create_dm()
            await message.author.dm_channel.send(
                f'Hi {message.author}, you sent a message containing the following word: {swearword}'
            )
            return```

Upvotes: 3

Views: 34

Answers (1)

Seth
Seth

Reputation: 2369

on_message runs after any message is sent in any channel the bot can view. At the top of your on_message, put the following code to prevent the bot from acting on its own messages:

if message.author == bot.user:
    return

Upvotes: 1

Related Questions