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

101
Views
How to collect multiple message in a loop using awaitMessage?

Im trying to create a game command in which players need to guess where the cat is among a number of boxes. The basic concept was done but I somehow can't collect more than 1 message, it would stop the process after collecting the first one

The code:

const Command = require("../structures/Command.js");
module.exports = new Command({
    name: "cat",
    async run(message, args, client) {
        //Preparing stuff
        var i = 0;
        var cat = Math.floor(Math.random()*5)+1;  
        message.channel.send("A wild cat have appeared and hiden in 5 random boxes, can you find it?")
        const filter = (user) => {
            return user.author.id === message.author.id 
        };
        //Loop
        while (i<10) {
            console.log(cat);
            let collected = await message.channel.awaitMessages(filter, { max: 1, time: 15000, errors: ['time'] });
            answer = collected.first().content;
            console.log(answer)
            //checking
            if (answer === cat){
                message.reply("You opened the correct box!");
                var i = 727;
                console.log("Yes")
            } else {
                var move  = Math.floor(Math.random()*10)+1;
                if (cat===1){var move = 1} else if (cat===5){var move = 10}
                if (move<5){var cat = cat+1} else {var cat = cat-1}
                var i = i + 1;
                var days = 10 - cat;
                console.log(cat)
                message.channel.send(`You didn't guessed the correct box, the cat has now moved to another box. You got ${days} tries left`)
            }
            
        } 

    }
});
about 4 years ago · Juan Pablo Isaza
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!