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

@ -6,44 +6,6 @@ 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,80 +151,74 @@ 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)) {
var configPath = getCurrentConfig(configPathString);
if(configPath == false || _.isUndefined(configPath.value)) {
event.reply("Config key doesn't exist bro");
return;
}
var currentOption = configPath.value;
if(!_.include(noChangeConfig, configPath)) {
this.internalAPI.getCurrentConfig(configPath, function(config) {
if(config) {
// Convert to boolean type if config item boolean // Convert to boolean type if config item boolean
if(_.isBoolean(currentOption)) { if(_.isBoolean(config)) {
newOption = (newOption == "true"); newOption = (newOption == "true");
} }
if(_.isArray(currentOption)) { event.reply(configPath + ": " + config + " -> " + newOption);
event.reply("Config option is an array. Try 'pushconfig'."); config = newOption;
} this.db.save('config', configPath, { 'value': config }, function(err) {
event.reply(configPathString + ": " + currentOption + " -> " + newOption);
configPath['user'][configKey] = newOption;
dbot.reloadModules(); dbot.reloadModules();
});
} else {
event.reply('Config path doesn\'t exist, bro.');
}
}.bind(this));
} 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) {
if(!_.isArray(currentArray)) {
event.reply("Config option is not an array. Try 'setconfig'.");
return
}
event.reply(configPathString + ": " + currentArray + " << " + newOption);
currentArray.push(newOption);
dbot.reloadModules(); dbot.reloadModules();
});
} else {
event.reply("Config option is not an array. Try 'setconfig'.");
}
} else {
event.reply('Config path doesn\'t exist, bro.');
}
}.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));
}
if(_.isArray(configPath.value)) {
event.reply(configKey + ': ' + configPath.value);
} else if(_.isObject(configPath.value)) {
event.reply('Config keys in ' + configPathString + ': ' + Object.keys(configPath.value));
} else { } else {
event.reply(configKey + ': ' + configPath.value); event.reply(configPath + ': ' + config);
} }
} else { } else {
event.reply('Config keys in root: ' + Object.keys(configPath['default'])); event.reply('Config path doesn\'t exist, bro.');
}
});
} else {
event.reply('Config keys in root: ' + _.keys(dbot.config));
} }
} }
}; };
@ -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"
} }