dischord . js v14 AttachmentBuilder图像未显示在嵌入



所以,我有一点麻烦,从画布制作的图像在嵌入中显示自己。我不太确定这里发生了什么,但是如果我将attachment作为附件包含到消息中,它似乎显示得很好。然而,当试图将它包含在嵌入本身时,它不会抛出任何错误,也不会显示在发送到通道的嵌入中。

我在下面包含了相关的代码。也许有人能帮我一下?非常感谢。

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}`)
interaction.editReply({ embeds: [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}`)
interaction.editReply({ embeds: [embed], files: [attachment] })

最新更新