我正在尝试使用本地图像作为Webhook的化身,Webhooks不允许将图像链接用作化身,但是使用本地图像给我错误:'_io.BufferedReader' object has no attribute 'startswith'
,下面是我的抄写
不允许使用链接作为头像(我认为这是因为当我使用图像链接时会出现错误:TypeError:startswith first 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")
答案 0 :(得分:0)
您需要将化身图像的数据作为bytes
对象而不是包含数据的文件对象传入。抛出异常是因为create_webhook()
代码试图在您传入的pfp
对象上使用bytes.startswith()
method,并且文件对象没有该方法。
传递pfp
的结果而不是pfp.read()
本身。这会返回图像数据作为bytes
值:
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]
)–类似于字节的对象,代表Webhook的默认头像。