From a431d4c636bf551cd7fd7a93b0b32932ec47a59a Mon Sep 17 00:00:00 2001 From: Luke Slater Date: Wed, 12 Dec 2012 18:25:07 +0000 Subject: [PATCH] Config defaults and file loading changes * Attempts to load config from default file upon failing to load config.json * Stops if db fails to load * Automatically loads config keys into dbot object --- config.json.sample | 4 +++- run.js | 43 +++++++++++++++++++------------------------ 2 files changed, 22 insertions(+), 25 deletions(-) diff --git a/config.json.sample b/config.json.sample index d665e9c..a7232d0 100644 --- a/config.json.sample +++ b/config.json.sample @@ -13,5 +13,7 @@ ] } }, - "admin": [ "batman" ] + "admin": [ "batman" ], + "moduleNames": [ "ignore", "admin", "command", "dice", "js", "kick", "puns", "quotes", "spelling", "youare" ], + "language": "english" } diff --git a/run.js b/run.js index 661e3d7..69dc469 100644 --- a/run.js +++ b/run.js @@ -5,8 +5,18 @@ require('./snippets'); var DBot = function(timers) { // Load external files - this.config = JSON.parse(fs.readFileSync('config.json', 'utf-8')); - this.db = null; + 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(); + } + } + var rawDB; try { var rawDB = fs.readFileSync('db.json', 'utf-8'); @@ -19,15 +29,15 @@ var DBot = function(timers) { this.db = JSON.parse(rawDB); } } catch(err) { - console.log('Probably a syntax error in db.json: ' + err); - this.db = {}; + console.log('Syntax error in db.json. Stopping: ' + err); + process.exit(); } // Load Strings file try { this.strings = JSON.parse(fs.readFileSync('strings.json', 'utf-8')); } catch(err) { - console.log('Probably a syntax error: ' + err); + console.log('Probably a syntax error in strings.json: ' + err); this.strings = {}; } @@ -37,26 +47,11 @@ var DBot = function(timers) { this.timers = timers.create(); // Populate bot properties with config data - this.name = this.config.name || 'dbox'; - this.admin = this.config.admin || [ 'reality' ]; - this.moduleNames = this.config.modules || [ 'ignore', 'admin', 'command', 'dice', 'js', 'kick', 'puns', 'quotes', 'spelling', 'youare' ]; - this.language = this.config.language || 'english'; - this.webHost = this.config.webHost || 'localhost'; - this.webPort = this.config.webPort || 80; - - // It's the user's responsibility to fill this data structure up properly in - // the config file. They can d-d-d-deal with it if they have problems. - this.servers = this.config.servers || { - 'freenode': { - 'server': 'irc.freenode.net', - 'port': 6667, - 'nickserv': 'nickserv', - 'password': 'lolturtles', - 'channels': [ - '#realitest' - ] + for(var configKey in this.config) { + if(this.config.hasOwnProperty(configKey) && this.hasOwnProperty(configKey) === false) { + this[configKey] = this.config[configKey]; } - }; + } // Create JSBot and connect to each server this.instance = jsbot.createJSBot(this.name);