Discord.js v14 斜杠命令中的临时回复未按预期运行

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

用户 我目前正在开发一个 Discord.js 14.14 机器人,专注于 Slash 命令和交互。我遇到了有关临时消息行为的问题,特别是在涉及延迟回复和后续后续消息的情况下。

在我的代码中,艺术家嵌入和专辑嵌入在我的代码中回复不应该是短暂的。但是,当用户尝试获取未定义或不存在的用户(导致 404 错误)时,对此的响应应该是短暂的。 不幸的是,将 ephemeral 设置为 true 或 false 似乎没有任何效果,或者至少它没有按照我的预期运行。

我在交互执行开始时设置了

interaction.deferReply();
,以避免超时,因为函数可能需要更长的时间来执行。

这是我的代码有问题的部分:

try {
    // ... functions irrelevant to my problem
    await interaction.editReply({
      embeds: [artistsEmbed],
      ephemeral: false,
    });
    await interaction.followUp({
      embeds: [albumsEmbed],
      ephemeral: false,
    });
} catch (error) {
    console.error("Error:", error.message);

    let responseContent;

    if (error.isAxiosError && error.response && error.response.status === 404) {
      const notFoundUser = error.config.url.split("user=")\[1\].split("&")\[0\];
      responseContent = `User ${notFoundUser} not found.`;
    } else {
      responseContent = "Error occurred. Please try again later.";
    }

    await interaction.editReply({
      content: responseContent,
      ephemeral: true,
    });

// debug
    await interaction.followUp({
      content: "Test follow-up, should be ephemeral.",
      ephemeral: true,
    });
}

按照我的方法,错误回复作为非临时回复发送,这是不正确的。然而,我添加的调试后续是短暂的。

如果我将延迟设置为顶部的

interaction.deferReply({ ephemeral: true });
,错误消息将作为临时发送,这是正确的。 但是第一个常规响应也将是短暂的,即使它被设置为 ephemeral: false。在这种情况下,第一个常规响应是 [artistsEmbed] 响应。

我想知道我在执行的最顶端推迟回复的方法是否不正确。我是否应该以某种方式分别为有错误的情况(

deferReply({ ephemeral: true })
)和没有问题的情况(
deferReply({ ephemeral: false })
)定义它?但我还不确定如何做到这一点,我希望有一种更好的方法,允许为每个回复/后续单独设置短暂状态。

我有点迷失,希望对此事有任何帮助。

javascript discord discord.js
1个回答
0
投票

传递到

interaction.editReply
方法的选项与
interaction.followUp
不同,不接受
ephemeral
属性,如您在 here 中看到的,因此在
editReply
上设置此属性没有任何效果。

这就是我的处理方式:

  • 我会推迟接收到的交互,并将

    ephemeral
    属性设置为
    true

  • 如果请求成功解决,我将编辑收到的交互,其中包含表示成功响应的消息,然后对其进行

    followUp
    ,并将
    ephemeral
    属性设置为
    false
    ,其中包含实际的相关数据。 请注意,您必须先
    editReply
    ,然后再
    followUp

  • 如果请求成功解决,我只会

    editReply
    原来收到的交互。

        try {
            // First defer the received interaction as an ephemeral.
            await interaction.deferReply({
                ephemeral: true,
            });

            // (Processing)

            // If we made it here, it means the request was successfully resolved.
            await interaction.editReply({
                embeds: [/* Something representing the a successful request. */]
            });

            await interaction.followUp({
                embeds: [artistsEmbed],
                ephemeral: false,
            });

            await interaction.followUp({
                embeds: [albumsEmbed],
                ephemeral: false,
            });
        } catch (error) {
            console.error("Error:", error.message);
    
            let responseContent;
    
            if (error.isAxiosError && error.response && error.response.status === 404) {
                const notFoundUser = error.config.url.split("user=")\[1\].split("&")\[0\];
                responseContent = `User ${notFoundUser} not found.`;
            } else {
                responseContent = "Error occurred. Please try again later.";
            }
    
            await interaction.editReply({
                content: responseContent,
                // ephemeral: true, // This is removed since it's not doing anything.
            });
        }
© www.soinside.com 2019 - 2024. All rights reserved.