From 3cd4513fe45c825738522639b2765f0512c0749f Mon Sep 17 00:00:00 2001 From: reality Date: Sat, 20 Apr 2013 16:08:34 +0000 Subject: [PATCH] fix module loader. admin sets config keys onload. I am incredibly annoyed. [#331] --- modules/admin/admin.js | 19 ++++++++++ modules/admin/commands.js | 10 ++++-- run.js | 76 ++++++++++++++++++--------------------- 3 files changed, 61 insertions(+), 44 deletions(-) diff --git a/modules/admin/admin.js b/modules/admin/admin.js index 23feda0..6923b19 100644 --- a/modules/admin/admin.js +++ b/modules/admin/admin.js @@ -36,6 +36,25 @@ var admin = function(dbot) { } }.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 " + 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(); }); } else { @@ -186,7 +189,10 @@ var commands = function(dbot) { if(_.isArray(config)) { event.reply(configPath + ": " + config + " << " + 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(); }); } else { diff --git a/run.js b/run.js index 2c7a21f..2568e96 100644 --- a/run.js +++ b/run.js @@ -131,12 +131,8 @@ DBot.prototype.reloadModules = function() { this.modules = {}; this.commands = {}; this.api = {}; - this.commandMap = {}; // Map of which commands belong to which modules this.usage = {}; - // Load config changes - _.extend(this.config, this.db.config); - try { this.strings = JSON.parse(fs.readFileSync('strings.json', 'utf-8')); } catch(err) { @@ -157,7 +153,6 @@ DBot.prototype.reloadModules = function() { require('./snippets'); this.instance.removeListeners(); - _.each(moduleNames, function(name) { this.status[name] = true; var moduleDir = './modules/' + name + '/'; @@ -207,13 +202,29 @@ DBot.prototype.reloadModules = function() { } 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 module = rawModule.fetch(this); this.rawModules.push(rawModule); module.name = name; - module.db = db; + module.db = this.ddb.databanks[name]; module.config = this.config[name]; // Load the module data @@ -276,45 +287,26 @@ DBot.prototype.reloadModules = function() { } this.modules[module.name] = module; - - if(_.has(this.modules, 'web')) this.modules.web.reloadPages(); - - _.each(this.modules, function(module, name) { - if(module.onLoad) { - try { - module.onLoad(); - } catch(err) { - this.status[name] = 'Error in onLoad: ' + err + ' ' + err.stack.split('\n')[1].trim(); - console.log('MODULE ONLOAD ERROR (' + name + '): ' + err ); - } - } - }, this); - }.bind(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)); - + + process.nextTick(function() { + if(_.has(this.modules, 'web')) this.modules.web.reloadPages(); + _.each(this.modules, function(module, name) { + if(module.onLoad) { + try { + module.onLoad(); + } catch(err) { + this.status[name] = 'Error in onLoad: ' + err + ' ' + err.stack.split('\n')[1].trim(); + console.log('MODULE ONLOAD ERROR (' + name + '): ' + err ); + } + } + }, this); + }.bind(this)); + this.save(); }; -// Load the module itself -DBot.prototype.loadModule = function(name, db) { - } - DBot.prototype.cleanNick = function(key) { key = key.toLowerCase(); while(key.endsWith("_")) {