简体   繁体   English

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

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

I'm using node.js to create an IRC bot that asks trivia questions. 我正在使用node.js创建一个询问琐事的IRC机器人。 So far it works great, but I wanted to implement a system that generates hints by providing you with underscores for each letter. 到目前为止,它运行良好,但是我想实现一个系统,该系统通过为您提供每个字母的下划线来生成提示。 As time passes, it slowly fills in the blanks. 随着时间的流逝,它慢慢地填补了空白。 If nobody gets the answer before all the blanks are filled in, the bot moves on. 如果在所有空白填满之前没有人得到答案,则机器人会继续前进。 (If it is not in single-question mode) (如果它不是在单个问题模式下)

The issue: If nobody answers the question, the bot moves on to the next one as intended. 问题:如果没有人回答问题,该机器人将按预期进行到下一个机器人。 The bot then supplies two hints at the same time. 然后,该漫游器会同时提供两个提示。 Sometimes it just repeats the first hint, but other times it's actually satisfying the else statement and providing the next hint. 有时,它只是重复第一个提示,而其他时候,它实际上是在满足else语句并提供下一个提示。

I have troubleshooted to verify that the function getHint is only being called ONCE. 我进行了故障排除,以验证功能getHint仅被调用一次。

I've been staring at this code for nearly 2.5 hours now and I'm beginning to lose hope. 我已经盯着这段代码近2.5个小时了,我开始失去希望了。 I'm brand new to javascript, this is essentially my first time coding in it. 我是javascript的新手,这实质上是我的第一次编码。 Any help is GREATLY appreciated. 任何帮助是极大的赞赏。

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

BOT> Who was the drummer for The Beatles? BOT>披头士乐队的鼓手是谁?

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

BOT> R _ _ GOS _ A _ R BOT> R _ _ GOS _ A _ R

BOT> R _ NGOSTARR BOT> R _ NGOSTARR

BOT> Oh man, nobody got the answer! BOT>哦,老兄,没人知道答案! It was: RINGO STARR 那是:RINGO STARR

BOT> Who is the president of the United States? BOT>谁是美国总统?

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

BOT> _ _ _ _ _ _ _ _ _ _ _ ** THESE TWO MESSAGES ARE SENT AT THE SAME TIME BOT> _ _ _ _ _ _ _ _ _ _ ** **这两个消息在同一时间发送

EXAMPLE 2: Satisfies if (there is no hint) AND the corresponding else statement 示例2:满足 if (there is no hint) 和相应的 else 语句

BOT> Who was the drummer for The Beatles? BOT>披头士乐队的鼓手是谁?

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

BOT> R _ _ GOS _ A _ R BOT> R _ _ GOS _ A _ R

BOT> R _ NGOSTARR BOT> R _ NGOSTARR

BOT> Oh man, nobody got the answer! BOT>哦,老兄,没人知道答案! It was: RINGO STARR 那是:RINGO STARR

BOT> Who is the president of the United States? BOT>谁是美国总统?

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

BOT> _ A _ AC _ OB _ _ _ ** THESE TWO MESSAGES ARE SENT AT THE SAME TIME BOT> _ A _ AC _ OB _ _ _ ** **这两个消息在同一时间发送

I've done troubleshooting to follow the bot's path. 我已经完成了故障排除,以遵循机器人的路径。 giveHint() is only being called once. 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() - *This function finds a random song from a database of songs and asks a question about it 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.");}
}

I have found the answer to my question. 我找到了我问题的答案。 In case anyone was curious, the issue was a recursive function. 万一有人好奇,这个问题就是递归函数。 giveHint was calling doTrivia which calls giveHint. GiveHint正在调用doTrivia,后者调用了GiveHint。 Thanks. 谢谢。

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

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