I want to build a Discord bot using Discord.py that can detect when users mention it in messages. For example, when someone writes something like “@MyBot#1234 what’s your favorite color?” I need the bot to recognize it was mentioned and respond accordingly.
I was originally planning to make it into a full chatbot but decided to keep it simpler for now. Just need to figure out how to properly catch mentions and trigger responses.
Has anyone worked with mention detection in Discord.py before? What’s the best approach to handle this kind of functionality?
I built something similar last year. Checking message.mentions works perfectly for this. I created a simple conditional inside the on_message event handler that looks for the bot’s user ID in the mentions list. Once it finds a mention, parse the rest of the message content to figure out how to respond. Pro tip I learned the hard way: strip the mention from the message content before processing it. Otherwise your bot tries to interpret the mention tag as part of the actual command or question. Don’t forget to enable the message content intent in your bot settings on Discord’s developer portal - without it, message content will be empty in newer API versions.
yep, it’s really easy! just listen for the on_message event and check if bot.user is in message.mentions. it’s a cool way to catch those mentions. oh, and make sure to add message.author != bot.user so it doesn’t reply to itself, haha!
The on_mention event seems obvious, but I’ve found on_message with mention checking works better. I set up a basic filter to check if the bot appears in the message mentions array, then grab everything after the mention for processing. One thing that surprised me - users mention with nicknames sometimes and usernames other times, so handle both formats. Also, add rate limiting if you’re putting this in busy servers. People spam mentions and you’ll hit Discord’s API limits fast without it.
Everyone’s sharing Discord.py code, but there’s a cleaner approach.
I use Latenode to handle this - it connects Discord webhooks to response triggers without any Python. My workflow catches mentions through Discord’s webhook system, processes messages automatically, and sends responses back through their API. No rate limiting headaches, content intents, or manual mention parsing.
Best part? Easy to expand later. Want keyword-based responses? Just tweak the workflow. Need mention logging? Add another node.
I’ve run this setup for months handling thousands of mentions - way more reliable than maintaining your own bot infrastructure.
don’t overthink it tbh. just use client.user in message.mentions inside your on_message handler and you’re good to go. works every time for me and way simpler than parsing mention strings manualy