Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

115
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!