1

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?

Kendel Ventonda
  • 411
  • 3
  • 9
  • 22
  • Duplicate: https://stackoverflow.com/questions/53026087/how-to-get-id-of-a-mentioned-user-discord-py/53026278#53026278 – Patrick Haugh Oct 29 '18 at 20:08
  • If you want to mention a user, you can directly use `user.mention` to get a string that will mention the user when included in a message. – Patrick Haugh Oct 29 '18 at 20:09
  • 1
    `discord.User.id` isn't getting the `id` of an instance of the `User` class, it's getting that attribute from the base class itself. You need to instantiate the `User` object correctly, and read values from instances of it. If you haven't instantiated it, how to you expect your code to even know which user you want the ID of? – Random Davis Oct 29 '18 at 20:11
  • @PatrickHaugh In both cases I get only "ctx is a required argument that is missing". – Kendel Ventonda Oct 29 '18 at 20:14
  • @RandomDavis I was expecting the ID of the author, as it says. I thought, it would give back the ID of the user that sends the message. – Kendel Ventonda Oct 29 '18 at 20:15
  • Looks like you're using the older async branch. – Patrick Haugh Oct 29 '18 at 20:16
  • @KendelVentonda I don't see how you would have thought that would return anything useful. Even if the `name` function provided access to an instance of a `User` (which it does when used correctly), the line `discord.User.id` completely bypasses that, and is instead looking at the definition of the `User` class. It's impossible for that line to return anything else no matter where or when you run it in your project, since it's looking at something completely static. – Random Davis Oct 29 '18 at 20:22

2 Answers2

5

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))
Patrick Haugh
  • 59,226
  • 13
  • 88
  • 96
  • Okay, we had this version as well but as it seems I made a typo. Works fine here and explains a lot. Thanks for the solution and help. – Kendel Ventonda Oct 29 '18 at 20:24
1

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
Andrew S
  • 21
  • 4