R
R
reCAPTCHA2022-02-19 22:43:45
Node.js
reCAPTCHA, 2022-02-19 22:43:45

How to solve discrod.js error with buttons - Interaction has already been acknowledged?

I'm writing a discord bot.
The code structure is like in the docks (each file has a separate command - https://github.com/discordjs/guide/tree/main/code-... )
So, we have the following:
Command file (dungeon.js):

let newDungeon = functions.dungeonGen("right")
        newDungeon[1].push("left")
        await interaction.reply({
            content: `Вы запустили мини-игру: "Подземелье"`,
            components: [functions.dungeonButtons(newDungeon[1])],
            embeds: [functions.dungeonEmbed(newDungeon[0])]
        });


index.js file:
const filter = i => i.user.id === interaction.user.id

  const collector = interaction.channel.createMessageComponentCollector({ filter, time: 60000 })

  collector.on('collect', async i => {
  const ButtonId = i.customId.split(";")
  if (ButtonId[0] === `up` || ButtonId[0] === `left` || ButtonId[0] === `right` || ButtonId[0] === `down`) {
    const newDungeon = functions.dungeonGen(ButtonId[0])
    let text = newDungeon[1].includes("treasure") ? `Пойдя ${ButtonId[1]}, вы нашли сокровища в комнате! :moneybag:` : `Вы пошли ${ButtonId[1]}... В комнате пусто.`
    console.log(i)
    await i.update({
      content: text,
      components: [functions.dungeonButtons(newDungeon[1])],
      embeds: [functions.dungeonEmbed(newDungeon[0])]});
  }
  if(ButtonId[0] === `exit`) {
    await i.update({
      content: "Приключения закончились..",
      embeds: []
    })
    await i.deleteReply();
    collector.stop();
  }


If you write any other commands, and after this dungeon command, then when you click on the button, the bot dies, giving an error:
C:\Users\ADMIN\Desktop\bot\node_modules\discord.js\src\rest\RequestHandler.js:350
      throw new DiscordAPIError(data, res.status, request);
            ^

DiscordAPIError: Interaction has already been acknowledged.
    at RequestHandler.execute (C:\Users\ADMIN\Desktop\bot\node_modules\discord.js\src\rest\RequestHandler.js:350:13)
    at processTicksAndRejections (node:internal/process/task_queues:96:5)
    at async RequestHandler.push (C:\Users\ADMIN\Desktop\bot\node_modules\discord.js\src\rest\RequestHandler.js:51:14)
    at async ButtonInteraction.update (C:\Users\ADMIN\Desktop\bot\node_modules\discord.js\src\structures\interfaces\InteractionResponses.js:214:5)
    at async InteractionCollector.<anonymous> (C:\Users\ADMIN\Desktop\bot\index.js:44:3) {


Line 44 of index.js:
await i.update

Answer the question

In order to leave comments, you need to log in

1 answer(s)
R
reCAPTCHA, 2022-02-20
@koldan_off

Found an error.
To whom it is necessary, then:
I simply shoved this collector into the client.on function with the 'interactionCreate' parameter. Without any checks or something else, which eventually led to the fact that the collector started up from time to time. Fix simple -

const { commandName } = interaction;
  if(commandName == "dungeon") {
       //Код коллектора тут
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question