在Webhook的头像上上传本地图像时,出现错误'_io.BufferedReader'对象没有属性'startswith'

时间:2019-04-13 17:36:23

标签: python python-3.x discord discord.py discord.py-rewrite

我正在尝试使用本地图像作为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")

1 个答案:

答案 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

  

avatarOptional[bytes])–类似于字节的对象,代表Webhook的默认头像。