1

I'm setting up a bot and need to get a user object when someone enters a command followed by the username of the user or mentions the user. I don't need both of those to work, just either or.

I see you can use client.get_user with an id however I don't want the people entering the command to have to deal with getting the id and would rather they just use the username or mention the user. I already have the command part working I just need to be able to get the user and was wondering if there is anything like get_user that uses the username or mention of the user.

  • 1
    Possible duplicate of [How to get the ID of a mentioned User in Python discord bot?](https://stackoverflow.com/questions/48354901/how-to-get-the-id-of-a-mentioned-user-in-python-discord-bot) – user10455554 Jul 12 '19 at 08:59
  • Can we see your code? Are you using the `commands` extension? If so, you can use [a converter](https://discordpy.readthedocs.io/en/latest/ext/commands/commands.html#converters) – Patrick Haugh Jul 12 '19 at 12:36
  • This works, I would say this as an answer, but to be honest I dont know what it's called ```def command(ctx, member: discord.Member, *args)``` – Kaito Jul 13 '19 at 12:45

1 Answers1

0

Well, there are 2 solutions to choose (others might find more)

  1. Getting the user object (users won't need to send their id, bot will do it)

    user = discord.utils.get(ctx.guild.members, id=ctx.author.id)
    

    And then just send a message:

    await ctx.send(f"Hello {user.mention}")
    
  2. Just using ctx.author (way easier than first solution)

    await ctx.send(f"Hello {ctx.author.mention}!")
    
Dec0Dedd
  • 335
  • 2
  • 12