How to get a user's avatar with their id in discord.py?

Question:

I tried using the following code but it didn’t work.

@bot.command()
async def avatar(ctx,*, avamember):
    user = bot.get_user(avamember)
    await ctx.send(f"{user.avatar_url}")

Edit: For anyone that had a similar problem, while not mentioned in the docs, discord.Member can take user ids aside from @username so there isn’t any need for a complicated way.

Asked By: jitter

||

Answers:

I’m presuming you’re Tagging the user with @UserNameHere in discord. It’s much easier to take that input as a member Object 🙂

You also don’t need to wrap the url in quotes.

This code is if it is in a cog:

@commands.command()
async def avatar(self, ctx, *,  avamember : discord.Member=None):
    userAvatarUrl = avamember.avatar_url
    await ctx.send(userAvatarUrl)

This code is if it is in the main bot.py file:

@bot.command()
async def avatar(ctx, *,  avamember : discord.Member=None):
    userAvatarUrl = avamember.avatar_url
    await ctx.send(userAvatarUrl)
Answered By: Kelo

A more optimized version of the code:

@bot.command()
async def get_user_icon(ctx, member:discord.Member):
    await ctx.send(member.avatar_url)
if message.content == '!avatar':
    clientProfilePicture = message.author.avatar_url
    await message.channel.send(clientProfilePicture)
Answered By: hieu letrung

you cant just use avamember simply as parameter, you need to define avamember using avamember:discord.Member. Perhaps you’re trying to create an avatar command, this will work

@bot.command()
  async def avatar(ctx, *, avamember: discord.Member = None):
       if avamember == None:
            embed = discord.Embed(description='❌ Error! Please specify a user',
                                  color=discord.Color.red())
            await ctx.reply(embed=embed, mention_author=False)
        else:
            userAvatarUrl = avamember.avatar_url
            embed = discord.Embed(title=('{}'s Avatar'.format(avamember.name)), colour=discord.Colour.red())
            embed.set_image(url='{}'.format(userAvatarUrl))
            await ctx.reply(embed=embed, mention_author=False)

If you’re using Cogs then use this one

@commands.command()
  async def avatar(self, ctx, *, avamember: discord.Member = None):
       if avamember == None:
            embed = discord.Embed(description='❌ Error! Please specify a user',
                                  color=discord.Color.red())
            await ctx.reply(embed=embed, mention_author=False)
        else:
            userAvatarUrl = avamember.avatar_url
            embed = discord.Embed(title=('{}'s Avatar'.format(avamember.name)), colour=discord.Colour.red())
            embed.set_image(url='{}'.format(userAvatarUrl))
            await ctx.reply(embed=embed, mention_author=False) 
Answered By: Eren
import discord
from discord.ext import commands
import os
import random

intents = discord.Intents.default()
intents.members = True
client = commands.Bot(command_prefix='>',intents=intents)

@client.event
async def on_ready():
    print("Log : "+str(client.user))

@client.command()
async def avatar(ctx, *, member: discord.Member = None):
    if not member:
        member = ctx.message.author
    userAvatar = member.avatar_url
    em = discord.Embed(color = discord.Color.from_rgb(255, 0, 0), title = "Avatar Link", url=userAvatar)
    em.set_image(url=f"{userAvatar}")
    em.set_author(name=f"{member}", icon_url=f"{userAvatar}")
    em.set_footer(text=f'Requested by {ctx.message.author}', icon_url=f"{ctx.author.avatar_url}")
    await ctx.reply(embed=em)

client.run("token")
Answered By: tre flse

Didnt work for me, this did though

@bot.command()
async def userprofile(ctx, member:discord.Member):
    await ctx.send(member.avatar)

Not sure about you guys though

Answered By: AAAAAAA
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.