discord.py

Detect Discord bot pings in Python


How can I detect if a discord message contains any mention that would ping the bot?

In another thread I saw that you can use client.user.mentioned_in(message), but this doesn't seem to recognize role pings. Is there any catch-all way to detect any mentions that ping the bot?


Solution

  • You can use the message.mentions and message.role_mentions attributes to check:

    me = message.guild.me # bot's member
    if me in message.mentions or any(role in message.role_mentions for role in me.roles):
        ... # bot has been mentioned
    

    PS: Remember to have guild, member and message content intents enabled.