fix module loader. admin sets config keys onload. I am incredibly annoyed. [#331]

This commit is contained in:
reality 2013-04-20 16:08:34 +00:00
parent 466cb12c94
commit 3cd4513fe4
3 changed files with 61 additions and 44 deletions

View File

@ -36,6 +36,25 @@ var admin = function(dbot) {
} }
}.bind(this) }.bind(this)
}; };
this.onLoad = function() {
var configMap = dbot.config;
this.db.scan('config', function(config) {
if(config) {
var currentPath = configMap,
key = config.key.split('.'),
value = config.value;
for(var i=0;i<key.length-1;i++) {
if(_.has(currentPath, key[i])) {
currentPath = currentPath[key[i]];
}
}
currentPath[key[i]] = value;
}
}, function(err) { });
}.bind(this);
}; };
exports.fetch = function(dbot) { exports.fetch = function(dbot) {

View File

@ -164,7 +164,10 @@ var commands = function(dbot) {
event.reply(configPath + ": " + config + " -> " + newOption); event.reply(configPath + ": " + config + " -> " + newOption);
config = newOption; config = newOption;
this.db.save('config', configPath, { 'value': config }, function(err) { this.db.save('config', configPath, {
'key': configPath,
'value': config
}, function(err) {
dbot.reloadModules(); dbot.reloadModules();
}); });
} else { } else {
@ -186,7 +189,10 @@ var commands = function(dbot) {
if(_.isArray(config)) { if(_.isArray(config)) {
event.reply(configPath + ": " + config + " << " + newOption); event.reply(configPath + ": " + config + " << " + newOption);
config.push(newOption); config.push(newOption);
this.db.save('config', configPath, { 'value': config }, function(err) { this.db.save('config', configPath, {
'key': configPath,
'value': config
}, function(err) {
dbot.reloadModules(); dbot.reloadModules();
}); });
} else { } else {

70
run.js
View File

@ -131,12 +131,8 @@ DBot.prototype.reloadModules = function() {
this.modules = {}; this.modules = {};
this.commands = {}; this.commands = {};
this.api = {}; this.api = {};
this.commandMap = {}; // Map of which commands belong to which modules
this.usage = {}; this.usage = {};
// Load config changes
_.extend(this.config, this.db.config);
try { try {
this.strings = JSON.parse(fs.readFileSync('strings.json', 'utf-8')); this.strings = JSON.parse(fs.readFileSync('strings.json', 'utf-8'));
} catch(err) { } catch(err) {
@ -157,7 +153,6 @@ DBot.prototype.reloadModules = function() {
require('./snippets'); require('./snippets');
this.instance.removeListeners(); this.instance.removeListeners();
_.each(moduleNames, function(name) { _.each(moduleNames, function(name) {
this.status[name] = true; this.status[name] = true;
var moduleDir = './modules/' + name + '/'; var moduleDir = './modules/' + name + '/';
@ -207,13 +202,29 @@ DBot.prototype.reloadModules = function() {
} }
this.config[name] = config; this.config[name] = config;
var loadModule = function(db) { // Groovy funky database shit
if(!_.has(config, 'dbType') || config.dbType == 'olde') {
// Generate missing DB keys
_.each(config.dbKeys, function(dbKey) {
if(!_.has(this.db, dbKey)) {
this.db[dbKey] = {};
}
}, this);
} else {
// Just use the name of the module for now, add dbKey iteration later
this.ddb.createDB(name, config.dbType, {}, function(db) {});
}
}.bind(this));
process.nextTick(function() {
_.each(moduleNames, function(name) {
var moduleDir = './modules/' + name + '/';
var rawModule = require(moduleDir + name); var rawModule = require(moduleDir + name);
var module = rawModule.fetch(this); var module = rawModule.fetch(this);
this.rawModules.push(rawModule); this.rawModules.push(rawModule);
module.name = name; module.name = name;
module.db = db; module.db = this.ddb.databanks[name];
module.config = this.config[name]; module.config = this.config[name];
// Load the module data // Load the module data
@ -276,45 +287,26 @@ DBot.prototype.reloadModules = function() {
} }
this.modules[module.name] = module; this.modules[module.name] = module;
}.bind(this));
}.bind(this));
if(_.has(this.modules, 'web')) this.modules.web.reloadPages(); process.nextTick(function() {
if(_.has(this.modules, 'web')) this.modules.web.reloadPages();
_.each(this.modules, function(module, name) { _.each(this.modules, function(module, name) {
if(module.onLoad) { if(module.onLoad) {
try { try {
module.onLoad(); module.onLoad();
} catch(err) { } catch(err) {
this.status[name] = 'Error in onLoad: ' + err + ' ' + err.stack.split('\n')[1].trim(); this.status[name] = 'Error in onLoad: ' + err + ' ' + err.stack.split('\n')[1].trim();
console.log('MODULE ONLOAD ERROR (' + name + '): ' + err ); console.log('MODULE ONLOAD ERROR (' + name + '): ' + err );
}
} }
}, this); }
}.bind(this); }, this);
// Groovy funky database shit
if(!_.has(config, 'dbType') || config.dbType == 'olde') {
// Generate missing DB keys
_.each(config.dbKeys, function(dbKey) {
if(!_.has(this.db, dbKey)) {
this.db[dbKey] = {};
}
}, this);
loadModule(this.db);
} else {
// Just use the name of the module for now, add dbKey iteration later
this.ddb.createDB(name, config.dbType, {}, function(db) {
loadModule(db);
});
}
}.bind(this)); }.bind(this));
this.save(); this.save();
}; };
// Load the module itself
DBot.prototype.loadModule = function(name, db) {
}
DBot.prototype.cleanNick = function(key) { DBot.prototype.cleanNick = function(key) {
key = key.toLowerCase(); key = key.toLowerCase();
while(key.endsWith("_")) { while(key.endsWith("_")) {