title: message listener in loop discord.js
body: <p>I'm coding a bot using Discord.js and in a loop in my code the callback function for the "message" event is not triggered inside it but works everywhere else during the execution. I made tests using console.log instructions but can't find the solution.</p>
  
  <p>Here's the code : the loop is at the bottom of this block</p>
  
  <pre><code>module.exports.play = async (client, channel, players) =&gt; {
      try {
          let playersCards = [];
          let pyramideCards = [];
  
          emitter.on("ok", message =&gt; {
              console.log(message.author.username + " : " + message.content);
          });
          stopEmitter.on("stop", async () =&gt; {
              await channel.delete();
              console.log("jeu fini");
          });
  
          await beginMessage(players, channel);
          await beginGame(players, playersCards, pyramideCards);
  
          //attendre que les messages se suppriment
          await sleep(30000);
          for(let i = 0; i &lt; pyramideCards.length; i++)
              await continueGame(pyramideCards[i], i, channel, players);
      }
      catch(e) {
          console.error(e);
      }    
  };
  
  async function beginMessage(players, channel) {
      let message = "que le jeu commence ";
      players.forEach(player =&gt; {
          message += `${player.toString()} `;
      });
  
      await removeSendingMessages(players, channel);
      await channel.send(message);
  }
  
  async function beginGame(players, playersCards, pyramideCards) {
      return new Promise(async (resolve, reject) =&gt; {
          fs.readFile('Utils/cartes.json', 'utf8', async (err, data) =&gt; {
              if(err) return reject(err);
  
              let cartes = JSON.parse(data);
              cartes = cartes.cartes;
  
              await asyncForEach(players, async player =&gt; {
                  let playerCards = await distributeCards(player, cartes);
                  playersCards.push(playerCards);
              });
  
              distributePyramideCards(pyramideCards, cartes);
  
  
              if(pyramideCards.length &gt; 0 &amp;&amp; playersCards.length &gt; 0)
                  resolve();
          });
      });
  }
  
  async function distributeCards(player, cartes) {
      let playerCards = { 
          name : player.username,
          cards : [],
      };
  
      for(let i = 0; i &lt; 4; i++) {
          let carte = getRandomCard(cartes);
          playerCards.cards.push(carte.name);
          playerCards.value = carte.value;
      }
      playerCards.cards.sort((a, b) =&gt; a - b);
  
      let dmChannel = await player.createDM();
      let messageSend = await dmChannel.send(`Voici tes cartes retiens les bien tu as 30sec : ${playerCards.cards}`);
      let deletedMessage = setTimeout(async () =&gt; {await messageSend.delete()},30000);
  
      return playerCards; 
  }
  
  function distributePyramideCards(pyramideCards, cartes, base = 4) {
      for(let i = base; i &gt; 0; i--) {
          pyramideCards[pyramideCards.length] = [];
          for(let j = 0; j &lt; i; j++) {
              pyramideCards[pyramideCards.length-1].push(getRandomCard(cartes));
          }
      }
  }
  
  function getRandomCard(cartes) {
      let carte = cartes[Maths.getRandomNumber(0,12)];
      let valid = carte.count &gt; 0;
  
      while(!valid) {
          carte = cartes[Maths.getRandomNumber(0,12)];
      }
  
      carte.count--;
      return carte;
  }
  
  async function asyncForEach(array, callback) {
      for (let index = 0; index &lt; array.length; index++) {
        await callback(array[index], index, array);
      }
  }
  
  async function continueGame(rangee, numRangee, channel, players) {
      let voteMess = "";
      for(let i = 0; i &lt; players.length; i++) {
          voteMess += `pour attaquer ${players[i].username} envoie ${i+1}\n`;
      }
  
      await channel.send(new Discord.MessageEmbed()
          .setColor("GREY")
          .addFields({
              name : "règles",
              value : `rangée ${numRangee + 1} : ${numRangee + 1} coups en jeu`,
              inline : true
          },
          {
              name : "joueurs",
              value : voteMess
          })
      );
  
      for(let i = 0; i &lt; rangee.length; i++)
          await playRound(rangee[i], channel, players);
  }
  
  async function playRound(carte, channel, players) {
      await channel.send(new Discord.MessageEmbed()
          .setTitle("prochaine carte")
          .setColor("RED")
          .addField("carte", carte.name)
      );
  
      let playersCopy = [];
      players.forEach(player =&gt; {
          playersCopy.push({
              name : player.username,
              ok : false
          });
      });
  
      await attack(carte, channel, players, playersCopy);
  }
  
  async function attack(carte, channel, players, playersCopy) {
      let valid = playersCopy.every(player =&gt; player.ok == true);
      await addSendingMessages(players, channel);
      let me = await channel.send("phase d'attaque quand vous avez fini envoyer 'ok'");
  
      while(!valid) {
          // !!!!!! in this loop !!!!
          let valid = playersCopy.every(player =&gt; player.ok == true);
      }
  }
  </code></pre>
  
  <p>and here's the callback function 
  <a href="https://i.stack.imgur.com/rWy82.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/rWy82.png" alt="enter image description here"></a></p>