How can I make on_message event only read messages from one channel, in discord.py

27 Views Asked by At

In my code, I have this on_message event for my discord bot, I have tried making it only detect messages from one channel it works but not the way I wanted.

Like now if I type anything in any channel it prints that text in console, and seems like it overwrites normal commands with ! prefix, because commands dont even work when this event is in the code.

Is there a way to make it really only read messages from one channel rather than checking messages all across the server?

Here is the event:

@bot.event
async def on_message(message):
    print("This print shows in console because this event reads every channels messages.")
    if message.author == bot.user:
        return

    if message.channel.id != 1221858607000326186:
        return

    if 'vouch' in message.content.lower() and message.mentions:
        check_db_connection()
        mentioned_user = message.mentions[0]

        cursor.execute("SELECT * FROM midman_information WHERE midman_user LIKE %s", (f"%<@{mentioned_user.id}>%",))
        result = cursor.fetchone()

        if result:
            vouch_count = result[2] + 1
            cursor.execute("UPDATE midman_information SET vouch_count = %s WHERE midman_user = %s", (vouch_count, result[1]))
            db_config.commit()

            log_channel = bot.get_channel(1221858628173041674)
            embed = discord.Embed(title="Vouch Logged",
                                  description=f"{message.author.mention} has given a vouch to {mentioned_user.mention} | `{vouch_count}` vouches",
                                  color=0x2b2d31)
            embed.set_thumbnail(url=thumbnail)
            embed.set_footer(text="Lunar Services")
            await log_channel.send(embed=embed)
        else:
            await message.delete()

    await bot.process_commands(message)
1

There are 1 best solutions below

1
pabludo8 On

The thing here is that

@bot.event async def on_message(message):

is just a wrapper for discord's api gateway message create event which is always sent by discord when a message in any of the servers that the bot is in is sent, so the only way to only read messages from one channel is what you have done:

 if message.channel.id != 1221858607000326186:
    return

So, no, there's no way other than check the message channel id.