discordjs using base64 image in webhook embed

吃可爱长大的小学妹 提交于 2021-02-11 14:18:36

问题


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

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

const embed = new Discord.MessageEmbed()
    .setImage('attachment://img.jpeg')

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

回答1:


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 = new Discord.MessageEmbed()
    .setImage(`${base_url}/img/thumb/${imgId}`)
    
webhookClient.send('', {
    username: userName,
    embeds: [embed],
});


来源:https://stackoverflow.com/questions/64834944/discordjs-using-base64-image-in-webhook-embed

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!