pythondiscorddiscord.pyavatar

discord.py: Get user's avatar from message instead of ctx


Hey guys I'm currently working on a function in my discord bot that would return the profile picture of the person that's mentioned after !pfp. Problem is: All my bot been working with if's statements in the on_message() function, which means I'm not using ctx, if that makes sense to you.

So from what I've been reading online the answers only give you a code with something like async def avatar(ctx, *, member: discord.Member = None):.

I'm not even sure that you can do it the way i want it, but i just want to ask first. Here is the code

import *


@client.event
async def on_message(message):

    async def pfp():
        try:
            person = message.content.split(" ", -1)[1]
            person = pers.replace("<", "")
            person = pers.replace("@", "")
            person = pers.replace(">", "")
            person = int(pers)

            # we have the id of the user in a variable

            pic = person.avatar_url
            embed = discord.Embed(title=f"profile pic of <@{person}>",
                                  color=0x3a88fe)
            embed.set_image(url=pic)
            await message.channel.send(embed=embed)

        except IndexError:
            authorProfilePicture = message.author.avatar_url
            a = message.author
            print(type(a))
            await message.channel.send(authorProfilePicture)



    if message.content.startswith("!pfp"):
        await pfp()

That's what i'm trying to do, but the code won't recognize the person variable as it is an int variable and not a discord.Member or something.

Thank you if you took time to read this, and for those of you who will help !


Solution

  • You were almost there. Your mistake is, as you already realized, that person is still an ID and accordingly of type int which hasn't an attribute avatar_url.

    To get the discord.User object, just do bot.get_user().
    Your code would look like that (I changed the variables a bit):

    bot = commands.Bot()
    
    @bot.event
    async def on_message(message: discord.Message):
    
        async def pfp():
            try:
                person_mention = message.content.split(" ", -1)[1]
                person_mention = person_mention.replace("<", "")
                person_mention = person_mention.replace("@", "")
                person_mention = person_mention.replace(">", "")
    
                person_id = int(person_mention)
                person = bot.get_user(person_id)
                avatar_url = person.avatar_url
    
                embed = discord.Embed(title=f"Profile picture of {person}")
                embed.set_image(url=avatar_url)
                await message.channel.send(embed=embed)
    
            except IndexError:
                avatar_url = message.author.avatar_url
    
                embed = discord.Embed(title=f"Profile picture of {message.author}")
                embed.set_image(url=avatar_url)
                await message.channel.send(embed=embed)
    
    
        if message.content.startswith("!pfp"):
            await pfp()
    

    References: