admin管理员组文章数量:1431426
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],
});
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],
});
Share
Improve this question
asked Nov 14, 2020 at 14:27
Shantanu AryanShantanu Aryan
1665 silver badges12 bronze badges
2 Answers
Reset to default 4Enter the code here. Discord has added a feature (or it was already there, I don't know), which enables you to do what you want to do.
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()
.attachFiles(file)
.setImage('attachment://img.jpeg')
webhookClient.send('', {
username: userName,
embeds: [embed],
});
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],
});
本文标签: javascriptdiscordjs using base64 image in webhook embedStack Overflow
版权声明:本文标题:javascript - discordjs using base64 image in webhook embed - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745571901a2664096.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论