Skip to content Skip to sidebar Skip to footer

Discordjs Using Base64 Image In Webhook Embed

How do I insert an image into a discord embed using webhook. I have the image saved as a base64 string which I get from database. I have tried this but I only get an empty embed c

Solution 1:

enter code hereHoping to get back to you soon enough. Discord added a function (or it was already present I don’t know), which allows you to do what you want.

const data = b64image.split(',')[1]; 
const buf = newBuffer.from(data, 'base64');
const file = newDiscord.MessageAttachment(buf, 'img.jpeg');

const embed = newDiscord.MessageEmbed()
    .attachFiles(file)
    .setImage('attachment://img.jpeg')

webhookClient.send('', {
    username: userName,
    embeds: [embed],
});

Solution 2:

I tried with a smaller image, and the code in the question worked. So it was a problem with the size of the request. I fixed it by setting up an express route to serve images and used the URL in the embed

router.get('/thumb/:imgId', (req, res) => {
    const imgId = req.params.imgId.toString().trim();
    let file = Buffer.from(b64Image.split(',')[1], 'base64')
    res.status(200);
    res.set('Content-Type', 'image/jpeg');
    res.set('Content-Length', file.length)
    res.send(file)
});
const embed = newDiscord.MessageEmbed()
    .setImage(`${base_url}/img/thumb/${imgId}`)
    
webhookClient.send('', {
    username: userName,
    embeds: [embed],
});

Post a Comment for "Discordjs Using Base64 Image In Webhook Embed"