Skip to content Skip to sidebar Skip to footer

Message Collector In While Loop

I am trying to make a discord bot that has a 'number guessing game' feature. When I run the bot, it gives me this error: FATAL ERROR: Ineffective mark-compacts near heap limit All

Solution 1:

the reason is cause the while loop runs really fast. While the messages come in slow. So by the time 5 "guesses" come, the loop could've ran a million times. therby making a million collectors as well.

try this:

functiongetGuesses(numberOfGuesses,maxGuesses){
const collector = newDiscord.MessageCollector(message.channel, m => m.author.id === message.author.id, { time: 10000, max: 1 });
    collector.on('collect', message => {
        if (guess.length == 0 || ! guessInRange(guess)) {
            message.channel.send('please say 1~100');                
        } else {
            if (parseInt(guess) == secretNumber) {
                message.channel.send('Boom! Correct!');
                return collector.stop();
            } elseif (parseInt(guess) < secretNumber) {
                numberOfGuesses++;
                message.channel.send('Too small');
                collector.stop();
            } else {
                numberOfGuesses++;
                message.channel.send('Too big');
                collector.stop();

            }
            if (numberOfGuesses == maxGuesses) {
                collector.stop();
                return message.channel.send('Game over');
            }
        }   
    });

    //Repeat if guesses still exist
    collector.on('end', collected => {
        if(numberOfGuessed < maxGuesses) getGuesses(numberOfGuesses,maxGuesses)
    });
};

getGuesses(0,5);

Post a Comment for "Message Collector In While Loop"