create getcurrentconfigpath for use by other commands

This commit is contained in:
reality 2013-01-20 15:49:31 +00:00
parent b8b9081e3f
commit 2607ce1e93

View File

@ -4,6 +4,29 @@ var fs = require('fs'),
exec = require('child_process').exec; exec = require('child_process').exec;
var commands = function(dbot) { var commands = function(dbot) {
var getCurrentConfigPath = function(configKey) {
var configKey = configKey.split('.');
defaultConfigPath = dbot.config;
userConfigPath = dbot.db.config;
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;
}
}
return {
'user': userConfigPath,
'default': defaultConfigPath
};
};
var commands = { var commands = {
// Join a channel // Join a channel
'join': function(event) { 'join': function(event) {
@ -154,40 +177,34 @@ var commands = function(dbot) {
/*** Config options ***/ /*** Config options ***/
'setconfig': function(event) { 'setconfig': function(event) {
var configKey = event.params[1]; var configPathString = event.params[1];
var configKey = _.last(configPathString.split('.'));
var newOption = event.params[2]; var newOption = event.params[2];
configKey = configKey.split('.'); var configPath = getCurrentConfigPath(configPathString);
defaultConfigPath = dbot.config; var currentOption;
userConfigPath = dbot.db.config; if(_.has(configPath['user'], configKey)) {
for(var i=0;i<configKey.length-1;i++) { currentOption = configPath['user'][configKey];
if(_.has(defaultConfigPath, configKey[i])) { } else if(_.has(configPath['default'], configKey)) {
if(!_.has(userConfigPath, configKey[i])) { currentOption = configPath['default'][configKey];
userConfigPath[configKey[i]] = {};
}
userConfigPath = userConfigPath[configKey[i]];
defaultConfigPath = defaultConfigPath[configKey[i]];
} else { } else {
event.reply("Config path doesn't exist."); event.reply("Config key doesn't exist bro");
return; return;
} }
}
var configItem = _.last(configKey);
var currentOption = defaultConfigPath[configItem];
if(_.has(userConfigPath, configItem)) {
currentOption = userConfigPath[configItem];
}
// Convert to boolean type if config item boolean // Convert to boolean type if config item boolean
if(_.isBoolean(currentOption)) { if(_.isBoolean(currentOption)) {
newOption = (newOption == "true"); newOption = (newOption == "true");
} }
if(_.isArray(currentOption)) {
event.reply("Config option is an array. Try 'pushconfig'.");
}
// TODO: Same for numbers and that I assume // TODO: Same for numbers and that I assume
event.reply(event.params[1] + ": " + currentOption + " -> " + newOption); event.reply(configPathString + ": " + currentOption + " -> " + newOption);
userConfigPath[configItem] = newOption; userConfigPath[configKey] = newOption;
dbot.reloadModules(); dbot.reloadModules();
} }
}; };
@ -196,6 +213,7 @@ var commands = function(dbot) {
commands['reload'].access = 'admin'; commands['reload'].access = 'admin';
commands['unload'].access = 'admin'; commands['unload'].access = 'admin';
commands['load'].access = 'admin'; commands['load'].access = 'admin';
commands['setconfig'].access = 'admin';
commands['join'].access = 'moderator'; commands['join'].access = 'moderator';
commands['part'].access = 'moderator'; commands['part'].access = 'moderator';
commands['opme'].access = 'moderator'; commands['opme'].access = 'moderator';