Hot-loading now works. Fuck yeah!

This commit is contained in:
Luke Slater 2011-08-24 16:15:42 +01:00
parent e31cb1beb7
commit 3a36ee1894
3 changed files with 10 additions and 15 deletions

View File

@ -14,14 +14,9 @@ var adminCommands = function(dbot) {
}, },
'reload': function(data, params) { 'reload': function(data, params) {
dbot.say(dbot.admin, 'Reloading DB.'); dbot.db = JSON.parse(fs.readFileSync('db.json', 'utf-8'));
try { dbot.reloadModules();
dbot.db = JSON.parse(fs.readFileSync('db.json', 'utf-8')); dbot.say(dbot.admin, 'Reloaded.');
} catch(err) {
dbot.say(dbot.admin, 'DB reload failed.');
} finally {
dbot.say(dbot.admin, 'DB Reload successful.');
}
}, },
'say': function(data, params) { 'say': function(data, params) {

4
run.js
View File

@ -13,8 +13,6 @@ var DBot = function(dModules, quotes) {
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('#itonlygetsworse');
}.bind(this)); }.bind(this));
this.moduleNames = dModules; this.moduleNames = dModules;
@ -39,6 +37,8 @@ DBot.prototype.reloadModules = function() {
this.modules = []; this.modules = [];
this.moduleNames.each(function(name) { this.moduleNames.each(function(name) {
var cacheKey = require.resolve('./modules/' + name);
require.cache[cacheKey] = undefined; // TODO: snippet to remove element properly
this.rawModules.push(require('./modules/' + name)); this.rawModules.push(require('./modules/' + name));
}.bind(this)); }.bind(this));

View File

@ -1,3 +1,5 @@
/*** Array ***/
Array.prototype.random = function() { Array.prototype.random = function() {
return this[Math.floor((Math.random()*this.length))]; return this[Math.floor((Math.random()*this.length))];
}; };
@ -13,18 +15,16 @@ Array.prototype.collect = function(fun) {
for(var i=0;i<this.length;i++) { for(var i=0;i<this.length;i++) {
collect.push(fun(this[i])); collect.push(fun(this[i]));
} }
return collect;
}; };
Array.prototype.include = function(value) { Array.prototype.include = function(value) {
var includes = false;
for(var i=0;i<this.length;i++) { for(var i=0;i<this.length;i++) {
if(this[i] == value) { if(this[i] == value) {
includes = true; return true;
break;
} }
} }
return false;
return includes;
}; };
/*** String ***/ /*** String ***/