You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Discord.JS v14:Canvas生成图片无法在Embed中显示求助

问题解决:Discord Embed中无法显示Canvas生成的附件图片

你的问题核心是没有将AttachmentBuilder创建的附件添加到消息的附件列表中。虽然Embed里用attachment://${attachment.name}引用了图片,但Discord需要同时收到附件文件才能在Embed中渲染它。

修改后的代码:

const canvas = Canvas.createCanvas(2000, 2000);
const context = canvas.getContext('2d');
const background = await Canvas.loadImage('redacted-link');

context.drawImage(background, 0, 0, canvas.width, canvas.height);

context.font = '211px sans-serif';
context.fillStyle = '#ffc22b';

context.fillText(`${prodsize}`, 230 / 2, 660 / 2);
context.fillText(`${prodrate}m`, 170 / 2, 1338 / 2);

if (landsize.length === 2) {
    context.fillText(`${landsize}`, 3175 / 2, 660 / 2);
} else if (landsize.length === 3) {
    context.fillText(`${landsize}`, 3050 / 2, 660 / 2);
}

const attachment = new AttachmentBuilder(await canvas.encode('png'), { name: 'random.png' });

const embed = new EmbedBuilder()
    .setTitle(`UI Example`)
    .setImage(`attachment://${attachment.name}`);

// 关键:在editReply中同时传入embeds和attachments参数
interaction.editReply({ embeds: [embed], attachments: [attachment] });

关键说明:

  • Discord的Embed引用本地附件时,必须将附件对象包含在消息的attachments数组中,否则attachment://路径无法被解析。
  • 直接发送附件时默认会把附件加入消息列表,所以能正常显示;但Embed是单独的结构,必须显式关联附件。

内容的提问来源于stack exchange,提问作者Jason H - Wizard

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.07 13:15:33