1
@bot.command()
async def id(ctx, a:str): #a = @user

how would I get the ID of a user mentioned in the command, and output it as:

await ctx.send(id)
Patrick Haugh
  • 59,226
  • 13
  • 88
  • 96
explodingfilms101
  • 480
  • 2
  • 11
  • 23

3 Answers3

6

Use a converter to get the User object:

@bot.command(name="id")
async def id_(ctx, user: discord.User):
    await ctx.send(user.id)

Or to get the id of the author:

@bot.command(name="id")
async def id_(ctx):
    await ctx.send(ctx.author.id)
Patrick Haugh
  • 59,226
  • 13
  • 88
  • 96
  • This just changed my life. Use `commands.errors.UserNotFound` to handle a bad `user` argument since you can't do this from inside the function. https://discordpy.readthedocs.io/en/stable/ext/commands/commands.html#error-handling – iyrin Mar 04 '22 at 02:01
2

Just realized that when you @someone and store it to the variable "a", it contains the user ID in the form of '<@userid>'. So a bit of clean up can get me the user ID

Here's the code:

@bot.command()
async def id(ctx, a:str):
    a = a.replace("<","")
    a = a.replace(">","")
    a = a.replace("@","")
    await ctx.send(a)

Since my command consists of "rev id @someone", the @someone gets stored in 'a' as the string '<@userid>' instead of '@someone'.

explodingfilms101
  • 480
  • 2
  • 11
  • 23
  • This may not be reliable due to an inconsistency in how users/nicknames or desktop/mobile apps format the mentions. If you want to format the mention manually, use `a.replace("!", "")` as well. "If the old substring is not found, it returns the copy of the original string." https://github.com/discord/discord-api-docs/issues/1912 https://www.programiz.com/python-programming/methods/string/replace – iyrin Mar 03 '22 at 23:01
0

If you want to handle a mention within your function, you can get the mention from the context instead of passing the mention as a string argument.

@bot.command()
async def id(ctx):
    # Loop through the list of mentioned users and print the id of each.
    print(*(user_mentioned.id for user_mentioned in ctx.message.mentions), sep='\n')

ctx.message.mentions will return:

A list of Member that were mentioned. If the message is in a private message then the list will be of User instead.

When you loop through ctx.message.mentions, each item is a mentioned member with attributes such as id, name, discriminator. Here's another example of looping through the mentioned list to handle each member who was mentioned:

for user_mentioned in ctx.message.mentions:
    # Now we can use the .id attribute.
    print(f"{user_mentioned}'s ID is {user_mentioned.id}")

It's up to you whether you want to require the argument a as shown in the question above. If you do need this, note that the string will sometimes include an exclamation in the mention depending on whether it is:

  • for a User or command was posted from mobile app: <@1234567890>
  • for a Nickname or command was posted from desktop app: <@!1234567890>

Which is why I prefer to get the id from a member/user attribute.

iyrin
  • 612
  • 5
  • 14