Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

128
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda