在webhook中使用base64图像的discordjs嵌入 [英] discordjs using base64 image in webhook embed

查看:30
本文介绍了在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],
});

推荐答案

我尝试使用较小的图像,问题中的代码起作用了.因此,这是请求大小的问题.我通过设置一条投放图片的快速路线并在嵌入中使用了网址来解决了该问题

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嵌入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆