status command and some basic error storing in module load [#198]

This commit is contained in:
reality 2013-01-23 22:32:17 +00:00
parent c77cb09f87
commit 045ff8b9f8
2 changed files with 32 additions and 4 deletions

View File

@ -102,6 +102,21 @@ var commands = function(dbot) {
}.bind(this)); }.bind(this));
}, },
'status': function(event) {
var moduleName = event.params[1];
if(_.has(dbot.status, moduleName)) {
var status = dbot.status[moduleName];
if(status === true) {
event.reply(moduleName + ' status: Shit looks good.');
} else {
event.reply(moduleName + ' status: Failed to load: ' + status);
}
} else {
event.reply('Either that module wasn\'t on the roster or shit is totally fucked.');
}
},
// Reload DB, translations and modules. // Reload DB, translations and modules.
'reload': function(event) { 'reload': function(event) {
dbot.db = JSON.parse(fs.readFileSync('db.json', 'utf-8')); dbot.db = JSON.parse(fs.readFileSync('db.json', 'utf-8'));
@ -125,7 +140,11 @@ var commands = function(dbot) {
if(!_.include(dbot.config.moduleNames, moduleName)) { if(!_.include(dbot.config.moduleNames, moduleName)) {
dbot.config.moduleNames.push(moduleName); dbot.config.moduleNames.push(moduleName);
dbot.reloadModules(); dbot.reloadModules();
event.reply(dbot.t('load_module', {'moduleName': moduleName})); if(dbot.status[moduleName] === true) {
event.reply(dbot.t('load_module', {'moduleName': moduleName}));
} else {
event.reply('Failed to load ' + moduleName + '. See \'status ' + moduleName + '\'.');
}
} else { } else {
if(moduleName == 'web') { if(moduleName == 'web') {
event.reply(dbot.t('already_loaded_web')); event.reply(dbot.t('already_loaded_web'));

15
run.js
View File

@ -5,7 +5,6 @@ var fs = require('fs'),
require('./snippets'); require('./snippets');
var DBot = function(timers) { var DBot = function(timers) {
// Load DB // Load DB
var rawDB; var rawDB;
try { try {
@ -55,6 +54,7 @@ var DBot = function(timers) {
// Initialise run-time resources // Initialise run-time resources
this.usage = {}; this.usage = {};
this.status = {};
this.sessionData = {}; this.sessionData = {};
this.timers = timers.create(); this.timers = timers.create();
@ -119,6 +119,7 @@ DBot.prototype.reloadModules = function() {
this.rawModules = []; this.rawModules = [];
this.pages = {}; this.pages = {};
this.status = {};
this.modules = {}; this.modules = {};
this.commands = {}; this.commands = {};
this.api = {}; this.api = {};
@ -151,6 +152,7 @@ DBot.prototype.reloadModules = function() {
this.instance.removeListeners(); this.instance.removeListeners();
moduleNames.each(function(name) { moduleNames.each(function(name) {
this.status[name] = true;
var moduleDir = './modules/' + name + '/'; var moduleDir = './modules/' + name + '/';
var cacheKey = require.resolve(moduleDir + name); var cacheKey = require.resolve(moduleDir + name);
delete require.cache[cacheKey]; delete require.cache[cacheKey];
@ -172,7 +174,13 @@ DBot.prototype.reloadModules = function() {
} }
try { try {
var defaultConfig = JSON.parse(fs.readFileSync(moduleDir + 'config.json', 'utf-8')); var defaultConfig = fs.readFileSync(moduleDir + 'config.json', 'utf-8');
try {
defaultConfig = JSON.parse(defaultConfig);
} catch(err) { // syntax error
this.status[name] = 'Error parsing config: ' + err + ' ' + err.stack.split('\n')[2].trim();
return;
}
config = _.defaults(config, defaultConfig); config = _.defaults(config, defaultConfig);
} catch(err) { } catch(err) {
// Invalid or no config data // Invalid or no config data
@ -188,7 +196,7 @@ DBot.prototype.reloadModules = function() {
}, [], this); }, [], this);
if(unmetDependencies.length != 0) { if(unmetDependencies.length != 0) {
throw new Error("Dependencies not met: " + unmetDependencies); this.status[name] = 'Dependencies not met: ' + unmetDependencies;
return; return;
} }
} }
@ -265,6 +273,7 @@ DBot.prototype.reloadModules = function() {
this.modules[module.name] = module; this.modules[module.name] = module;
} catch(err) { } catch(err) {
console.log(this.t('module_load_error', {'moduleName': name})); console.log(this.t('module_load_error', {'moduleName': name}));
this.status[name] = err + ' - ' + err.stack.split('\n')[1].trim();
if(this.config.debugMode) { if(this.config.debugMode) {
console.log('MODULE ERROR (' + name + '): ' + err.stack ); console.log('MODULE ERROR (' + name + '): ' + err.stack );
} else { } else {