使用 node.js 创建 IRC 机器人.尽管函数只被调用一次,但消息发送两次
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 _ _ 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。谢谢。
- Javascript返回值只在循环中返回一次
- Jquery FadeIn FadeOut 只工作一次
- Javascript html每点击一次就会更改url
- 如何在chrome扩展中存储数据/结果,以及如何使用setTimeout使其只被调用一次
- Rails操作只调用一次,但我在ajax中每秒钟都调用一次
- jQuery滚动功能只工作一次
- 刷新导致我的帖子“;张贴“;再一次
- 引导程序崩溃一次只能看到一个
- 有没有一个抽象层,这样我就可以集成一次,然后使用pusher、pubnub或faye
- 加载器组件仅加载一次
- 根据Angular.JS上一次的内容禁用选择
- 有没有可能的方法可以在一次ASP按钮单击中同时显示JavaScript和服务器端代码中的消息
- 节点.js:一次接收太多UDP消息,丢失它们
- WebShim:一次显示表单验证的所有错误消息
- 在提交时一次内联显示所有验证消息
- jQuery欢迎消息,直到关闭一次(Rails 3)
- 使用 node.js 创建 IRC 机器人.尽管函数只被调用一次,但消息发送两次
- Rails 设置一个 Cookie 以确保只收到一次 Javascript 消息
- Javascript显示弹出消息一次,但它显示每次页面加载
- 当你想在javascript中使用它们时,你如何规避重复的按键消息,你按住键,但只希望它播放一次