在 webhook 的头像上上传本地图像时出现错误“_io.BufferedReader”对象没有属性 'startswith'
Getting error '_io.BufferedReader' object has no attribute 'startswith' while uploading local image on a webhook's avatar
我正在尝试将本地图像用作 webhook 的头像作为 webhook,webhook 不允许图像 links 作为头像,但使用本地图像会出现错误:'_io.BufferedReader' object has no attribute 'startswith'
, 下面是我的股票
因为不允许使用 link 作为头像(我认为这是因为当我使用图像 link 时出现错误:TypeError: startswith first arg must be str or a tuple 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()
代码试图在您传入的 pfp
对象上使用 bytes.startswith()
method,而文件对象没有该方法。
而不是pfp
本身,传入pfp.read()
的结果。此 returns 图像数据作为 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]
) – A bytes-like object representing the webhook’s default avatar.
我正在尝试将本地图像用作 webhook 的头像作为 webhook,webhook 不允许图像 links 作为头像,但使用本地图像会出现错误:'_io.BufferedReader' object has no attribute 'startswith'
, 下面是我的股票
因为不允许使用 link 作为头像(我认为这是因为当我使用图像 link 时出现错误:TypeError: startswith first arg must be str or a tuple 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()
代码试图在您传入的 pfp
对象上使用 bytes.startswith()
method,而文件对象没有该方法。
而不是pfp
本身,传入pfp.read()
的结果。此 returns 图像数据作为 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]
) – A bytes-like object representing the webhook’s default avatar.