while循环中的消息收集器 [英] Message collector in while loop

查看:32
本文介绍了while循环中的消息收集器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试制作一个具有猜数字游戏"功能的不和谐机器人.
当我运行机器人时,它给了我这个错误:

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:

致命错误:接近堆限制的无效标记压缩分配失败 - JavaScript 堆内存不足

FATAL ERROR: Ineffective mark-compacts near heap limit Allocation failed - JavaScript heap out of memory

似乎while循环变成了无限循环.这是我的代码(声明了所有的函数和变量)

It seems like the while loop became an infinite loop. This is my code (all the functions and variables are declared)

while (numberOfGuesses < 5) {
  const collector = new Discord.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();
      } else if (parseInt(guess) < secretNumber) {
        numberOfGuesses++;
        message.channel.send('Too small');
        collector.stop();
      } else {
        numberOfGuesses++;
        message.channel.send('Too big');
        collector.stop();

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

推荐答案

原因是 while 循环运行起来非常快.虽然消息进来缓慢.因此,当 5 个猜测"出现时,循环可能已经运行了 100 万次.从而使收藏家也达到了 100 万.

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.

试试这个:

function getGuesses(numberOfGuesses,maxGuesses){
const collector = new Discord.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();
            } else if (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);

这篇关于while循环中的消息收集器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆