From a32b011b7f21f23e5f039f12a1370ea3b4af8802 Mon Sep 17 00:00:00 2001 From: Luke Slater Date: Mon, 22 Aug 2011 18:56:36 +0100 Subject: [PATCH] Split more into seperate files and automatic loading of modules. --- admin.js | 47 ++++++++++++++++++++++++++++++++++++++++++ puns.js | 22 ++++++++++++++++++++ quotes.js | 8 ++++---- run.js | 61 +++++++++++-------------------------------------------- user.js | 2 +- 5 files changed, 86 insertions(+), 54 deletions(-) create mode 100644 admin.js create mode 100644 puns.js diff --git a/admin.js b/admin.js new file mode 100644 index 0000000..af4cfb9 --- /dev/null +++ b/admin.js @@ -0,0 +1,47 @@ +var adminCommands = function(dbot) { + var dbot = dbot; + + var commands = { + 'join': function(data, params) { + dbot.join(params[1]); + dbot.say(admin, 'Joined ' + params[1]); + }, + + 'part': function(data, params) { + dbot.part(params[1]); + dbot.say(admin); + }, + + 'reload': function(data, params) { + dbot.say(admin, 'Reloading DB.'); + try { + dbot.db = JSON.parse(fs.readFileSync('db.json', 'utf-8')); + } catch(err) { + dbot.say(admin, 'DB reload failed.'); + } finally { + dbot.say(admin, 'DB Reload successful.'); + } + }, + + 'say': function(data, params) { + var c = params[1]; + var m = params.slice(2).join(' '); + dbot.say(c, m); + } + }; + + return { + 'listener': function(data) { + params = data.message.split(' '); + + if(commands.hasOwnProperty(params[0])) + commands[params[0]](data, params); + }, + + 'on': 'PRIVMSG' + }; +}; + +exports.fetch = function(dbot) { + return adminCommands(dbot); +}; diff --git a/puns.js b/puns.js new file mode 100644 index 0000000..3ad0129 --- /dev/null +++ b/puns.js @@ -0,0 +1,22 @@ +var puns = function(dbot) { + var dbot = dbot; + + return { + 'listener': function(data) { + if(data.user == 'Lamp') { + dbot.say(data.channel, dbot.db.quoteArrs.lamp.random()); + } else if(data.user == 'reality') { + dbot.instance.say(data.channel, dbot.db.realiPuns.random()); + } else if(instance.inChannel(data.channel)) { + dbot.instance.say('aisbot', '.karma ' + data.user); + dbot.waitingForKarma = data.channel; + } + }, + + 'on': 'JOIN' + }; +} + +exports.fetch = function(dbot) { + return puns(dbot); +}; diff --git a/quotes.js b/quotes.js index 959f6d9..8cc002f 100644 --- a/quotes.js +++ b/quotes.js @@ -1,5 +1,5 @@ -var quotes = function(quotes) { - var qArrs = quotes; +var quotes = function(dbot) { + var qArrs = dbot.db.quoteArrs; return { get: function(key) { @@ -42,6 +42,6 @@ var quotes = function(quotes) { }; }; -exports.fetch = function() { - return quotes; +exports.fetch = function(dbot) { + return quotes(dbot); }; diff --git a/run.js b/run.js index 9b4c403..5872967 100644 --- a/run.js +++ b/run.js @@ -2,6 +2,8 @@ var fs = require('fs'); var jsbot = require('./jsbot'); var quote = require('./quotes'); var userCommands = require('./user'); +var adminCommands = require('./admin'); +var puns = require('./puns'); /////////////////////////// @@ -9,65 +11,24 @@ Array.prototype.random = function() { return this[Math.floor((Math.random()*this.length))]; }; -/////////////////////////// - - -this.adminCommands = { - 'join': function(data, params) { - instance.join(params[1]); - instance.say(admin, 'Joined ' + params[1]); - }, - - 'part': function(data, params) { - instance.part(params[1]); - instance.say(admin); - }, - - 'reload': function(data, params) { - instance.say(admin, 'Reloading DB.'); - try { - db = JSON.parse(fs.readFileSync('db.json', 'utf-8')); - } catch(err) { - instance.say(admin, 'DB reload failed.'); - } finally { - instance.say(admin, 'DB Reload successful.'); - } - }, - - 'say': function(data, params) { - var c = params[1]; - var m = params.slice(2).join(' '); - instance.say(c, m); - } - }; - - /////////////////////////// var dbot = Class.create({ - initialize: function(quotes, userCommands) { + initialize: function(dModules, quotes) { this.admin = 'reality'; this.waitingForKarma = false; this.name = 'depressionbot'; this.db = JSON.parse(fs.readFileSync('db.json', 'utf-8')); - this.quotes = quotes(this.db.quoteArrs); - this.userCommands = userCommands.fetch(this); + this.quotes = quotes.fetch(this); this.instance = jsbot.createJSBot(this.name, 'elara.ivixor.net', 6667, this, function() { this.instance.join('#realitest'); }.bind(this)); - this.instance.addListener(this.userCommands.on, this.userCommands.listener); - - this.instance.addListener('JOIN', function(data) { - if(data.user == 'Lamp') { - this.instance.say(data.channel, db.quoteArrs.lamp.random()); - } else if(data.user == 'reality') { - this.instance.say(data.channel, db.realiPuns.random()); - } else if(instance.inChannel(data.channel)) { - this.instance.say('aisbot', '.karma ' + data.user); - this.waitingForKarma = data.channel; - } - }); + this.modules = dModules.collect(function(n) { + var module = n.fetch(this); + this.instance.addListener(module.on, module.listener); + return n.fetch(this); + }.bind(this)); this.instance.addListener('KICK', function(data) { if(data.kickee == name) { @@ -114,6 +75,8 @@ var dbot = Class.create({ } } }); + + this.instance.connect(); }, say: function(channel, data) { @@ -125,4 +88,4 @@ var dbot = Class.create({ } }); -new dbot(quote.fetch(), userCommands); +new dbot([userCommands, adminCommands, puns], quote); diff --git a/user.js b/user.js index 3321a29..0ac9bdb 100644 --- a/user.js +++ b/user.js @@ -75,7 +75,7 @@ var userCommands = function(dbot) { }, 'on': 'PRIVMSG' - } + }; }; exports.fetch = function(dbot) {