【问题标题】:Send Canvas in embed Discord.js在嵌入 Discord.js 中发送 Canvas
【发布时间】:2020-10-17 00:10:05
【问题描述】:

我正在尝试在嵌入消息中发送画布图像,但这样做我只能发送照片然后单独嵌入(由于声誉不佳,我无法附加照片 D :)

对不起,英语不是我的母语

    let channel = member.guild.channels.get('720372789286141963')
const Mensaje = {
    color: 1752220,
    title: `<a:pepeShoot:659762885706383380> | **Hola ${member.user.username}!**`,
    description: `**Bienvenid@ a:** \n Psyco RP`,
};

const file = {
    attachment: await createCanvas(),
    name:'bienvenida.jpg'
}
    
        channel.send({embed: Mensaje, files : [file]});
});

【问题讨论】:

    标签: discord discord.js


    【解决方案1】:

    最简单且最强烈推荐的方法是创建图像缓冲区,因为它可以直接放入setImage()。我也强烈推荐使用 embed 构造函数。

    ctx.blahBlahBlah(); //make your image
    
    const attachment = new Discord.Attachment(canvas.toBuffer(), 'image-name-here.png'); //buffer the canvas and pass it into an Attachment constructor
    
    const myEmbed = new Discord.RichEmbed()
    .setTitle(`<a:pepeShoot:659762885706383380> | **Hola ${member.user.username}!**`)
    .setDescription(`**Bienvenid@ a:** \n Psyco RP`)
    .setColor("1752220")
    .setImage(attachment); //this is where we use the buffered image
    
    message.channel.send(myEmbed);
    

    它非常简单直观,希望对您有所帮助!

    【讨论】:

    • 如果我使用你的代码,我会收到错误(node:8408) UnhandledPromiseRejectionWarning: ReferenceError: canvas is not defined
    • @NilMartinez 在 WubzyGD#8766 的 Discord 上与我多聊聊
    【解决方案2】:

    尝试缓冲图像,然后使用.attachFiles(),如下所示:

    const attachment = new Discord.MessageAttachment(canvas.toBuffer(),'bufferedfilename.png'); 
     
    const embed = new Discord.MessageEmbed()
        .attachFiles(attachment)
        .setImage('attachment://bufferedfilename.png');
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-02-20
      • 1970-01-01
      • 2020-10-09
      • 2021-08-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-06-29
      相关资源
      最近更新 更多