Get avatar of nitro users

Viewed 345

I am currently developing a bot for Discord using discord.py.

The bot is triggered using Slash Commands. One of the parameter of my slash command is a user object. I need the URL of the user's avatar. I can get that by using the property avatar_url of the user object. It works just fine with regular users.

The problem comes with Nitro users. They can choose to have a special avatar depending on the guild they are on. When they choose to, the property avatar_url gives the "default" avatar, not the one currently in use in the guild.

I tried to use the Member object instead, but avatar_url still points to the default avatar, and not the guild-specific avatar.

# userobj is the user object given by the slash command
# context is the context object given by the slash command
member = context.guild.get_member(userobj.id)
if member:
    print(userobj.avatar_url)
    print(member.avatar_url) # They are the same

Do you know how I can get the URL of the guild-specific avatar of nitro users instead of the default avatar?

3 Answers

For the latest version of the discord.py, (which is v1.7.3 afaik) there is no way to do this but it exists in the master aka 2.0 unreleased version of the discord.py.

Check this.

IMPORTANT: Only available in v2.0+

You might be able to use discord.Member.guild_avatar.

Returns an Asset for the guild avatar the member has. If unavailable, None is returned.

Note that this returns a discord.Asset object, so you'll need to access it's url using discord.Member.guild_avatar.url

member = context.guild.get_member(userobj.id)

if member:
    print(userobj.avatar_url) # User avatar
    print(member.avatar_url)  # Member avatar (same as user avatar)

    if member.guild_avatar:
        print(member.guild_avatar.url)
    else:
        print("No Guild Avatar")

TheNightHawk's answer is the proper answer to my question. However, I ended up implementing a direct call to the API itself, as TheNightHawk suggested, and I thought it would be nice to share this solution too.

import json
import requests

guild_id   = ...
user_id    = ...
token      = ...
appurl     = ... # Can be anything
appversion = ... # Can be anything

url      = f"https://discord.com/api/guilds/{guild_id}/members/{user_id}"
res      = requests.get(url, headers = {
                 "Authorization": f"Bot {token}",
                 "User-Agent":    f"DiscordBot ({appurl}, {appversion})"})
data     = json.loads(res.content)

if data['avatar'] is None:
    avatar_ext = ("gif"
                  if data['user']['avatar'].startswith("a_")
                  else "png")
    avatar_url = ( "https://cdn.discordapp.com/avatars/" +
                  f"{user_id}/{data['user']['avatar']}" +
                  f".{avatar_ext}?size=1024")
else:
    avatar_ext = ("gif"
                  if data['avatar'].startswith("a_")
                  else "png")

    avatar_url = ( "https://cdn.discordapp.com/guilds/" +
                  f"{guild_id}/users/{user_id}/avatars/" +
                  f"{data['avatar']}.{avatar_ext}?size=1024")
Related