Interaction also replying for other users in Discord Js v14

Viewed 24

I have a problem with slash commands. Indeed, I'm coding a Quiz bot where users can answer a question thanks to 4 buttons. When the user answers correctly, I use interaction.update() to display the next question (so the next embed).

Unfortunately, when two or more users play at the same time, especially when users answer at the same time, the next embed is displayed for each player as well.

For example, if user1 answers question 2 and user2 answers question 5 at about the same time, user1 will not go to question 3 but to question 4.

For me, the error only occurs because of the interaction collector, but it seems to be configured correctly.

I'm giving you parts of my code where the error might come from (for me), feel free to ask for more if needed.

Thanks.

const filter = (i) => (i.customId == "0" || i.customId == "1" || i.customId == "2" || i.customId == "3" || i.customId == "startQuiz" || i.customId == "stopQuiz") && i.user.id == interaction.user.id;
// 0 = Answer 1, 1 = Answer 2...
const collector = interaction.channel.createMessageComponentCollector({ filter });

// here, if Answer A button we check if that's the correct answer
// if it is, we update the interaction with the next embed
// if it isn't, we do the same but we're not increasing the correct answer counter

if (i.customId == "0") {
    switch (actualQuestion) {
      case "q1":
        if (q1["correct"] == 0) {
          await i.update({ content: "Prochaine question...", components: [row], embeds: [embed2], ephemeral: true });
          actualQuestion = "q2";
          rightAnswer++;
        } else {
          await i.update({ content: "Prochaine question...", components: [row], embeds: [embed2], ephemeral: true });
          actualQuestion = "q2";
        }
        break;
        [………]

Update:

I have a variable called actualQuestion who store as a string the acutal question of the user (like "q3").

Wondering if when one user is passing to the next question and updating actualQuestion variable, this is also updating for every users ?

If it's right, how can I counter this issue ?

(sorry for my english, I'm french)

0 Answers
Related