简体   繁体   中英

Getting error '_io.BufferedReader' object has no attribute 'startswith' while uploading local image on a webhook's avatar

I I'm trying to use a local image as a webhook's avatar as a webhook, webhooks don't allow image links as an avatar but using a local image gives me error: '_io.BufferedReader' object has no attribute 'startswith' , here below is my scrip

As using a link as an avatar isn't allowed (I think this because when i use an image link i get error: TypeError: startswith first arg must be str or a tuple of str, not bytes) I tried to use a local file using with open but I'm just getting more errors!

@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")

You need to pass in the data of the avatar image, as a bytes object, not the file object that contains the data. The exception is thrown because the create_webhook() code tries to use the bytes.startswith() method on the pfp object you passed in, and file objects don't have that method.

Instead of pfp itself, pass in the result of pfp.read() . This returns the image data as a bytes value:

with open("image.png", 'rb') as pfp:
    await ctx.channel.create_webhook(name="Mr.W.hook", avatar=pfp.read())
    await ctx.send("Done")

From the discord.TextChannel.create_webhook() documentation :

avatar ( Optional[bytes] ) – A bytes-like object representing the webhook's default avatar.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM