getCurrentConfig, pushconfig, showconfig and setconfig databankerised [#331]

This commit is contained in:
reality 2013-04-20 13:50:47 +00:00
parent 4319ee18e1
commit 466cb12c94
3 changed files with 89 additions and 100 deletions

View File

@ -7,6 +7,35 @@ var fs = require('fs'),
_ = require('underscore')._; _ = require('underscore')._;
var admin = function(dbot) { var admin = function(dbot) {
this.internalAPI = {
'getCurrentConfig': function(configKey, callback) {
if(configKey) {
this.db.read('config', configKey, function(err, cRecord) {
if(cRecord) {
callback(cRecord.value)
} else {
var configPath = dbot.config;
configKey = configKey.split('.');
for(var i=0;i<configKey.length;i++) {
if(_.has(configPath, configKey[i])) {
configPath = configPath[configKey[i]];
} else {
callback(false);
break;
}
}
process.nextTick(function() {
callback(configPath);
});
}
});
} else {
callback(dbot.config);
}
}.bind(this)
};
}; };
exports.fetch = function(dbot) { exports.fetch = function(dbot) {

View File

@ -5,45 +5,7 @@ var fs = require('fs'),
var commands = function(dbot) { var commands = function(dbot) {
var noChangeConfig = [ 'servers', 'name', 'moduleNames' ]; var noChangeConfig = [ 'servers', 'name', 'moduleNames' ];
var getCurrentConfig = function(configKey) {
var defaultConfigPath = dbot.config;
var userConfigPath = dbot.db.config;
if(configKey) {
var configKey = configKey.split('.');
for(var i=0;i<configKey.length-1;i++) {
if(_.has(defaultConfigPath, configKey[i])) {
if(!_.has(userConfigPath, configKey[i])) {
userConfigPath[configKey[i]] = {};
}
userConfigPath = userConfigPath[configKey[i]];
defaultConfigPath = defaultConfigPath[configKey[i]];
} else {
return false;
}
}
}
var currentOption;
if(configKey && configKey.length != 1) {
configKey = _.last(configKey);
if(_.has(userConfigPath, configKey) && !_.isUndefined(userConfigPath[configKey])) {
currentOption = userConfigPath[configKey];
} else if(_.has(defaultConfigPath, configKey)) {
currentOption = defaultConfigPath[configKey];
}
} else {
currentOption = defaultConfigPath[configKey];
}
return {
'user': userConfigPath,
'default': defaultConfigPath,
'value': currentOption
};
};
var commands = { var commands = {
// Join a channel // Join a channel
'join': function(event) { 'join': function(event) {
@ -189,82 +151,76 @@ var commands = function(dbot) {
/*** Config options ***/ /*** Config options ***/
'setconfig': function(event) { 'setconfig': function(event) {
var configPathString = event.params[1], var configPath = event.input[1],
configKey = _.last(configPathString.split('.')), newOption = event.input[2];
newOption = event.params[2];
if(!_.include(noChangeConfig, configKey)) { if(!_.include(noChangeConfig, configPath)) {
var configPath = getCurrentConfig(configPathString); this.internalAPI.getCurrentConfig(configPath, function(config) {
if(config) {
// Convert to boolean type if config item boolean
if(_.isBoolean(config)) {
newOption = (newOption == "true");
}
if(configPath == false || _.isUndefined(configPath.value)) { event.reply(configPath + ": " + config + " -> " + newOption);
event.reply("Config key doesn't exist bro"); config = newOption;
return; this.db.save('config', configPath, { 'value': config }, function(err) {
} dbot.reloadModules();
var currentOption = configPath.value; });
} else {
// Convert to boolean type if config item boolean event.reply('Config path doesn\'t exist, bro.');
if(_.isBoolean(currentOption)) { }
newOption = (newOption == "true"); }.bind(this));
}
if(_.isArray(currentOption)) {
event.reply("Config option is an array. Try 'pushconfig'.");
}
event.reply(configPathString + ": " + currentOption + " -> " + newOption);
configPath['user'][configKey] = newOption;
dbot.reloadModules();
} else { } else {
event.reply("This config option cannot be altered while the bot is running."); event.reply("This config option cannot be altered while the bot is running.");
} }
}, },
'pushconfig': function(event) { 'pushconfig': function(event) {
var configPathString = event.params[1], var configPath = event.input[1],
configKey = _.last(configPathString.split('.')), newOption = event.input[2];
newOption = event.params[2];
if(!_.include(noChangeConfig, configKey)) { if(!_.include(noChangeConfig, configPath)) {
var configPath = getCurrentConfig(configPathString); this.internalAPI.getCurrentConfig(configPath, function(config) {
if(configPath == false || _.isUndefined(configPath.value)) { if(config) {
event.reply("Config key doesn't exist bro"); if(_.isArray(config)) {
return; event.reply(configPath + ": " + config + " << " + newOption);
} config.push(newOption);
var currentArray = configPath.value; this.db.save('config', configPath, { 'value': config }, function(err) {
dbot.reloadModules();
if(!_.isArray(currentArray)) { });
event.reply("Config option is not an array. Try 'setconfig'."); } else {
return event.reply("Config option is not an array. Try 'setconfig'.");
} }
} else {
event.reply(configPathString + ": " + currentArray + " << " + newOption); event.reply('Config path doesn\'t exist, bro.');
currentArray.push(newOption); }
dbot.reloadModules(); }.bind(this));
} else {
event.reply("This config option cannot be altered while the bot is running.");
} }
}, },
'showconfig': function(event) { 'showconfig': function(event) {
var configPathString = event.params[1]; var configPath = event.params[1];
var configPath = getCurrentConfig(configPathString); if(configPath) {
this.internalAPI.getCurrentConfig(configPath, function(config) {
if(configPathString) { if(config) {
var configKey = _.last(configPathString.split('.')); if(_.isArray(config)) {
if(configKey == false) { event.reply('Config keys in ' + configPath + ': ' + config);
event.reply("Config path doesn't exist"); } else if(_.isObject(config)) {
return; event.reply('Config keys in ' + configPath + ': ' + _.keys(config));
} } else {
event.reply(configPath + ': ' + config);
if(_.isArray(configPath.value)) { }
event.reply(configKey + ': ' + configPath.value); } else {
} else if(_.isObject(configPath.value)) { event.reply('Config path doesn\'t exist, bro.');
event.reply('Config keys in ' + configPathString + ': ' + Object.keys(configPath.value)); }
} else { });
event.reply(configKey + ': ' + configPath.value);
}
} else { } else {
event.reply('Config keys in root: ' + Object.keys(configPath['default'])); event.reply('Config keys in root: ' + _.keys(dbot.config));
} }
} }
}; };
_.each(commands, function(command) { _.each(commands, function(command) {
@ -277,6 +233,9 @@ var commands = function(dbot) {
commands['opme'].access = 'moderator'; commands['opme'].access = 'moderator';
commands['say'].access = 'moderator'; commands['say'].access = 'moderator';
commands['pushconfig'].regex = [/pushconfig ([^ ]+) ([^ ]+)/, 3];
commands['setconfig'].regex = [/setconfig ([^ ]+) ([^ ]+)/, 3];
return commands; return commands;
}; };

View File

@ -1,5 +1,6 @@
{ {
"ignorable": false, "ignorable": false,
"dbType": "redis",
"dependencies": [ "command" ], "dependencies": [ "command" ],
"help": "http://github.com/reality/depressionbot/blob/master/modules/admin/README.md" "help": "http://github.com/reality/depressionbot/blob/master/modules/admin/README.md"
} }