Add !config command, to group all settings activities in to 1 command
This commit is contained in:
parent
272b60842e
commit
d4c7c41c79
1 changed files with 106 additions and 0 deletions
106
modules/set.py
106
modules/set.py
|
@ -1,9 +1,36 @@
|
|||
import enum
|
||||
from src import ModuleManager, utils
|
||||
|
||||
CHANNELSET_HELP = "Get a specified channel setting for the current channel"
|
||||
CHANNELSETADD_HELP = ("Add to a specified channel setting for the "
|
||||
"current channel")
|
||||
|
||||
class ConfigInvalidValue(Exception):
|
||||
pass
|
||||
|
||||
class ConfigResults(enum.Enum):
|
||||
Changed = 1
|
||||
Retrieved = 2
|
||||
class ConfigResult(object):
|
||||
def __init__(self, result, data=None):
|
||||
self.result = result
|
||||
self.data = data
|
||||
|
||||
class ConfigChannelTarget(object):
|
||||
def __init__(self, bot, server, channel_name):
|
||||
self._bot = bot
|
||||
self._server = server
|
||||
self._channel_name = channel_name
|
||||
def _get_id(self):
|
||||
return self._server.channels.get_id(self._channel_name)
|
||||
def set_setting(self, setting, value):
|
||||
channel_id = self._get_id()
|
||||
self._bot.database.channel_settings.set(channel_id, setting, value)
|
||||
def get_setting(self, setting, default=None):
|
||||
channel_id = self._get_id()
|
||||
return self._bot.database.channel_settings.get(channel_id, setting,
|
||||
default)
|
||||
|
||||
class Module(ModuleManager.BaseModule):
|
||||
def _set(self, category, event, target, array, arg_index=0):
|
||||
args = event["args_split"][arg_index:]
|
||||
|
@ -185,3 +212,82 @@ class Module(ModuleManager.BaseModule):
|
|||
:permission: channelsetoverride
|
||||
"""
|
||||
self._unset(event, event["args_split"][0], "channelset", event["user"])
|
||||
|
||||
def _get_export_setting(self, context):
|
||||
settings = self.exports.get_all(context)
|
||||
return {setting["setting"].lower(): setting for setting in settings}
|
||||
|
||||
def _config(self, export_settings, target, setting, value=None):
|
||||
if not value == None:
|
||||
validation = export_settings.get("validate", lambda x: x)
|
||||
validated_value = validation(value)
|
||||
if not validated_value == None:
|
||||
target.set_setting(setting, validated_value)
|
||||
return ConfigResult(ConfigResults.Changed)
|
||||
else:
|
||||
raise ConfigInvalidValue()
|
||||
else:
|
||||
existing_value = target.get_setting(setting, None)
|
||||
return ConfigResult(ConfigResults.Retrieved, existing_value)
|
||||
|
||||
@utils.hook("received.command.config", min_args=1)
|
||||
def config(self, event):
|
||||
"""
|
||||
:help: Change config options
|
||||
:usage: <context>[:name] [setting [value]]
|
||||
:permission: config
|
||||
"""
|
||||
|
||||
arg_count = len(event["args_split"])
|
||||
context, _, name = event["args_split"][0].partition(":")
|
||||
context = context.lower()
|
||||
|
||||
setting = None
|
||||
value = None
|
||||
if arg_count > 1:
|
||||
setting = event["args_split"][1].lower()
|
||||
if arg_count > 2:
|
||||
value = " ".join(event["args_split"][2:])
|
||||
|
||||
target = None
|
||||
setting_key = None
|
||||
|
||||
if context == "user":
|
||||
setting_key = "set"
|
||||
if name:
|
||||
target = event["server"].get_user(name)
|
||||
else:
|
||||
target = event["user"]
|
||||
elif context == "channel":
|
||||
setting_key = "channelset"
|
||||
if name:
|
||||
if name in event["server"].channels:
|
||||
target = event["server"].channels.get(name)
|
||||
else:
|
||||
target = ConfigChannelTarget(self.bot, event["server"],
|
||||
name)
|
||||
else:
|
||||
target = event["target"]
|
||||
elif context == "server":
|
||||
setting_key = "serverset"
|
||||
target = event["server"]
|
||||
elif context == "bot":
|
||||
setting_key = "botset"
|
||||
target = self.bot
|
||||
|
||||
export_settings = self._get_export_setting(setting_key)
|
||||
if not setting == None:
|
||||
if not setting in export_settings:
|
||||
raise utils.EventError("Setting not found")
|
||||
|
||||
try:
|
||||
result = self._config(export_settings, target, setting, value)
|
||||
except ConfigInvalidValue:
|
||||
raise utils.EventError("Invalid value")
|
||||
if result.result == ConfigResults.Changed:
|
||||
event["stdout"].write("Config changed")
|
||||
elif result.result == ConfigResults.Retrieved:
|
||||
event["stdout"].write("%s: %s" % (setting, result.data))
|
||||
else:
|
||||
event["stdout"].write("Available config: %s" %
|
||||
", ".join(export_settings.keys()))
|
||||
|
|
Loading…
Reference in a new issue