Skip to content Skip to sidebar Skip to footer

How To Get A Mentioned User's Avatar Url With Discord.py?

How can I get a mentioned user's avatar? I can get a mentioned user's ID, but I can't find out how to use it like message.author.avatar_url. Can I make this into (Userid).author.av

Solution 1:

client = commands.Bot(command_prefix='-')

@client.command(name='avatar', help='fetch avatar of a user')asyncdefdp(ctx, *, member: discord.Member = None):
    ifnot member:
        member = ctx.message.author
    userAvatar = member.avatar_url
    await ctx.send(userAvatar)

The command will be something like => '-avatar' or '-avatar [user]'

P.s. Don't use [ ] after '-avatar' and before mentioning the user

If a user is not mentioned, it will fetch the avatar of the user sending the command

Solution 2:

If you're using the commands extension, you can use a MemberConverter or UserConverter to get the Member or User object, respectively. Otherwise, you can use the Message.mentions attribute of the Message object to get a list of the Members that were mentioned in the message.

If you have the user ID already, you can use of the methods covered in the How do I get a specific model? section of the FAQ in the documentation to retrieve the Member or User object.

You can then use the avatar_url attribute of the Member or User object.

Solution 3:

author = ctx.message.author
pfp = author.avatar_url

Author creates an object and can be used a string e.g username#0000

pfp (profile picture) is an asset that can be used in an embed for example

embed = discord.Embed()
embed.set_image(url=pfp)

await ctx.send(embed=embed)

Solution 4:

You can use this:

author = message.author
await message.channel.send(author.avatar_url)

On use message args

Solution 5:

If you're using discord.py v2 it is: message.author.avatar.

Check this: https://discordpy.readthedocs.io/en/master/api.html?highlight=member#discord.Member

Post a Comment for "How To Get A Mentioned User's Avatar Url With Discord.py?"