How to Attach Multiple Images in a Embed

Is it possible to attach multiple images in a embed?

There is actually a way. Webhook messages can contain up to 10 embeds per message. So you can, using a webhook, send 10 embeds, each of them containing one images.

The image are supposed to have width and height fields documented here but I didn't see any way with embed object or RichEmbed to do it, so you'll have to resize the images you want to use so they are of the same size.

In your case, the idea of slothiful: merging the image in one canvas and rendering it, would be a better and closer to the picture you shared

const client = new Discord.Client();
const hook = new Discord.WebhookClient(webHook.id, webHook.token);

client.on('ready', () => {
console.log('Starting!');
client.user.setActivity(config.activity);
});



client.on('message', async (msg) => {
if (msg.author.bot) { return; }
sendImage();
});

let webHook = {
token: "token-webhook",
id: "id-webhook"
};

let img = [
'https://i.imgur.com/ezC66kZ.png',
'https://i.imgur.com/wSTFkRM.png'
];

function sendImage() {
let embeds = [];
embeds.push(new Discord.RichEmbed()
.setTitle('First Messages')
.setImage(img[0])
.setTimestamp()
.setFooter('Pulled time:'));
embeds.push(new Discord.RichEmbed()
.setTitle('First Messages')
.setImage(img[1])
.setTimestamp()
.setFooter('Pulled time:'));
hook.send({embeds: embeds});
}

client.login(config.token)
.then(() => console.log("We're in!"))
.catch((err) => console.log(err));

Render:

enter image description here

Multiple photos in discord.py embed

I have linked an image I hope can help you to understand how embeds work a bit better, so essentially embeds can only have 1 image per one and you will need to send two embeds, unfortunately. Also, here is a nice online embed visualizer.

I hope this helped

How to add multiple embed images in an email using python

The solution turns out to be:

def AddImage(self, fileName, title):
internalFileName = '%s-%s-%s' %(fileName, datetime.now().strftime('%Y%m%d%H%M%S'), uuid.uuid4())
self.imgHtml +='<p style="font-size:15px;font-weight:bold;font-family:Comic Sans MS">%s</p><br><img src="cid:%s"><br>' %(title, internalFileName)
fp = open(fileName, 'rb')
msgImage = MIMEImage(fp.read())
fp.close()
msgImage.add_header('Content-ID', '<%s>' %(internalFileName))
self.msgRoot.attach(msgImage)
def Send(self, toList):
msgText = MIMEText(self.imgHtml, 'html')
self.msgAlternative.attach(msgText)
self.msgRoot['Subject'] = 'Audience Ingestion Integrated Test Report @%s [%s]' %(datetime.now().strftime('%Y-%m-%d'), socket.gethostname())
strFrom = 'notifier@freewheel.tv'
self.msgRoot['From'] = strFrom
strTo = email.Utils.COMMASPACE.join(toList)
self.msgRoot['To'] = strTo
smtp = smtplib.SMTP('smtp1.dev.fwmrm.net', 25)
smtp.sendmail(strFrom, strTo, self.msgRoot.as_string())
smtp.quit()

which means that when AddImage(), just attach the MIMEImage to the MIMEMultipart and add the string to the html string, and when Send() after several invocations of AddImage(), attach the MIMEText generated from the html string to MIMEMultipart.

Attach multiple images into html email in Python

Simply keep filenames as list and use for-loop to repeate the same code for different items from list.

To have different Content-ID you can simply use f-string like f"<image{number}> with enumerate().

filenames = [
'newsletter/bar_chart.png',
'newsletter/scatter_plot.png',
'newsletter/image3.png',
'newsletter/image4.png',
]

for number, name in enumerate(filenames, 1):
fp = open(name, 'rb')
msg_image = MIMEImage(fp.read()) # PEP8: `lower_case_names` for variables
fp.close()
msg_image.add_header('Content-ID', f'<image{number}>')
msg_root.attach(msg_image)

This way you can get filenames from:

  • file
  • database
  • os.listdir('newsletter')
  • glob.glob('newsletter/*.png')
  • command line - python.exe script.py name1 name2 and filenames = sys.argv[1:]

Eventually you can create list with tuples (filename, content_id)

In list you can use string "<image4>" or you can use string "image4" and later use f-string to add < > - f"<{content_id}>".

filenames = [
('newsletter/bar_chart.png', 'bar_chart'),
('newsletter/scatter_plot.png', 'scatter_plot'),
('newsletter/image3.png', 'image3'),
('newsletter/image4.png', 'image4'),
]

for name, content_id in filenames:
fp = open(name, 'rb')
msg_image = MIMEImage(fp.read()) # PEP8: `lower_case_names` for variables
fp.close()
msg_image.add_header('Content-ID', f'<{content_id}>')
msg_root.attach(msg_image)

Eventually you could use name like newsletter/bar_chart.png to create content_id.

content_id = name.split('/')[-1].split('.')[0]  

It gives bar_chart


PEP 8 -- Style Guide for Python Code

Sending multiple files in discord Embed - discord.py

Explanation

This can be accomplished by making a list of discord.File objects, then passing it as Messageable.send(files=your_list_of_files).

I've included code below which opens a list of file objects based on their filenames.

Code
files_to_read: list[str] = []
files_to_send: list[discord.File] = []
for filename in files_to_read:
with open(filename, 'rb') as f: # discord file objects must be opened in binary and read mode
files_to_send.append(discord.File(f))
await {Messageable}.send(files=files_to_send)
Reference

discord.File

Messageable.send



Related Topics



Leave a reply



Submit