Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I get the avatar of a specific user using his user id with discord.py

I used discord.py to make a bot that stores the user ids in its database to identify them but I can't figure out how to get the avatar of a specific user just by using their id. I searched around and found about something like this - Client.get_user() but it doesn't work for me as I couldn't understand it's working from the documentation. Is there any way I can pick up a user's id string from my database and pass it through a function to get that user's avatar and use that in an embed?

I found another question on StackOverflow like mine but the solution in that one doesn't work for me as well.

like image 574
dunking-the-hamster Avatar asked Sep 19 '25 14:09

dunking-the-hamster


1 Answers

According to the article, you get the user's id and request the avatar data of the user. It looks like string. Here is the sample JSON response from the request:

{
  "id": "80351110224678912",
  "username": "Nelly",
  "discriminator": "1337",
  "avatar": "8342729096ea3675442027381ff50dfe",
  "verified": true,
  "email": "[email protected]",
  "flags": 64,
  "premium_type": 1,
  "public_flags": 64
}

Now when you get this: "avatar": "8342729096ea3675442027381ff50dfe", you know that the avatar data is 8342729096ea3675442027381ff50dfe. After that, you will use Image Base Url for images: https://cdn.discordapp.com/.
To request an image you have to chose format (jpg, gif, png etc...). So your final request should be:

https://cdn.discordapp.com/avatars/{user_id}/{user_avatar}.png

where user_id and user_avatar are variables to be changed. For example:

https://cdn.discordapp.com/avatars/80351110224678912/8342729096ea3675442027381ff50dfe.png
like image 93
Giorgi Gvimradze Avatar answered Sep 21 '25 02:09

Giorgi Gvimradze