我的discord机器人出了点问题.我希望,如果这是可能的话,图片正在嵌入式显示.目前,这张图片就像是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!",
                    });
                }
            }
        }
    );
}

推荐答案

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

我用//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!",
                    });
                }
            }
        }
    );
}

Javascript相关问答推荐

为什么我达到了时间限制!?LeetCode链接列表循环(已解决,但需要解释!)

在JavaScript中对大型级联数组进行切片的最有效方法?

一次仅播放一个音频

为什么从liveWire info js代码传递数组我出现错误?

Cypress -使用commands.js将数据测试id串在一起失败,但在将它们串在一起时不使用命令有效

在nextjs服务器端api调用中传递认证凭证

将现场录音发送到后端

Promise Chain中的第二个useState不更新

在拖放时阻止文件打开

显示图—如何在图例项上添加删除线效果?

用于编辑CSS样式的Java脚本

我怎么在JS里连续加2个骰子的和呢?

使用Ace编辑器对子组件实例的native-element 进行Angular 获取时面临的问题

第一项杀死下一项,直到数组长度在javascript中等于1

无法重定向到Next.js中的动态URL

如何组合Multer上传?

如何使用抽屉屏幕及其子屏幕/组件的上下文?

如果我的列有条件,我如何呈现图标?

如何使用Reaction路由导航测试挂钩?

浮动标签效果移除时,所需的也被移除