Kendel Ventonda
Kendel Ventonda

Reputation: 411

How do I get the ID of an user in Discord.py

We're writing a bot for our Discord Server which is supposed to mention the person that starts a specific command. For this I'd need the ID of the user, but can't figure out how to get it.

@bot.command()
async def name():

author = discord.User.id

await bot.say(str(author))

We tried it this way, since the documentation says, the ID of a user is in the class User. But the only thing we get is

<member 'id' of 'User' objects>

In our eyes we got the right param but can't get the ID itself? Do we need to convert it somehow?

Upvotes: 1

Views: 31763

Answers (2)

Patrick Haugh
Patrick Haugh

Reputation: 61052

To get the bot to mention the author of the message in the async branch, you need to refer to that author through the message that invoked the command, ctx.message:

@bot.command(pass_context=True)
async def name(ctx):
    await bot.say("{} is your name".format(ctx.message.author.mention))

To get their id:

@bot.command(pass_context=True)
async def myid(ctx):
    await bot.say("{} is your id".format(ctx.message.author.id))

Upvotes: 5

Andrew S
Andrew S

Reputation: 21

You need to provide a parameter in the function name. Each bot.command needs to have at least one parameter known as the "context". Such as:

async def name(ctx):
   author = ctx.message.author
   user_name = author.name

Upvotes: 1

Related Questions