python - 从 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)
解决方案
你快到了,你需要传递一个文件而不是缓冲区才能工作。
buffer = io.BytesIO(await resp.read())
file = discord.File(buffer)
await self.bot.user.edit(avatar=file)
参考:-
推荐阅读
- windows - Bluestacks 作为 Android 模拟器 - 运行应用程序时出错
- javascript - 将动态值与逗号分隔的属性值进行比较
- javascript - 为什么这段代码不替换每个单词的第一个字母?
- ipc - 简单的进程间通信
- azure-devops - 如何为 Azure DevOps SSH 任务转义分号 (;) 字符?
- powershell - 创建新 AD 用户的脚本失败
- sql-server - SQL Pivot 作为子查询
- php - JSON - 使用 php 按日期对值求和
- html - 是否可以将自定义元素与 .Net Web Forms 控件一起使用?
- java - 如何从我的活动中的另一个活动中读取方法代码?