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.');
try {
dbot.db = JSON.parse(fs.readFileSync('db.json', 'utf-8'));
dbot.reloadModules();
} catch(err) {
dbot.say(dbot.admin, 'DB reload failed.');
} finally {

View File

@ -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();

View File

@ -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.');

81
run.js
View File

@ -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');

View File

@ -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<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;
}
};