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:
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);
});
}
}