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
This commit is contained in:
Luke Slater 2012-12-12 18:25:07 +00:00
parent c64e66da17
commit a431d4c636
2 changed files with 22 additions and 25 deletions

View File

@ -13,5 +13,7 @@
] ]
} }
}, },
"admin": [ "batman" ] "admin": [ "batman" ],
"moduleNames": [ "ignore", "admin", "command", "dice", "js", "kick", "puns", "quotes", "spelling", "youare" ],
"language": "english"
} }

41
run.js
View File

@ -5,8 +5,18 @@ require('./snippets');
var DBot = function(timers) { var DBot = function(timers) {
// Load external files // Load external files
try {
this.config = JSON.parse(fs.readFileSync('config.json', 'utf-8')); this.config = JSON.parse(fs.readFileSync('config.json', 'utf-8'));
this.db = null; } 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; var rawDB;
try { try {
var rawDB = fs.readFileSync('db.json', 'utf-8'); var rawDB = fs.readFileSync('db.json', 'utf-8');
@ -19,15 +29,15 @@ var DBot = function(timers) {
this.db = JSON.parse(rawDB); this.db = JSON.parse(rawDB);
} }
} catch(err) { } catch(err) {
console.log('Probably a syntax error in db.json: ' + err); console.log('Syntax error in db.json. Stopping: ' + err);
this.db = {}; process.exit();
} }
// Load Strings file // Load Strings file
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) {
console.log('Probably a syntax error: ' + err); console.log('Probably a syntax error in strings.json: ' + err);
this.strings = {}; this.strings = {};
} }
@ -37,26 +47,11 @@ var DBot = function(timers) {
this.timers = timers.create(); this.timers = timers.create();
// Populate bot properties with config data // Populate bot properties with config data
this.name = this.config.name || 'dbox'; for(var configKey in this.config) {
this.admin = this.config.admin || [ 'reality' ]; if(this.config.hasOwnProperty(configKey) && this.hasOwnProperty(configKey) === false) {
this.moduleNames = this.config.modules || [ 'ignore', 'admin', 'command', 'dice', 'js', 'kick', 'puns', 'quotes', 'spelling', 'youare' ]; this[configKey] = this.config[configKey];
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'
]
} }
};
// Create JSBot and connect to each server // Create JSBot and connect to each server
this.instance = jsbot.createJSBot(this.name); this.instance = jsbot.createJSBot(this.name);