discordjs using base64 image in webhook embed(在 webhook 嵌入中使用 base64 图像的 discordjs)
问题描述
如何使用 webhook 将图像插入到不和谐嵌入中.我将图像保存为从数据库中获取的 base64 字符串.我试过这个但是我只得到一个空嵌入
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],
});
推荐答案
我尝试使用较小的图像,问题中的代码有效.所以这是请求大小的问题.我通过设置一个快速路由来提供图像并使用嵌入中的 URL 来修复它
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],
});
这篇关于在 webhook 嵌入中使用 base64 图像的 discordjs的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:在 webhook 嵌入中使用 base64 图像的 discordjs
- addEventListener 在 IE 11 中不起作用 2022-01-01
- Quasar 2+Apollo:错误:找不到ID为默认的Apollo客户端。如果您在组件设置之外,请使用ProvideApolloClient() 2022-01-01
- 如何使用 JSON 格式的 jQuery AJAX 从 .cfm 页面输出查 2022-01-01
- 失败的 Canvas 360 jquery 插件 2022-01-01
- 400或500级别的HTTP响应 2022-01-01
- Fetch API 如何获取响应体? 2022-01-01
- Flexslider 箭头未正确显示 2022-01-01
- Css:将嵌套元素定位在父元素边界之外一点 2022-09-07
- 使用RSelum从网站(报纸档案)中抓取多个网页 2022-09-06
- CSS媒体查询(最大高度)不起作用,但为什么? 2022-01-01