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

23
run.js
View File

@ -1,12 +1,10 @@
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) {
var DBot = function(dModules, quotes) {
this.admin = 'reality';
this.waitingForKarma = false;
this.name = 'depressionbot';
@ -26,19 +24,17 @@ var dbot = Class.create({
this.reloadModules();
this.instance.connect();
},
};
say: function(channel, data) {
DBot.prototype.say = function(channel, data) {
this.instance.say(channel, data);
},
};
save: function() {
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 = [];
@ -53,7 +49,6 @@ var dbot = Class.create({
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;
}
};