Discord 嵌入图片

问题描述 投票:0回答:1

我的不和谐机器人有问题。如果可能的话,我希望图片能够显示在嵌入中。目前,该图像就像 bing 在嵌入之外作为自己的消息发布。

这是我的代码,也许有人可以帮我解决这个问题吗?

function fight(interaction, pool, swords) {
    const id = interaction.member.user.id;
    const random_sword = swords[Math.floor(Math.random() * swords.length)];
    const sword_name = random_sword.name;
    const sword_database = random_sword.database;
    const file = new AttachmentBuilder("/home/user/win.jpg");

    pool.query(
        "UPDATE `inv` SET ?? = ?? + 1, `fight` = NOW() WHERE `id` = ? AND (`fight` < DATE_SUB(NOW(), INTERVAL 4 HOUR) or `fight` is null);",
        [sword_database, sword_database, id],
        (err, result) => {
            if (err) {
                console.log(err);
                return interaction.reply({
                    content: "Database error!",
                    ephemeral: true,
                });
            } else {
                if (!(result.changedRows === 0)) {
                    const embed = new EmbedBuilder()
                        .setTitle("Win!")
                        .setDescription(`Congrats you won!\nYou grabbed a ${sword_name}!`)
                        .setColor("#00ff00")
                        .setAuthor({
                            name: interaction.member.user.username,
                            iconURL: interaction.member.user.avatarURL(),
                        })
                        .setTimestamp();

                    return interaction.reply({
                        embeds: [embed],
                        files: [file],
                    });
                } else {
                    return interaction.reply({
                        content: "You already fought in the last 4 hours!",
                    });
                }
            }
        }
    );
}
javascript mysql node.js discord
1个回答
0
投票

要在嵌入中展示图像,您必须指定附件路径并将其分配给嵌入中的图像属性。

我用 //new 标记了更改的行

function fight(interaction, pool, swords) {
    const id = interaction.member.user.id;
    const random_sword = swords[Math.floor(Math.random() * swords.length)];
    const sword_name = random_sword.name;
    const sword_database = random_sword.database;
    const file = new AttachmentBuilder("/home/user/win.jpg");
    const path = "attachment://win.jpg"; //new

    pool.query(
        "UPDATE `inv` SET ?? = ?? + 1, `fight` = NOW() WHERE `id` = ? AND (`fight` < DATE_SUB(NOW(), INTERVAL 4 HOUR) or `fight` is null);",
        [sword_database, sword_database, id],
        (err, result) => {
            if (err) {
                console.log(err);
                return interaction.reply({
                    content: "Database error!",
                    ephemeral: true,
                });
            } else {
                if (!(result.changedRows === 0)) {
                    const embed = new EmbedBuilder()
                        .setTitle("Win!")
                        .setDescription(`Congrats you won!\nYou grabbed a ${sword_name}!`)
                        .setColor("#00ff00")
                        .setImage(path) //new
                        .setAuthor({
                            name: interaction.member.user.username,
                            iconURL: interaction.member.user.avatarURL(),
                        })
                        .setTimestamp();

                    return interaction.reply({
                        embeds: [embed],
                        files: [file],
                    });
                } else {
                    return interaction.reply({
                        content: "You already fought in the last 4 hours!",
                    });
                }
            }
        }
    );
}
© www.soinside.com 2019 - 2024. All rights reserved.