If the user does not interact with a button within a certain time, do it for them discord.js

Viewed 25

i'm trying to make a multiplayer Blackjack command in my Discord bot. It works like this:

  1. When the command starts, it collects everyone who wants to join the table and play.
  2. When playing, it's the turn of the first user in an array, and when the turn ends, i do a .shift() on the array.
  3. When a turn ends and the array length is 1, the ButtonCollector ends and lets the dealer play.

The problem is, if someone does not interact with the button while it's their turn, the others can't play! And if i set the idle property to let's say 1 min, after one min, if the collector is inactive, it will ends, making the dealer play, without letting the remaining player make their moves. I tried in various ways, such as making the player stand with as setTimeout, but it does not work and i don't know how to implement this for every player! I want that if the player timeouts the stand button is played and executes the code inside the cl.on("end") event, or something similar. Any help is appreciated. Thank you!
code:

 let ebd = misc.Embed({
        title: `It's ${joinedName[0]}'s turn!`,
        fields: misc.displayHandsBJ(joinedName, pHands, dHand),
      });
      await botMsg.edit({
        embeds: [ebd],
        components: [row],
      });
      let i = 0;
      let cl = msg.channel.createMessageComponentCollector({
        filter: filters.blackjack([hID, sID, dID], joined),
        idle: 1.8e5,
        dispose: true,
      });
      cl.on("collect", async btnInt => {
        await btnInt.deferUpdate();
        if (btnInt.customId !== sID) {
          if (btnInt.customId === hID) {
            row.components[2].setDisabled(true);
          }
          misc.draw(deck, pHands[i]);
          ebd.setFields(misc.displayHandsBJ(joinedName, pHands, dHand));
          let sum = math.sum(pHands[i].map(c => c.value));
          if (sum >= 21) {
            if (joined.length == 1) {
              cl.stop();
            } else {
              joined.shift();
              i++;
              ebd.setTitle(`It's ${joinedName[i]}'s turn!`);
              row.components[2].setDisabled(false);
            }
          }
        }
        await botMsg.edit({
          embeds: [ebd],
          components: [row],
        });
      });
      cl.on("end", async coll => {
        console.log("dealer turn");
      });

this is just a portion of the code, but it should be enough. If you need the full command code, here is the pastebin.

0 Answers
Related