Discord.py |保存用户头像

Discord.py | Saving users avatar

我正在尝试让机器人编辑用户头像。我目前只是在试验它。 首先,我想将头像保存到我的本地驱动器。我不知道怎么办。

这是我的:

@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'))

错误:

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

如何解决这个错误?

Discord 有 save() 方法来保存用户头像:

@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)