3
0
mirror of https://github.com/reality/dbot.git synced 2025-02-02 23:54:19 +01:00

Load module config options from db store [#145]

This commit is contained in:
reality 2013-01-20 14:45:58 +00:00
parent 94363a6d8f
commit a2ea1b8c33

14
run.js
View File

@ -34,6 +34,9 @@ var DBot = function(timers) {
if(!this.db) { // If it wasn't empty if(!this.db) { // If it wasn't empty
this.db = JSON.parse(rawDB); this.db = JSON.parse(rawDB);
} }
if(!_.has(this.db, 'config')) {
this.db.config = {};
}
} catch(err) { } catch(err) {
console.log('Syntax error in db.json. Stopping: ' + err); console.log('Syntax error in db.json. Stopping: ' + err);
process.exit(); process.exit();
@ -157,12 +160,19 @@ DBot.prototype.reloadModules = function() {
try { try {
// Load the module config data // Load the module config data
var config = {}; var config = {};
if(_.has(this.db.config, name)) {
config = this.db.config;
}
try { try {
config = JSON.parse(fs.readFileSync(moduleDir + 'config.json', 'utf-8')); var defaultConfig = JSON.parse(fs.readFileSync(moduleDir + 'config.json', 'utf-8'));
config = _.defaults(config, defaultConfig);
} catch(err) { } catch(err) {
// Invalid or no config data // Invalid or no config data
} }
// Load module config
this.config[name] = config; this.config[name] = config;
_.each(config.dbKeys, function(dbKey) { _.each(config.dbKeys, function(dbKey) {
if(!_.has(this.db, dbKey)) { if(!_.has(this.db, dbKey)) {
@ -170,6 +180,8 @@ DBot.prototype.reloadModules = function() {
} }
}, this); }, this);
// Override module config with any stored in the DB
// Load the module itself // Load the module itself
var rawModule = require(moduleDir + name); var rawModule = require(moduleDir + name);
var module = rawModule.fetch(this); var module = rawModule.fetch(this);