tontsa28
tontsa28

Reputation: 83

How to get id of tagged person in message (discord.py rewrite)

How can i get the id of a user mentioned in a message? I'm trying to do a command where i need the user id that was mentioned in the command. Here is my code so far:

@commands.command()
async def fight(self, ctx):

    # Send command message
    await ctx.send('You need to tag someone: .fight @user')

    if user.mention in ctx.content:
        if user.mention == self.client.user:
            await ctx.send('You can't fight the bot!')

This does not work. When the user uses the command and tags another user, the bot needs the tagged users id. How can I make this?

Upvotes: 0

Views: 1088

Answers (1)

shark
shark

Reputation: 465

You can use discord.Message.mentions to get a list of members mentioned in the message.

You can then use Members objects to retrieve member ids.

@commands.command()
async def fight(ctx):
    members = ctx.message.mentions
    first_member_id = members[0].id

Upvotes: 2

Related Questions