gpt4 book ai didi

python - 在 webhook 的头像上上传本地镜像时出现错误 '_io.BufferedReader' 对象没有属性 'startswith'

转载 作者:太空宇宙 更新时间:2023-11-03 21:01:44 25 4
gpt4 key购买 nike

我正在尝试使用本地镜像作为 webhook 的头像作为 webhook,webhook 不允许图像链接作为头像,但使用本地镜像会出现错误:'_io.BufferedReader' 对象有没有属性“startswith”,下面是我的脚本

由于不允许使用链接作为头像(我认为这是因为当我使用图像链接时我收到错误:TypeError:startswith第一个arg必须是str或str的元组,而不是字节)我尝试使用使用 with open 的本地文件,但我收到了更多错误!

@bot.command()
async def whook(ctx):
with open("image.png", 'rb') as pfp:
await ctx.channel.create_webhook(name="Mr.W.hook",avatar=pfp)
await ctx.send("Done")

最佳答案

您需要将头像图像的数据作为字节对象传入,而不是包含数据的文件对象。引发异常是因为 create_webhook() 代码尝试使用 bytes.startswith() method在您传入的 pfp 对象上,而文件对象没有该方法。

传入 pfp.read() 的结果,而不是 pfp 本身。这将图像数据作为字节值返回:

with open("image.png", 'rb') as pfp:
await ctx.channel.create_webhook(name="Mr.W.hook", avatar=pfp.read())
await ctx.send("Done")

来自 discord.TextChannel.create_webhook() documentation :

avatar (Optional[bytes]) – A bytes-like object representing the webhook’s default avatar.

关于python - 在 webhook 的头像上上传本地镜像时出现错误 '_io.BufferedReader' 对象没有属性 'startswith',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55668180/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com