Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

117
Vistas
How to listen parallel two users?

I have next code:

module.exports = {
  name: 'd',
  description: 'test',
  execute(client, message, args) {
    const Discord = module.require("discord.js");
    
      message.channel.send("Yes or no?")

      let player1 = '0';
      function firstListen() {
        const collector1 = new Discord.MessageCollector(message.channel, m => m.author.id === 'HERE_ID_PLAYER_1', { time: 10000 });
        collector1.on('collect', message => {
          if (message.content.toLowerCase() == "yes") {
            player1 = 'yes';
            message.channel.send("Player 1 chose YES");
          } else if (message.content.toLowerCase() == "no") {
            player1 = 'no';
            message.channel.send("Player 1 chose NO");
          }
        })
      };

      let player2 = '0';
      function secondListen() {
        const collector2 = new Discord.MessageCollector(message.channel, m => m.author.id === 'HERE_ID_PLAYER_2', { time: 10000 });
        collector2.on('collect', message => {
          if (message.content.toLowerCase() == "yes") {
            player2 = 'yes';
            message.channel.send("Player 2 chose YES");
          } else if (message.content.toLowerCase() == "no") {
            player2 = 'no';
            message.channel.send("Player 2 chose NO");
          }
        })
      };

      async function runListen() {
        message.channel.send('start');
        await firstListen();
        await secondListen();
        await message.channel.send(`Player 1 chose ${player1} and Player 2 chose ${player2}`);
      }

      runListen();

    }
  }

First, I wanna send message from bot "Yes or no?",
then wanna wait until both of users answer "yes" or "no", or until time is up,
and only after that wanna send final result "Player1 chose yes/no and Player2 chose yes/no"
But cant understand how working with .then in js, or how correctly use async/await..(
Cause, Player2 can answer quicker than Player1. Need to listen both of user parallel, and they must have common timer (for ex.: 10sec for both). PS: i have 3 question:

  1. How I can get result from each function after users chose something? (to do something with their choice)
  2. How I can wait until both functions are finish, and only after that send message with results?
  3. How I can catch when time is over? and send "Player1, Time is up" or "Player2, Time is up"
about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

Thanks all! Answer was: need to use promise, and promise.all)

module.exports = {
  name: 'd',
  description: 'test',
  async execute(client, message, args) {
    const Discord = module.require("discord.js");
    message.channel.send("Yes or no?")

    var p1 = new Promise((resolve, reject) => {
      const collector1 = new Discord.MessageCollector(message.channel, m => m.author.id === '568481051895136278', { max: 1, time: 10000 });
      collector1.on('collect', message => {
        if (message.content.toLowerCase() == "yes") {
          resolve('yes');
          message.channel.send("Player 1 chose YES");
        } else if (message.content.toLowerCase() == "no") {
          resolve('no');
          message.channel.send("Player 1 chose NO");
        }
      })
      collector1.on('end', (collected, reason) => {
        if (reason === 'time') {
          resolve('time');
          message.channel.send('Player 1, Time is up!');
        }
      });
    });
    var p2 = new Promise((resolve, reject) => {
      const collector2 = new Discord.MessageCollector(message.channel, m => m.author.id === '568481051895136279', { max: 1, time: 10000 });
      collector2.on('collect', message => {
        if (message.content.toLowerCase() == "yes") {
          resolve('yes');
          message.channel.send("Player 2 chose YES");
        } else if (message.content.toLowerCase() == "no") {
          resolve('no');
          message.channel.send("Player 2 chose NO");
        }
      })
      collector2.on('end', (collected, reason) => {
        if (reason === 'time') {
          resolve('time');
          message.channel.send('Player 2, Time is up!');
        }
      });
    });

    Promise.all([p1, p2]).then(values => {
      console.log(values);
    });

  }
}
about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda