首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >在webhook嵌入中使用base64图像的discordjs

在webhook嵌入中使用base64图像的discordjs
EN

Stack Overflow用户
提问于 2020-11-14 22:27:59
回答 2查看 1.4K关注 0票数 0

如何使用webhook将图像插入到不一致嵌入中。我将图像保存为base64字符串,这是我从数据库中获得的。我已经试过了,但是我只能得到一个空的嵌入

代码语言:javascript
运行
复制
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],
});
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2020-12-06 01:36:44

我尝试使用较小的图像,问题中的代码起作用了。所以这是一个请求大小的问题。我通过设置快速路由来提供图像,并在嵌入中使用URL来修复它

代码语言:javascript
运行
复制
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)
});
代码语言:javascript
运行
复制
const embed = new Discord.MessageEmbed()
    .setImage(`${base_url}/img/thumb/${imgId}`)
    
webhookClient.send('', {
    username: userName,
    embeds: [embed],
});
票数 0
EN

Stack Overflow用户

发布于 2021-05-16 18:19:32

输入代码hereHoping,以便尽快与您联系。Discord增加了一个功能(或者它已经存在了,我不知道),它允许你做你想做的事情。

代码语言:javascript
运行
复制
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],
});
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/64834944

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档