Discord.js 收集多组按钮上的多个交互

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

我有一个 Discord.js 机器人,它允许用户对游戏结果进行投票。管理员使用斜杠命令发送一条带有 2 个按钮的消息,代表 2 个可能的投票。通常一次会有不止 1 场比赛可供投票。我正在处理这样的交互:

        interaction.client.on("interactionCreate", async (i) => {
            if (!i.isButton()) return;

            let team; 

            try {
                if (i.customId === 'team1Button' || i.customId === 'team2Button') {
                    // Handle team vote with retry mechanism
                    await retryOperation(async () => {
                        team = await getTeamEmoji(i.message.id, i.customId); // Assign value to team variable
                        await castVote(team.name, i.user.username, i.message.id);
                    }, 3, i.message.id, i.user.username); // Retry team vote operation up to 3 times

                    // Reply to interaction
                    await i.reply({
                        content: `Vote for ${team.emoji} submitted.`,
                        ephemeral: true,
                    });
                }
            } catch (error) {
                console.error("Interaction handling error:", error);
                logError(error, i.message.id, i.user.username, 'Interaction handling error: ' + i);
            }
        });

所以这有效。机器人永远不会崩溃,投票也会通过。但是,我的错误日志最终充满了以下消息的记录:

error
"DiscordAPIError[10062]: Unknown interaction"

error
"DiscordAPIError[40060]: Interaction has already been acknowledged."

但没有更多信息了。我还知道,有些交互第一次失败,然后通过我的

retryOpoertaion
函数进行额外尝试后才起作用,如果您需要查看它,则为:

async function retryOperation(operation, maxRetries, id, username, initialDelay = 1000, maxDelay = 60000) {
    let retries = 0;
    let delay = initialDelay;
    
    while (retries < maxRetries) {
        try {
            await operation();
            return; // Operation succeeded, no need to retry
        } catch (error) {
            console.error(`Error during operation, retrying in ${delay}ms:`, error);
            logError(error, id, username, 'Error while voting for team. Retrying...');
            await new Promise(resolve => setTimeout(resolve, delay));
            delay = Math.min(delay * 2, maxDelay);
            retries++;
        }
    }
    
    throw new Error(`Operation failed after ${retries} retries.`);
}

那么,为什么交互有时会失败呢?我想说的是,当多人同时对多个游戏进行投票时,这种情况发生的频率要高得多,但大多数情况下都是这种情况。什么可能导致该问题?我不太确定如何调试这个。我在 Visual Studio 中本地运行机器人。非常感谢任何帮助。如果您需要任何其他信息,请告诉我。

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

我认为问题出在等待时间超过 3 秒。 Discord 交互必须在 3 秒内响应,否则交互结束并返回错误。

您的

retryOperation()
有 1 秒或更长的延迟,因此在大多数情况下可能高于 3 秒。

您可以通过推迟回复来避免这种情况:

await i.deferReply({ ephemeral: true });

它看起来像这样。请注意,请将您的回复编辑为

editReply
以避免错误

interaction.client.on("interactionCreate", async (i) => {
    if (!i.isButton()) return;

    let team; 

    try {
        if (i.customId === 'team1Button' || i.customId === 'team2Button') {
            await i.deferReply({ ephemeral: true });
            // Handle team vote with retry mechanism
            await retryOperation(async () => {
                team = await getTeamEmoji(i.message.id, i.customId); // Assign value to team variable
                await castVote(team.name, i.user.username, i.message.id);
            }, 3, i.message.id, i.user.username); // Retry team vote operation up to 3 times

            // Reply to interaction
            await i.editReply({
                content: `Vote for ${team.emoji} submitted.`,
                ephemeral: true,
            });
        }
    } catch (error) {
        console.error("Interaction handling error:", error);
        logError(error, i.message.id, i.user.username, 'Interaction handling error: ' + i);
    }
});

对于您的第二个错误,

Interaction has already been acknowledged
这可能来自您的机器人的重复实例。因此,请检查您的机器人是否仅在一个终端(本地或远程)中运行

© www.soinside.com 2019 - 2024. All rights reserved.