From b1cb45798ff3a4df40cb159de5b2b217582a5a90 Mon Sep 17 00:00:00 2001 From: Luke Slater Date: Mon, 22 Aug 2011 19:24:23 +0100 Subject: [PATCH] moved everything to a module --- karma.js | 30 ++++++++++++++++++++++++++++++ kick.js | 26 ++++++++++++++++++++++++++ quotes.js | 4 ++-- reality.js | 22 ++++++++++++++++++++++ run.js | 52 +++++----------------------------------------------- 5 files changed, 85 insertions(+), 49 deletions(-) create mode 100644 karma.js create mode 100644 kick.js create mode 100644 reality.js diff --git a/karma.js b/karma.js new file mode 100644 index 0000000..7218e74 --- /dev/null +++ b/karma.js @@ -0,0 +1,30 @@ +var karma = function(dbot) { + var dbot = dbot; + + return { + 'listener': function(data) { + if(data.user == 'aisbot' && data.channel == 'aisbot' && + dbot.waitingForKarma != false && data.message.match(/is at/)) { + var split = data.message.split(' '); + var target = split[0]; + var karma = split[3]; + + if(karma.startsWith('-')) { + dbot.say(dbot.waitingForKarma, target + dbot.db.hatedPhrases.random() + ' (' + karma + ')'); + } else if(karma == '0') { + dbot.say(dbot.waitingForKarma, target + dbot.db.neutralPhrases.random() + ' (0)'); + } else { + dbot.say(dbot.waitingForKarma, target + dbot.db.lovedPhrases.random() + ' (' + karma + ')'); + } + + dbot.waitingForKarma = false; + } + }, + + 'on': 'PRIVMSG' + } +}; + +exports.fetch = function(dbot) { + return karma(dbot); +}; diff --git a/kick.js b/kick.js new file mode 100644 index 0000000..2395898 --- /dev/null +++ b/kick.js @@ -0,0 +1,26 @@ +var kick = function(dbot) { + return { + 'listener': function(data) { + if(data.kickee == dbot.name) { + dbot.instance.join(data.channel); // make interface + dbot.say(data.channel, 'Thou shalt not kick ' + dbot.name); + dbot.db.kicks[name] += 1; + } else { + if(dbot.db.kicks[data.kickee] == undefined) { + dbot.db.kicks[data.kickee] = 1; + } else { + dbot.db.kicks[data.kickee] += 1; + } + instance.say(data.channel, data.kickee + '-- (' + data.kickee + ' has been kicked ' + dbot.db.kicks[data.kickee] + ' times)'); + } + + dbot.save(); + }, + + on: 'KICK' + }; +}; + +exports.fetch = function(dbot) { + return kick(dbot); +}; diff --git a/quotes.js b/quotes.js index 8cc002f..7424506 100644 --- a/quotes.js +++ b/quotes.js @@ -1,10 +1,10 @@ var quotes = function(dbot) { - var qArrs = dbot.db.quoteArrs; + var quotes = dbot.db.quoteArrs; return { get: function(key) { if(quotes.hasOwnProperty(key)) { - return key + ': ' + qArrs[key].random(); + return key + ': ' + quotes[key].random(); } else { return 'No quotes under ' + key; } diff --git a/reality.js b/reality.js new file mode 100644 index 0000000..edb415c --- /dev/null +++ b/reality.js @@ -0,0 +1,22 @@ +var reality = function(dbot) { + var dbot = dbot; + + return { + 'listener': function(data, params) { + if(data.user == 'reality') { + var once = data.message.match(/I ([\d\w\s]* once)/); + if(once != null) { + dbot.db.realiPuns.push('reality ' + once[1] + '.'); + dbot.instance.say(data.channel, '\'reality ' + once[1] + '.\' saved.'); + dbot.save(); + } + } + }, + + 'on': 'PRIVMSG' + }; +} + +exports.fetch = function(dbot) { + return reality(dbot); +} diff --git a/run.js b/run.js index 5872967..dad8f79 100644 --- a/run.js +++ b/run.js @@ -4,6 +4,9 @@ var quote = require('./quotes'); var userCommands = require('./user'); var adminCommands = require('./admin'); var puns = require('./puns'); +var kick = require('./kick'); +var reality = require('./reality'); +var karma = require('./karma'); /////////////////////////// @@ -12,6 +15,7 @@ Array.prototype.random = function() { }; /////////////////////////// + var dbot = Class.create({ initialize: function(dModules, quotes) { this.admin = 'reality'; @@ -30,52 +34,6 @@ var dbot = Class.create({ return n.fetch(this); }.bind(this)); - this.instance.addListener('KICK', function(data) { - if(data.kickee == name) { - this.instance.join(data.channel); - this.instance.say(data.channel, 'Thou shalt not kick ' + name); - this.db.kicks[name] += 1; - } else { - if(this.db.kicks[data.kickee] == undefined) { - this.db.kicks[data.kickee] = 1; - } else { - this.db.kicks[data.kickee] += 1; - } - instance.say(data.channel, data.kickee + '-- (' + data.kickee + ' has been kicked ' + this.db.kicks[data.kickee] + ' times)'); - } - - this.save(); - }); - - this.instance.addListener('PRIVMSG', function(data) { - if(data.user == 'aisbot' && data.channel == name && waitingForKarma != false && data.message.match(/is at/)) { - var split = data.message.split(' '); - var target = split[0]; - var karma = split[3]; - - if(karma.startsWith('-')) { - this.instance.say(waitingForKarma, target + this.db.hatedPhrases.random() + ' (' + karma + ')'); - } else if(karma == '0') { - this.instance.say(waitingForKarma, target + this.db.neutralPhrases.random() + ' (0)'); - } else { - this.instance.say(waitingForKarma, target + this.db.lovedPhrases.random() + ' (' + karma + ')'); - } - - waitingForKarma = false; - } - }); - - this.instance.addListener('PRIVMSG', function(data) { - if(data.user == 'reality') { - var once = data.message.match(/I ([\d\w\s]* once.)/); - if(once != null) { - this.db.realiPuns.push('reality ' + once[1]); - this.instance.say(data.channel, '\'reality ' + once[1] + '\' saved.'); - this.save(); - } - } - }); - this.instance.connect(); }, @@ -88,4 +46,4 @@ var dbot = Class.create({ } }); -new dbot([userCommands, adminCommands, puns], quote); +new dbot([userCommands, adminCommands, puns, kick, reality, karma], quote);