From af20b5cbfb9daeae50d90d78f2cd56f15d10dd28 Mon Sep 17 00:00:00 2001 From: reality Date: Sat, 12 Jan 2013 15:07:14 +0000 Subject: [PATCH] Load missing config directives from defaults file --- run.js | 31 ++++++++++++++----------------- 1 file changed, 14 insertions(+), 17 deletions(-) diff --git a/run.js b/run.js index 8e08e42..8e03fe9 100644 --- a/run.js +++ b/run.js @@ -5,26 +5,23 @@ var fs = require('fs'), require('./snippets'); var DBot = function(timers) { - // Load external files - var requiredConfigKeys = [ 'name', 'servers', 'admins', 'moderators', 'moduleNames', 'language', 'debugMode' ]; + // Load config try { this.config = JSON.parse(fs.readFileSync('config.json', 'utf-8')); } catch(err) { - console.log('Config file is screwed up. Attempting to load defaults.'); - try { - this.config = JSON.parse(fs.readFileSync('config.json.sample', 'utf-8')); - } catch(err) { - console.log('Error loading sample config. Bugger off. Stopping.'); - process.exit(); - } + console.log('Config file is invalid. Stopping'); + process.exit(); } - _.each(requiredConfigKeys, function(key) { - if(!_.has(this.config, key)) { - console.log('Error: Please set a value for ' + key + ' in ' + - 'config.json. Stopping.'); - process.exit(); - } - }, this); + + try { + var defaultConfig = JSON.parse(fs.readFileSync('config.json.sample', 'utf-8')); + } catch(err) { + console.log('Error loading sample config. Bugger off this should not even be edited. Stopping.'); + process.exit(); + } + + // Load missing config directives from sample file + _.defaults(this.config, defaultConfig); var rawDB; try { @@ -172,7 +169,7 @@ DBot.prototype.reloadModules = function() { this.config[name] = config; _.each(config.dbKeys, function(dbKey) { - if(!_.has(this.db, dbKey( { + if(!_.has(this.db, dbKey)) { this.db[dbKey] = {}; } }, this);