TechnicPepijn
TechnicPepijn

Reputation: 35

Discord.py | Saving users avatar

I am trying to make the bot edit the users avatar. I am currently just experimenting with it. First I want to save the avatar to my local drive. I can't figure out how.

This is what I have:

@client.command()
async def avatar(ctx):
    im1 = Image.new('RGB', (200, 200), (20,20,20))

    url = f'https://cdn.discordapp.com/avatars/{ctx.author.id}/{ctx.author.avatar}.jpg'
    filename = 'avatar.jpg'

    print('Beginning file download with urllib2...')
    urllib.request.urlretrieve(url, filename)

    im2 = Image.open(filename)

    back_im = im1.copy()
    back_im.paste(im2, (100, 100))

    back_im.save('avatar1.jpg')

    await ctx.send("Enjoy :>", file=File('avatar1.jpg'))

Error:

discord.ext.commands.errors.CommandInvokeError: Command raised an exception: HTTPError: HTTP Error 403: Forbidden

How can I solve this error?

Upvotes: 3

Views: 2703

Answers (1)

Diggy.
Diggy.

Reputation: 6944

Discord has the save() method to save user's avatars:

@client.command()
async def avatar(ctx):
    filename = "avatar1.jpg"
    await ctx.author.avatar_url.save(filename)
    file = discord.File(fp=filename)
    await ctx.send("Enjoy :>", file=file)

Upvotes: 3

Related Questions