forked from GitHub/dbot
fix db loading logic and check the file exists before attempting to load [#163]
This commit is contained in:
parent
447bcbabf8
commit
c1aa8db815
26
run.js
26
run.js
@ -6,23 +6,19 @@ require('./snippets');
|
|||||||
|
|
||||||
var DBot = function(timers) {
|
var DBot = function(timers) {
|
||||||
// Load DB
|
// Load DB
|
||||||
var rawDB;
|
if(fs.existsSync('db.json')) {
|
||||||
try {
|
try {
|
||||||
var rawDB = fs.readFileSync('db.json', 'utf-8');
|
this.db = JSON.parse(fs.readFileSync('db.json', 'utf-8'));
|
||||||
} catch(err) {
|
} catch(err) {
|
||||||
this.db = {}; // If no db file, make empty one
|
console.log('Error loading db.json. Stopping: ' + err);
|
||||||
|
process.exit();
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
this.db = {};
|
||||||
}
|
}
|
||||||
|
|
||||||
try {
|
if(!_.has(this.db, 'config')) {
|
||||||
if(!this.db) { // If it wasn't empty
|
this.db.config = {};
|
||||||
this.db = JSON.parse(rawDB);
|
|
||||||
}
|
|
||||||
if(!_.has(this.db, 'config')) {
|
|
||||||
this.db.config = {};
|
|
||||||
}
|
|
||||||
} catch(err) {
|
|
||||||
console.log('Syntax error in db.json. Stopping: ' + err);
|
|
||||||
process.exit();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Load config
|
// Load config
|
||||||
|
Loading…
Reference in New Issue
Block a user