我正在尝试使用本地图像作为webhook的头像作为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")
您需要传递头像图像的数据,作为bytes
对象,而不是包含数据的文件对象。抛出异常是因为create_webhook()
代码尝试在传入的bytes.startswith()
method对象上使用pfp
,而文件对象没有该方法。
而不是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的默认头像。