Search code examples
pythondiscorddiscord.pybots

How to check id of a tagged user with discord.py


I have a command defined with my discord bot that responds to a prompt like shown:

if message.content.startswith("doge roast") : print("<the roast>")

This responds in all cases, but I would like to check whether I have been tagged in the post and respond differently in said case.

I've tried looking at some solutions, but they use command contexts. I have simply defined my command in on_message.

So, how would I find the id of the user that is tagged in the message?


Solution

  • Explanation

    You can access the mentions in a discord.Message using the mentions property.

    A list is stored in the mentions property, so you can access the tagged discord.Member using message.mentions[0].

    Code

    if message.content.startswith("doge roast"):
        if message.mentions[0].id == YOUR_ID:
            print("something else")
        else:
            print("<the roast>")
    
    

    Note: if using discord.ext.commands.Bot, you can replace YOUR_ID with bot.owner_id

    Reference

    Message.mentions

    Member.id