首页 > 解决方案 > 从 url 更新机器人的头像

问题描述

我正在使用 discord.py,但我似乎无法使用图像 url 更新机器人的头像。如果它是一条路径,它工作得很好。因此,我希望得到一些帮助,确切地知道出了什么问题以及如何解决它。这是我的代码。谢谢。

    @commands.command(aliases=["edit"], hidden=True)
    @commands.is_owner()
    async def edit_bot_pic(self, ctx, avatar_location):
        valid = validators.url(avatar_location)
        if valid:
            async with aiohttp.ClientSession() as session:
                async with session.get(avatar_location) as resp:
                    buffer = io.BytesIO(await resp.read())
                    await self.bot.user.edit(avatar=buffer)
        else:
            with open(avatar_location, "rb") as file:
                avatar = file.read()
                await self.bot.user.edit(avatar=avatar)

标签: pythonurliodiscorddiscord.py

解决方案


你快到了,你需要传递一个文件而不是缓冲区才能工作。

buffer = io.BytesIO(await resp.read())
file = discord.File(buffer)
await self.bot.user.edit(avatar=file)

参考:-


推荐阅读