I have a !trivia command which has 4 functions and inside the functions I have a .then()
function and .setTimeout()
. The problem is that once someone triggers the command, it will only stop once someone say '!stop'. How can I make it so it stops if no one typed in that channel within 5 minutes?
Asked
Active
Viewed 238 times
0
-
A quick google will pull up this: https://stackoverflow.com/questions/14249506/how-can-i-wait-in-node-js-javascript-l-need-to-pause-for-a-period-of-time I would suggest using an asynchronous function or your bot will stop, but it should otherwise be the same. http://idownvotedbecau.se/noresearch/ – Tom Martin Nov 09 '18 at 18:06
1 Answers
0
Use .awaitMessages()
let userUsingTheCommand = "<USER_ID>";
let filter = msg => msg.author.id = userUsingTheCommand;
// Errors: ['time'] treats ending because of the time limit as an error
function wait() {
channel.awaitMessages(filter, {
max: 1,
time: 60000 * 5,
errors: ['time']
})
.then(wait)
.catch(() => {
console.log("Time to stop!");
});
}

Federico Grandi
- 6,785
- 5
- 30
- 50

Frustrated programmer
- 680
- 8
- 24
-
And if i don't want someone else to use that command while a trivia is going on, can i do the same? – Voropoulo Nov 09 '18 at 22:33
-
Thats what `filter` does. It checks if `msg.author.id` equals to `userUsingTheCommand`. When the user starts the trivia, save their ID to `userUsingTheCommand` – Frustrated programmer Nov 09 '18 at 23:59
-
I added your code in a totally new file just to check it out.it didn't really print someone in the console. Did i do something wrong? – Voropoulo Nov 10 '18 at 00:15