From 3a36ee1894417c0450fd4edf3720532c34af7abb Mon Sep 17 00:00:00 2001 From: Luke Slater Date: Wed, 24 Aug 2011 16:15:42 +0100 Subject: [PATCH] Hot-loading now works. Fuck yeah! --- modules/admin.js | 11 +++-------- run.js | 4 ++-- snippets.js | 10 +++++----- 3 files changed, 10 insertions(+), 15 deletions(-) diff --git a/modules/admin.js b/modules/admin.js index 34767a2..5cc1ef9 100644 --- a/modules/admin.js +++ b/modules/admin.js @@ -14,14 +14,9 @@ var adminCommands = function(dbot) { }, 'reload': function(data, params) { - dbot.say(dbot.admin, 'Reloading DB.'); - try { - dbot.db = JSON.parse(fs.readFileSync('db.json', 'utf-8')); - } catch(err) { - dbot.say(dbot.admin, 'DB reload failed.'); - } finally { - dbot.say(dbot.admin, 'DB Reload successful.'); - } + dbot.db = JSON.parse(fs.readFileSync('db.json', 'utf-8')); + dbot.reloadModules(); + dbot.say(dbot.admin, 'Reloaded.'); }, 'say': function(data, params) { diff --git a/run.js b/run.js index 0fab6af..8718b74 100644 --- a/run.js +++ b/run.js @@ -13,8 +13,6 @@ var DBot = function(dModules, quotes) { 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; @@ -39,6 +37,8 @@ DBot.prototype.reloadModules = function() { this.modules = []; 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)); }.bind(this)); diff --git a/snippets.js b/snippets.js index f0b572a..6746548 100644 --- a/snippets.js +++ b/snippets.js @@ -1,3 +1,5 @@ +/*** Array ***/ + Array.prototype.random = function() { return this[Math.floor((Math.random()*this.length))]; }; @@ -13,18 +15,16 @@ Array.prototype.collect = function(fun) { for(var i=0;i