获取错误'_io.BufferedReader'对象在webhook的头像上传本地图像时没有属性'startswith'

问题描述 投票:1回答:1

我正在尝试使用本地图像作为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")
python python-3.x discord discord.py discord.py-rewrite
1个回答
0
投票

您需要传递头像图像的数据,作为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

avatarOptional[bytes]) - 一个类似字节的对象,代表webhook的默认头像。

© www.soinside.com 2019 - 2024. All rights reserved.