Dano
Dano

Reputation: 55

Problem with using a file to embed attachements in discord.py

This is the code that i use:

if message.attachments:
                file = await message.attachments[0].to_file()
                file.filename = 'image.png'
                embed.set_image(url='attachment://image.png')
for x in my_channels:
    webhook = Webhook.from_url(x, adapter=AsyncWebhookAdapter(session))
    await webhook.send(file=file, embed=embed)

It sends the embed to 3 channels using a webhook ( tried the normal way and still doesn't work ), the image is sent to only one channel, other ones don't have the image field. There aren't any errors but I suppose the file is somehow "not reusable" and since I need to send it separately, it's not in the saved embed. Are there any solutions to that?

Upvotes: 0

Views: 459

Answers (1)

Łukasz Kwieciński
Łukasz Kwieciński

Reputation: 15689

From the docs

File objects are single use and are not meant to be reused in multiple abc.Messageable.send()s.

One way to fix your problem is to re-define the file inside the for loop

if message.attachments:
    embed.set_image(url='attachment://image.png')
    for x in my_channels:
        file = await message.attachments[0].to_file()
        file.filename = 'image.png'
        webhook = Webhook.from_url(x, adapter=AsyncWebhookAdapter(session))
        await webhook.send(file=file, embed=embed)

Upvotes: 1

Related Questions