繁体   English   中英

使用node.js创建IRC bot。 尽管功能仅被调用一次,但消息仍发送两次

[英]Using node.js to create IRC bot. Message sends twice despite function only being called once

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

问题:如果没有人回答问题,该机器人将按预期进行到下一个机器人。 然后,该漫游器会同时提供两个提示。 有时,它只是重复第一个提示,而其他时候,它实际上是在满足else语句并提供下一个提示。

我进行了故障排除,以验证功能getHint仅被调用一次。

我已经盯着这段代码近2.5个小时了,我开始失去希望了。 我是javascript的新手,这实质上是我的第一次编码。 任何帮助是极大的赞赏。

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

BOT>披头士乐队的鼓手是谁?

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

BOT> R _ _ GOS _ A _ R

BOT> R _ NGOSTARR

BOT>哦,老兄,没人知道答案! 那是:RINGO STARR

BOT>谁是美国总统?

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

BOT> _ _ _ _ _ _ _ _ _ _ ** **这两个消息在同一时间发送

示例2:满足 if (there is no hint) 和相应的 else 语句

BOT>披头士乐队的鼓手是谁?

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

BOT> R _ _ GOS _ A _ R

BOT> R _ NGOSTARR

BOT>哦,老兄,没人知道答案! 那是:RINGO STARR

BOT>谁是美国总统?

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

BOT> _ A _ AC _ OB _ _ _ ** **这两个消息在同一时间发送

我已经完成了故障排除,以遵循机器人的路径。 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.");}
}

我找到了我问题的答案。 万一有人好奇,这个问题就是递归函数。 GiveHint正在调用doTrivia,后者调用了GiveHint。 谢谢。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM