简体   繁体   English

Discord.JS v14 AttachmentBuilder 图像未在 Embed 中显示

[英]Discord.JS v14 AttachmentBuilder image not showing up in Embed

So, I'm having a bit of trouble with getting an image made from Canvas to display itself within an Embed.因此,我在获取由 Canvas 制作的图像以在 Embed 中显示自身时遇到了一些麻烦。 I'm not too sure what's going on here, but if I include the attachment as an attachment to the message it seems to show up fine.我不太确定这里发生了什么,但如果我将attachment作为邮件的附件包含在内,它似乎显示正常。 However, when trying to include it in the embed itself, it doesn't throw any errors and doesn't show up in the Embed sent to the channel.但是,当尝试将其包含在嵌入本身中时,它不会引发任何错误,也不会出现在发送到频道的嵌入中。

I've included relevant code below.我在下面包含了相关代码。 Maybe, someone can help me here?也许,有人可以在这里帮助我吗? Much thanks in advance.非常感谢。

    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] })

You should make sure the files array is filled with your attachment to make it work:您应该确保 files 数组中充满了您的附件以使其正常工作:

    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] })

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM