Load and unload modules while running, fixes in the user module regex

This commit is contained in:
Luke Slater 2011-08-24 18:23:00 +01:00
parent d17ee73393
commit c5c37334f1
4 changed files with 39 additions and 13 deletions

View File

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

View File

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

View File

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

8
run.js
View File

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