【问题标题】:Discord.js: interaction has already been acknowledged. Discord ButtonsDiscord.js:交互已经被确认。不和谐按钮
【发布时间】:2022-06-24 12:18:20
【问题描述】:

我正在尝试为我的 Discord 机器人创建 Poker 命令,并且我想使用 Discord 按钮实现轮流系统。现在的命令是:

  1. 有人使用命令
  2. 机器人发送一个嵌入按钮以加入
  3. 如果按下加入,播放器元素将被推入播放器数组
  4. 如果点击开始比赛,机器人会在 dm 中发送卡片
  5. 然后机器人会询问每个玩家他们想按顺序做什么
  6. 如果玩家选择,机器人会崩溃并向我发送此错误:DiscordAPIError: Interaction has already been acknowledged.

我不知道是什么导致了问题。代码如下:

const players = [new Player(interaction.user.id, interaction.user.username)];

const hasJoined = [interaction.user];

const playerRow = new Discord.MessageActionRow().addComponents(
    new Discord.MessageButton().setCustomId("join").setLabel("Join").setStyle("SUCCESS"),
    new Discord.MessageButton().setCustomId("start").setLabel("Start Game").setStyle("SUCCESS")
);

const playerEmbed = new Discord.MessageEmbed()
.setTitle(`${interaction.user.username} started a game of Poker Texas hold'em! \nClick the button if you wanna join!`)
.setAuthor({ name: `${interaction.user.username}`, iconURL: interaction.user.displayAvatarURL({ format: "png"})})
.setDescription(`**players:** \n${codeLine(players.map(a => a.name).join("\n"))}`)

interaction.reply({ embeds: [playerEmbed], components: [playerRow] });

const collector =  interaction.channel.createMessageComponentCollector({ time: 90000 });

collector.on("collect", async (i) => {

    await i.deferUpdate();

    if (i.customId == "join") {

        //if (hasJoined.includes(i.user)) return i.editReply(`You are already in game ${i.user}!`);

        players.push(new Player(i.user.id, i.user.username));
        hasJoined.push(i.user);
    
        playerEmbed.setDescription(`**Players:** \n${codeLine(hasJoined.map(a => a.username).join("\n"))}`);

        interaction.editReply({ embeds: [playerEmbed], components: [playerRow] });

        if (hasJoined.length == 8) playerRow.components[0].setDisabled(true);
    }

    if (i.customId == "start") collector.stop();
});

collector.on("end", async () => {

    for (let i = 0; i < players.length; i++) {

        const rcard1 = chance.pickone(deck);
        deck.splice(deck.indexOf(rcard1), 1);
        const rcard2 = chance.pickone(deck);
        deck.splice(deck.indexOf(rcard2), 1);
        
        players[i].card1 = rcard1;
        players[i].card2 = rcard2;

        client.users.fetch(players[i].id).then((user) => {
            user.send(`here you are ${players[i].name}! These are your cards: ${players[i].card1.emoji} ${players[i].card2.emoji}.`);
        });
    }

    const matchRow = new Discord.MessageActionRow().addComponents(
        new Discord.MessageButton().setCustomId("stand").setLabel("Stand").setStyle("SECONDARY"),
        new Discord.MessageButton().setCustomId("double").setLabel("Double").setStyle("SECONDARY"),
        new Discord.MessageButton().setCustomId("fold").setLabel("Fold").setStyle("DANGER")
    );

    const matchEmbed = new Discord.MessageEmbed()
    .setTitle("**Texas hold'em!**")
    .setDescription(`The Small Blind is ${codeLine(players[0].name)} and they bet ${codeLine(bet)} bananas!
    The Large Blind is ${codeLine(players[1].name)} and they double! So ${codeLine(bet * 2)} bananas!`);

    await interaction.editReply({ embeds: [matchEmbed], components: [matchRow] });

    for (let i = 0; i < players.length; i++) {

        const playerFilter = (pInt) => { return pInt.user.id == players[i].id}
        const matchCollector = interaction.channel.createMessageComponentCollector({ playerFilter, time: 90000 });
        
        matchCollector.on("collect", async (int) => {

            await int.deferUpdate();

            if (int.customId == "fold") {

                matchEmbed.setDescription(`${codeLine(players[i].name)} folded!`);

                players.splice(players[i], 1);

            }

            int.editReply({ embeds: [matchEmbed], components: [matchRow], });
        });
    }
});

【问题讨论】:

  • CustomID 应该是真正的自定义 ID,否则会出现这种错误。 ID 应该是唯一的。
  • 对不起,我没明白你的意思,我让它们都不同,所以我不明白 xD

标签: javascript node.js discord.js poker


【解决方案1】:

matchCollector 中的 int 对象是 ButtonInteraction。因此,您无法编辑回复,因为您没有回复。相反,如果要编辑 Button 所在的消息,则需要使用 ButtonInteraction#update 方法:

int.update({ embeds: [matchEmbed], components: [matchRow] });

【讨论】:

    【解决方案2】:

    您应该添加一个诊断不和谐 api 错误的函数。将此添加到“index.js”文件的任何位置。

    process.on("unhandledRejection", async (err) => {
      console.error("Unhandled Promise Rejection:\n", err);
    });
    process.on("uncaughtException", async (err) => {
      console.error("Uncaught Promise Exception:\n", err);
    });
    process.on("uncaughtExceptionMonitor", async (err) => {
      console.error("Uncaught Promise Exception (Monitor):\n", err);
    });
    process.on("multipleResolves", async (type, promise, reason) => {
      console.error("Multiple Resolves:\n", type, promise, reason);
    });

    【讨论】:

      【解决方案3】:

      您正在推迟互动的更新,您应该推迟互动的回复

      matchCollector.on("collect", async (int) => {
          await int.deferReply();
      
          if (int.customId == "fold") {
              matchEmbed.setDescription(`${codeLine(players[i].name)} folded!`);
              players.splice(players[i], 1);
          }
      
          int.editReply({ embeds: [matchEmbed], components: [matchRow], });
      });
      

      【讨论】:

        猜你喜欢
        • 2022-12-04
        • 2022-11-03
        • 2022-12-18
        • 2023-01-31
        • 2021-12-02
        • 1970-01-01
        • 1970-01-01
        • 2022-08-14
        • 1970-01-01
        相关资源
        最近更新 更多