NiA
NiA

Reputation: 1

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.avatar_url?

I already got the mentioned user's ID by slicing the message's content.

Upvotes: 0

Views: 25979

Answers (6)

Sera Rose
Sera Rose

Reputation: 1

for discord.py v2, use member.avatar not member.avatar_url

here's an example

@bot.command()
async def avatar(ctx, *, member: discord.Member = None):
    if not member:
        member = ctx.message.author
    embed = discord.Embed()
    embed.set_image(url=member.avatar)
    await ctx.send(embed=embed)

EDIT: for if you're using discord-py-slash-commands the following example should work for you https://discord-py-slash-command.readthedocs.io/en/latest/quickstart.html

@bot.tree.command(name="avatar", description="gets mentioned users avatar")
async def avatar(interaction: discord.Interaction, member: discord.Member):
    if not member:
        member = interaction.user
    embed = discord.Embed()
    embed.set_image(url=member.avatar)
    await interaction.response.send_message(embed=embed)

Upvotes: 0

hmmm
hmmm

Reputation: 1

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

Upvotes: 0

Freddy Mcloughlan
Freddy Mcloughlan

Reputation: 4496

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)

Upvotes: 2

Souvik Guria
Souvik Guria

Reputation: 131

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

@client.command(name='avatar', help='fetch avatar of a user')
async def dp(ctx, *, member: discord.Member = None):
    if not 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

Upvotes: 4

ShikariEagle
ShikariEagle

Reputation: 1

You can use this:

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

On use message args

Upvotes: 0

Harmon758
Harmon758

Reputation: 5157

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.

Upvotes: 1

Related Questions