From 3d843f6c96c5902afab4d00765a21ccb8f6ac459 Mon Sep 17 00:00:00 2001 From: Luke Slater Date: Wed, 24 Aug 2011 01:57:52 +0100 Subject: [PATCH] Remove dependency on prototype.js. Not everything is tested so far though... --- modules/admin.js | 1 - modules/kick.js | 6 ++-- modules/user.js | 2 +- run.js | 81 +++++++++++++++++++++++------------------------- snippets.js | 43 +++++++++++++++++++++++++ 5 files changed, 85 insertions(+), 48 deletions(-) diff --git a/modules/admin.js b/modules/admin.js index 8771781..34767a2 100644 --- a/modules/admin.js +++ b/modules/admin.js @@ -17,7 +17,6 @@ var adminCommands = function(dbot) { dbot.say(dbot.admin, 'Reloading DB.'); try { dbot.db = JSON.parse(fs.readFileSync('db.json', 'utf-8')); - dbot.reloadModules(); } catch(err) { dbot.say(dbot.admin, 'DB reload failed.'); } finally { diff --git a/modules/kick.js b/modules/kick.js index 63e6957..b8fbe4a 100644 --- a/modules/kick.js +++ b/modules/kick.js @@ -6,14 +6,14 @@ var kick = function(dbot) { if(data.kickee == dbot.name) { dbot.instance.join(data.channel); // TODO: make interface dbot.say(data.channel, 'Thou shalt not kick ' + dbot.name); - dbot.db.kicks[name] += 1; + dbot.db.kicks[dbot.name] += 1; } else { - if(dbot.db.kicks.hasOwnProperty(data.kickee)) { + if(!dbot.db.kicks.hasOwnProperty(data.kickee)) { 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.say(data.channel, data.kickee + '-- (' + data.kickee + ' has been kicked ' + dbot.db.kicks[data.kickee] + ' times)'); } dbot.save(); diff --git a/modules/user.js b/modules/user.js index 832a231..1e490af 100644 --- a/modules/user.js +++ b/modules/user.js @@ -51,7 +51,7 @@ var userCommands = function(dbot) { }, '~kickcount': function(data, params) { - if(dbot.db.kicks.hasOwnProperty(params[1])) { + if(!dbot.db.kicks.hasOwnProperty(params[1])) { dbot.say(data.channel, params[1] + ' has either never been kicked or does not exist.'); } else { dbot.say(data.channel, params[1] + ' has been kicked ' + dbot.db.kicks[params[1]] + ' times.'); diff --git a/run.js b/run.js index 6cf1799..0fab6af 100644 --- a/run.js +++ b/run.js @@ -1,59 +1,54 @@ require('./snippets'); var fs = require('fs'); var jsbot = require('./jsbot'); -//var quote = require('./modules/quotes'); var modules = ['user', 'admin', 'puns', 'kick', 'reality', 'karma']; -var dbot = Class.create({ - 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 = require(quotes).fetch(this); +var DBot = function(dModules, quotes) { + this.admin = 'reality'; + this.waitingForKarma = false; + this.name = 'depressionbot'; + this.db = JSON.parse(fs.readFileSync('db.json', 'utf-8')); + this.quotes = require(quotes).fetch(this); - this.instance = jsbot.createJSBot(this.name, 'elara.ivixor.net', 6667, this, function() { - this.instance.join('#realitest'); - this.instance.join('#42'); - this.instance.join('#itonlygetsworse'); - }.bind(this)); + this.instance = jsbot.createJSBot(this.name, 'elara.ivixor.net', 6667, this, function() { + this.instance.join('#realitest'); + this.instance.join('#42'); + this.instance.join('#itonlygetsworse'); + }.bind(this)); - this.moduleNames = dModules; - this.rawModules = []; - this.modules = []; + this.moduleNames = dModules; + this.rawModules = []; + this.modules = []; - this.reloadModules(); - - this.instance.connect(); - }, + this.reloadModules(); + + this.instance.connect(); +}; - say: function(channel, data) { - this.instance.say(channel, data); - }, +DBot.prototype.say = function(channel, data) { + this.instance.say(channel, data); +}; - save: function() { - fs.writeFile('db.json', JSON.stringify(this.db, null, ' ')); - }, +DBot.prototype.save = function() { + fs.writeFile('db.json', JSON.stringify(this.db, null, ' ')); +}; - reloadModules: function() { - require.cache = {}; // Stupid bloody prototype.js +DBot.prototype.reloadModules = function() { + this.rawModules = []; + this.modules = []; - this.rawModules = []; - this.modules = []; + this.moduleNames.each(function(name) { + this.rawModules.push(require('./modules/' + name)); + }.bind(this)); - this.moduleNames.each(function(name) { - this.rawModules.push(require('./modules/' + name)); - }.bind(this)); + this.instance.removeListeners(); - this.instance.removeListeners(); + this.modules = this.rawModules.collect(function(rawModule) { + var module = rawModule.fetch(this); + this.instance.addListener(module.on, module.listener); + return module; + }.bind(this)); +}; - this.modules = this.rawModules.collect(function(rawModule) { - var module = rawModule.fetch(this); - this.instance.addListener(module.on, module.listener); - return module; - }.bind(this)); - } -}); - -new dbot(modules, './modules/quotes'); +new DBot(modules, './modules/quotes'); diff --git a/snippets.js b/snippets.js index d69c6d5..3967c0a 100644 --- a/snippets.js +++ b/snippets.js @@ -1,3 +1,46 @@ Array.prototype.random = function() { return this[Math.floor((Math.random()*this.length))]; }; + +Array.prototype.each = function(fun) { + for(var i=0;i