How to make Discord bot repost attachments in a single row?

I’m trying to create a Discord bot that can repost images and videos from one channel to another. Right now, my bot grabs the attachment URLs and reposts them, but there’s a problem. The original post shows images side by side in a nice row, but when my bot reposts them, each image appears on a separate line, taking up too much vertical space.

I tried using embeds, but that only displayed one image at a time. Plus, the reposted attachments come with an extra icon that the original posts don’t have.

Here’s a simplified version of my code:

for attachment in message.attachments:
    attachment_urls.append(attachment.url)

await target_channel.send(' '.join(attachment_urls))

Can anyone suggest a way to have the bot repost the attachments so they display in a single row, just like the original post? Thanks for any help!

I’ve dealt with this issue before, and it can be frustrating. One approach that worked for me was using Discord’s Webhook functionality. It gives you more control over message formatting.

Here’s what I did:

  1. Created a webhook for the target channel
  2. Used aiohttp to send a custom JSON payload with all attachment URLs

The code looked something like this:

import aiohttp
from discord import Webhook, AsyncWebhookAdapter

async def repost_attachments(message, webhook_url):
    attachment_urls = [attachment.url for attachment in message.attachments]
    
    async with aiohttp.ClientSession() as session:
        webhook = Webhook.from_url(webhook_url, adapter=AsyncWebhookAdapter(session))
        await webhook.send(content=' '.join(attachment_urls))

This method allowed me to send all attachments in a single message, which Discord then displayed in a row. It’s not perfect, but it’s the closest I’ve gotten to replicating the original post’s layout.

Just be aware of rate limits when using webhooks, and make sure to handle any potential errors. Good luck with your bot!

hey there! have u tried using discord’s File class? it might help. something like:

files = [discord.File(await attachment.to_file()) for attachment in message.attachments]
await target_channel.send(files=files)

this should send all attachments in one message. hope it helps!

Have you considered using Discord’s webhook functionality? It offers more flexibility in message formatting. You could create a webhook for your target channel and use it to send a custom JSON payload that includes all attachment URLs. Something like this:

import aiohttp
from discord import Webhook, AsyncWebhookAdapter

webhook_url = 'YOUR_WEBHOOK_URL_HERE'
async with aiohttp.ClientSession() as session:
    webhook = Webhook.from_url(webhook_url, adapter=AsyncWebhookAdapter(session))
    await webhook.send(content=' '.join(attachment_urls))

This method might give you more control over how the attachments are displayed. It’s worth experimenting with different payload structures to see if you can achieve the desired single-row layout. Just remember to handle any potential rate limiting or errors that might occur when using webhooks.