1

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.

jitter
  • 29
  • 1
  • 1
  • 8

6 Answers6

5

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)
Kelo
  • 1,783
  • 2
  • 9
  • 21
  • Nope. I wasn't tagging them, just inputting their id. But yeah, your solution worked. Thanks. – jitter Jan 18 '20 at 12:16
  • Sir, I want to get avatar of member in ` on_member_join ` as we know that it only takes member I cant use ctx so how can i get avatar image. This is required me because I want to make welcome embed just like mee6 – SDB PROGRAM LEARNER Apr 10 '23 at 16:32
0
if message.content == '!avatar':
    clientProfilePicture = message.author.avatar_url
    await message.channel.send(clientProfilePicture)
0

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) 
Eren
  • 1
  • 2
0
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")
0

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

  • Hi there This is already listed in this thread, but requires passing a keyword argument. There's also a similar answer [here](https://stackoverflow.com/a/66571851/12439119). – Alexander L. Hayes Jan 01 '23 at 02:05
  • As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Jan 01 '23 at 13:08
-4

A more optimized version of the code:

@bot.command()
async def get_user_icon(ctx, member:discord.Member):
    await ctx.send(member.avatar_url)
Community
  • 1
  • 1