From c5c37334f1a12120fdc2641e559c6c2b0a8f845d Mon Sep 17 00:00:00 2001 From: Luke Slater Date: Wed, 24 Aug 2011 18:23:00 +0100 Subject: [PATCH] Load and unload modules while running, fixes in the user module regex --- modules/admin.js | 21 +++++++++++++++++++-- modules/reality.js | 15 +++++++++------ modules/user.js | 8 ++++---- run.js | 8 +++++++- 4 files changed, 39 insertions(+), 13 deletions(-) diff --git a/modules/admin.js b/modules/admin.js index 5cc1ef9..8cb4319 100644 --- a/modules/admin.js +++ b/modules/admin.js @@ -16,13 +16,30 @@ var adminCommands = function(dbot) { 'reload': function(data, params) { dbot.db = JSON.parse(fs.readFileSync('db.json', 'utf-8')); dbot.reloadModules(); - dbot.say(dbot.admin, 'Reloaded.'); + dbot.say(data.channel, 'Reloaded that shit.'); }, 'say': function(data, params) { var c = params[1]; var m = params.slice(2).join(' '); dbot.say(c, m); + }, + + 'load': function(data, params) { + dbot.moduleNames.push(params[1]); + dbot.reloadModules(); + dbot.say(data.channel, 'Loaded new module: ' + params[1]); + }, + + 'unload': function(data, params) { + console.log(dbot.moduleNames); + if(dbot.moduleNames.include(params[1])) { + dbot.moduleNames[params[1]] = undefined; + dbot.reloadModules(); + dbot.say(data.channel, 'Turned off module: ' + params[1]); + } else { + dbot.say(data.channel, 'Module ' + params[1] + ' isn\'t loaded... Idiot...'); + } } }; @@ -30,7 +47,7 @@ var adminCommands = function(dbot) { 'listener': function(data) { params = data.message.split(' '); - if(commands.hasOwnProperty(params[0])) + if(commands.hasOwnProperty(params[0]) && data.user == dbot.admin) commands[params[0]](data, params); }, diff --git a/modules/reality.js b/modules/reality.js index edb415c..ed10975 100644 --- a/modules/reality.js +++ b/modules/reality.js @@ -4,12 +4,15 @@ var reality = function(dbot) { return { 'listener': function(data, params) { if(data.user == 'reality') { - var once = data.message.match(/I ([\d\w\s]* once)/); - if(once != null) { - dbot.db.realiPuns.push('reality ' + once[1] + '.'); - dbot.instance.say(data.channel, '\'reality ' + once[1] + '.\' saved.'); - dbot.save(); - } + var once = data.message.match(/^I ([\d\w\s,]* once)/); + } else { + var once = data.message.match(/^reality ([\d\w\s,]* once)/); + } + + if(once != null) { + dbot.db.realiPuns.push('reality ' + once[1] + '.'); + dbot.instance.say(data.channel, '\'reality ' + once[1] + '.\' saved.'); + dbot.save(); } }, diff --git a/modules/user.js b/modules/user.js index 1e490af..a4c6095 100644 --- a/modules/user.js +++ b/modules/user.js @@ -8,14 +8,14 @@ var userCommands = function(dbot) { }, '~q': function(data, params) { - var q = data.message.match(/~q ([\d\w\s]*)/) + var q = data.message.match(/^~q ([\d\w\s]*)/) if(q != undefined) { dbot.say(data.channel, dbot.quotes.get(q[1].trim())); } }, '~qadd': function(data, params) { - var q = data.message.match(/~qadd ([\d\w\s]*)=(.+)$/); + var q = data.message.match(/^~qadd ([\d\w\s]*)=(.+)$/); if(q != null && q.length >= 3) { dbot.say(data.channel, dbot.quotes.add(q)); dbot.save(); @@ -25,14 +25,14 @@ var userCommands = function(dbot) { }, '~qset': function(data, params) { - var q = data.message.match(/~qset ([\d\w\s]*)=(.+)$/); + var q = data.message.match(/^~qset ([\d\w\s]*)=(.+)$/); if(q != undefined && q.length >= 3) { dbot.say(data.channel, dbot.quotes.set(q)); } }, '~qcount': function(data, params) { - var q = data.message.match(/~qcount ([\d\w\s]*)/)[1].trim(); + var q = data.message.match(/^~qcount ([\d\w\s]*)/)[1].trim(); if(q != undefined) { dbot.say(data.channel, dbot.quotes.count(q)); } diff --git a/run.js b/run.js index 8718b74..00f379e 100644 --- a/run.js +++ b/run.js @@ -13,6 +13,8 @@ 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,7 +41,11 @@ DBot.prototype.reloadModules = function() { 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)); + try { + this.rawModules.push(require('./modules/' + name)); + } catch(err) { + console.log('Failed to load module: ' + name); + } }.bind(this)); this.instance.removeListeners();