我的node-irc bot有时会重复聊天中的内容。有时它完美无缺;其他时候它重复。我查看了整个代码,无法发现任何递归。
连接功能(与我的听众一起)
//connects the bot to the channel
this.connectToChannel = function(callback) {
console.log("Connection");
botInstance.connect();
botInstance.join(currentChannel, function(){
botInstance.say(currentChannel, initalConnectionMessage);
console.log("Joined");
callback();
});
botInstance.addListener("message", function(nick, to, text, message) {
if (text == lastText){
return; // you can i've tried to avoid this issue
}
bannedWords.forEach(function(item,index){
if (text.includes(item)){
parent.sendMessage(nick + " > You're not allowed to use that type of language.");
parent.sendMessage("/timeout " + nick + " 10");
}
});
commands.forEach(function(item, index){
var splitData = text.split(' ');
if (splitData[0] == item.getExecutor()) {
//running this command
var response = item.getResponse();
response = response.replace("%user%", nick);
response = response.replace("%bot_username%", config.botName);
parent.sendMessage(response);
}
});
});
}
Bot Creation(在连接之前调用此方法)
this.createBot = function(channel) {
currentChannel = channel;
var irc = require('irc');
botInstance = new irc.Client(config.server, config.botName, {
channels: [currentChannel + " " + config.oAuth],
userName: config.botName,
password: config.oAuth
});
}
Bot发送消息
this.sendMessage = function(msg){
if(botInstance != null) {
botInstance.say(currentChannel, msg);
lastMessage = msg;
}