使用node.js创建IRC bot。尽管只调用一次函数,但消息发送两次

时间:2014-04-07 04:41:59

标签: javascript node.js bots irc

我正在使用node.js创建一个询问琐事问题的IRC机器人。到目前为止它工作得很好,但我想通过为每个字母提供下划线来实现一个生成提示的系统。随着时间的推移,它会慢慢填满空白。如果在填写所有空格之前没有人得到答案,那么机器人会继续前进。 (如果不是单问题模式)

问题:如果没有人回答这个问题,那么机器人会按照预期继续前进到下一个问题。然后机器人同时提供两个提示。有时它会重复第一个提示,但有时它实际上满足else语句并提供下一个提示。

我已经排除故障以验证函数getHint仅被称为ONCE。

我现在盯着这段代码将近2.5个小时,我开始失去希望。我是javascript的新手,这基本上是我第一次编写代码。非常感谢任何帮助。

示例1:满意 if (there is no hint) 两次

  

BOT>谁是甲壳虫乐队的鼓手?

     

BOT> _ _ _ _ _ _ _ _ _ _

     

BOT> R _ _ G O S _ A _ R

     

BOT> R _ N G O S T A R R

     

BOT>哦,伙计,没有人得到答案!它是:RINGO STARR

     

BOT>谁是美国总统?

     

BOT> _ _ _ _ _ _ _ _ _ _ _ **

     

BOT> _ _ _ _ _ _ _ _ _ _ _ ** 这两个消息同样发送   TIME

示例2:满意 if (there is no hint) 以及相应的 else 声明

  

BOT>谁是甲壳虫乐队的鼓手?

     

BOT> _ _ _ _ _ _ _ _ _ _

     

BOT> R _ _ G O S _ A _ R

     

BOT> R _ N G O S T A R R

     

BOT>哦,伙计,没有人得到答案!它是:RINGO STARR

     

BOT>谁是美国总统?

     

BOT> _ _ _ _ _ _ _ _ _ _ _ **

     

BOT> _ A _ A C _ O B _ _ _ ** 这两个消息同样发送   TIME

我已经完成了故障排除以遵循僵尸程序的路径。 giveHint()只被调用一次。

function giveHint() {
    if (triviaActive) { //If trivia is enabled   

        if (!isHint) { //If there's no hint

            isHint = true;

            triviaHint = triviaAnswer.replace(/ /g, '  ');
            triviaHint = triviaHint.replace(/[A-Za-z0-9]/g, '_ ');
            modAnswer = triviaAnswer.replace(/[^A-Za-z0-9]/g, '');
            answerArray = modAnswer.split('');
            totalLetters = Math.round(answerArray.length / 2);

            bot.say(to, triviaHint);

            giveHintInterval = setInterval(giveHint, 11000);

        } else { //There is already a hint

            for (var i = 0; i < totalLetters - 1; i++) {
                giveLetter = Math.floor(Math.random() * answerArray.length);
                characterReplace = answerArray[giveLetter];
                answerArray.splice(giveLetter, 1);
                triviaHint = replaceNthMatch(triviaHint, "_", giveLetter + 1, characterReplace);
                console.log("Replacing the " + giveLetter + " underscore with " + characterReplace);

                if (answerArray.length == 0) {

                    i = totalLetters; //Escape loop
                    clearInterval(giveHintInterval);

                    if (!triviaCont) { //If trivia is 'single question' mode

                        isHint = false;
                        triviaActive = false;
                        bot.say(to, "Oh man, nobody got the answer! It was: " + color(triviaFull, 6));
                        triviaAnswer = "";

                    } else { //If trivia is in 'continuous mode'

                        isHint = false;
                        triviaActive = false;
                        bot.say(to, "Oh man, nobody got the answer! It was: " + color(triviaFull, 6));
                        triviaAnswer = "";
                        doTrivia(); //Ask a new question

                    }
                }
            }

            bot.say(to, triviaHint);

        }
    }
}

doTrivia() - *此功能从歌曲数据库中找到一首随机歌曲并询问有关它的问题

function doTrivia() {
    if (!triviaRestricted) {

        //New question, restart the hint timer
        if (giveHintInterval) {
            clearInterval(giveHintInterval);
        }

        for (var i=0;i!=1;i) {
                getRandomLine('shuffle.txt');
                var shufflearray = shuffled.split(",");
                var submitter = shufflearray[0];
                var shufArtist = shufflearray[1];
                var shufTitle = shufflearray[2];
                var shufLink = shufflearray[3];

                if (shufArtist && shufTitle) {
                    triviaActive = true; //Trivia is now active
                    i=1; // escape loop
                    var max = 2;
                    var min = 1;
                    var trivRandom = Math.floor(Math.random()*(max-min+1)+min);
                    isHint = false;

                    if (trivRandom == 1) {
                        triviaQuestion = "Who is the artist of "+color(shufTitle,12)+"?";
                        bot.say(to,triviaQuestion);
                        triviaAnswer = shufArtist;
                        triviaFull = shufArtist+" - "+shufTitle;
                        giveHint();
                    } else {
                        triviaQuestion = "Can you name this song by "+color(shufArtist,12)+"?";
                        triviaFull = shufArtist+" - "+shufTitle;
                        triviaAnswer = shufTitle;
                        bot.say(to, triviaQuestion);
                        giveHint();
                    }
                }
        }
    } else {bot.notice(from, "Trivia is currently disabled.");}
}

1 个答案:

答案 0 :(得分:0)

我找到了问题的答案。如果有人好奇,问题就是递归功能。 giveHint正在调用doTrivia,调用giveHint。感谢。