Remove dependency on prototype.js. Not everything is tested so far though...

This commit is contained in:
Luke Slater 2011-08-24 01:57:52 +01:00
parent df61c97eb2
commit 3d843f6c96
5 changed files with 85 additions and 48 deletions

View File

@ -17,7 +17,6 @@ var adminCommands = function(dbot) {
dbot.say(dbot.admin, 'Reloading DB.'); dbot.say(dbot.admin, 'Reloading DB.');
try { try {
dbot.db = JSON.parse(fs.readFileSync('db.json', 'utf-8')); dbot.db = JSON.parse(fs.readFileSync('db.json', 'utf-8'));
dbot.reloadModules();
} catch(err) { } catch(err) {
dbot.say(dbot.admin, 'DB reload failed.'); dbot.say(dbot.admin, 'DB reload failed.');
} finally { } finally {

View File

@ -6,14 +6,14 @@ var kick = function(dbot) {
if(data.kickee == dbot.name) { if(data.kickee == dbot.name) {
dbot.instance.join(data.channel); // TODO: make interface dbot.instance.join(data.channel); // TODO: make interface
dbot.say(data.channel, 'Thou shalt not kick ' + dbot.name); dbot.say(data.channel, 'Thou shalt not kick ' + dbot.name);
dbot.db.kicks[name] += 1; dbot.db.kicks[dbot.name] += 1;
} else { } else {
if(dbot.db.kicks.hasOwnProperty(data.kickee)) { if(!dbot.db.kicks.hasOwnProperty(data.kickee)) {
dbot.db.kicks[data.kickee] = 1; dbot.db.kicks[data.kickee] = 1;
} else { } else {
dbot.db.kicks[data.kickee] += 1; 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(); dbot.save();

View File

@ -51,7 +51,7 @@ var userCommands = function(dbot) {
}, },
'~kickcount': function(data, params) { '~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.'); dbot.say(data.channel, params[1] + ' has either never been kicked or does not exist.');
} else { } else {
dbot.say(data.channel, params[1] + ' has been kicked ' + dbot.db.kicks[params[1]] + ' times.'); dbot.say(data.channel, params[1] + ' has been kicked ' + dbot.db.kicks[params[1]] + ' times.');

79
run.js
View File

@ -1,59 +1,54 @@
require('./snippets'); require('./snippets');
var fs = require('fs'); var fs = require('fs');
var jsbot = require('./jsbot'); var jsbot = require('./jsbot');
//var quote = require('./modules/quotes');
var modules = ['user', 'admin', 'puns', 'kick', 'reality', 'karma']; var modules = ['user', 'admin', 'puns', 'kick', 'reality', 'karma'];
var dbot = Class.create({ var DBot = function(dModules, quotes) {
initialize: function(dModules, quotes) { this.admin = 'reality';
this.admin = 'reality'; this.waitingForKarma = false;
this.waitingForKarma = false; this.name = 'depressionbot';
this.name = 'depressionbot'; this.db = JSON.parse(fs.readFileSync('db.json', 'utf-8'));
this.db = JSON.parse(fs.readFileSync('db.json', 'utf-8')); this.quotes = require(quotes).fetch(this);
this.quotes = require(quotes).fetch(this);
this.instance = jsbot.createJSBot(this.name, 'elara.ivixor.net', 6667, this, function() { this.instance = jsbot.createJSBot(this.name, 'elara.ivixor.net', 6667, this, function() {
this.instance.join('#realitest'); this.instance.join('#realitest');
this.instance.join('#42'); this.instance.join('#42');
this.instance.join('#itonlygetsworse'); this.instance.join('#itonlygetsworse');
}.bind(this)); }.bind(this));
this.moduleNames = dModules; this.moduleNames = dModules;
this.rawModules = []; this.rawModules = [];
this.modules = []; this.modules = [];
this.reloadModules(); this.reloadModules();
this.instance.connect(); this.instance.connect();
}, };
say: function(channel, data) { DBot.prototype.say = function(channel, data) {
this.instance.say(channel, data); this.instance.say(channel, data);
}, };
save: function() { DBot.prototype.save = function() {
fs.writeFile('db.json', JSON.stringify(this.db, null, ' ')); fs.writeFile('db.json', JSON.stringify(this.db, null, ' '));
}, };
reloadModules: function() { DBot.prototype.reloadModules = function() {
require.cache = {}; // Stupid bloody prototype.js this.rawModules = [];
this.modules = [];
this.rawModules = []; this.moduleNames.each(function(name) {
this.modules = []; this.rawModules.push(require('./modules/' + name));
}.bind(this));
this.moduleNames.each(function(name) { this.instance.removeListeners();
this.rawModules.push(require('./modules/' + name));
}.bind(this));
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) { new DBot(modules, './modules/quotes');
var module = rawModule.fetch(this);
this.instance.addListener(module.on, module.listener);
return module;
}.bind(this));
}
});
new dbot(modules, './modules/quotes');

View File

@ -1,3 +1,46 @@
Array.prototype.random = function() { Array.prototype.random = function() {
return this[Math.floor((Math.random()*this.length))]; return this[Math.floor((Math.random()*this.length))];
}; };
Array.prototype.each = function(fun) {
for(var i=0;i<this.length;i++) {
fun(this[i]);
}
};
Array.prototype.collect = function(fun) {
var collect = [];
for(var i=0;i<this.length;i++) {
collect.push(fun(this[i]));
}
};
/*** String ***/
String.prototype.endsWith = function(needle) {
var end = this.slice(this.length - needle.length);
if(needle === end) {
return true;
} else {
return false;
}
};
String.prototype.startsWith = function(needle) {
var start = this.slice(0, this.length - needle.length);
if(needle === start) {
return true;
} else {
return false;
}
};
/*** Object ***/
Object.prototype.isFunction = function(obj) {
if(typeof(obj) == 'function') {
return true;
} else {
return false;
}
};