Search code examples
pythondiscorddiscord.py

discord.py image command not working when @ other users


I want my bot to get the mentioned user pfp, and overlay it with the defined image

When i type "$avatartest", the bot get my discord PFP, and overlay's the original image with it. It works how it should, but my problem is when i want the bot to use other people pfp's. When i try mentioning other users, like " $avatartest @NotSoBot#9555" it only shows my own avatar. What can i do to make it work with mentions?

My code:

intents = discord.Intents.all()

intents.members = True
bot = commands.Bot(command_prefix= '$', intents=intents)

@bot.command()
async def avatartest(ctx, user: discord.Member = None):
if user == None:
user = ctx.author

    overlay = Image.open('the.png')

    asset = ctx.author.avatar
    data = BytesIO(await asset.read())
    pfp = Image.open(data)

    pfp = pfp.resize((177,177))

    overlay.paste(pfp, (120,212))

    overlay.save("profile.png")

    await ctx.send(file = discord.File("profile.png"))

bot.run(tokenbot)    

Solution

  • That's because you're still getting the avatar from the ctx.author and not from the user that you wanted to.

    @bot.command()
    async def avatartest(ctx, user: discord.Member = None):
        if user == None:
            user = ctx.author
    
        overlay = Image.open('the.png')
        asset = ctx.author.avatar
        #       ^^^^^^^^^^
    
        ...
    

    You can easily fix this by getting it from the user instead.

    @bot.command()
    async def avatartest(ctx, user: discord.Member = None):
        if user == None:
            user = ctx.author
    
        overlay = Image.open('the.png')
        asset = user.avatar
        #       ^^^^
    
        ...