繁体   English   中英

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

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

因此,我在获取由 Canvas 制作的图像以在 Embed 中显示自身时遇到了一些麻烦。 我不太确定这里发生了什么,但如果我将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] })

您应该确保 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