使用 node.js 创建 IRC 机器人.尽管函数只被调用一次,但消息发送两次

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

本文关键字:一次 消息 两次 调用 IRC 创建 js node 机器人 函数 使用      更新时间:2023-09-26

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

问题

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

我已经进行了故障排除,以验证函数getHint是否只被调用一次。

我已经盯着这段代码将近 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

博特>天啊,没有人得到答案!那是:林戈·斯塔尔

博特>谁是美国总统?

BOT> _ _ **

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

示例 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

博特>天啊,没有人得到答案!那是:林戈·斯塔尔

博特>谁是美国总统?

BOT> _ _ **

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

我已经完成了故障排除以遵循机器人的路径。 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,doTrivia调用giveHint。谢谢。