From 2921ed77aef7a21ba148d43eb12c8581b95b080e Mon Sep 17 00:00:00 2001 From: jesopo Date: Tue, 6 Nov 2018 13:01:30 +0000 Subject: [PATCH] Support arrays of user/channel/bot/server settings --- modules/github.py | 3 +-- modules/set.py | 51 ++++++++++++++++++++++++++++++++++------------- 2 files changed, 38 insertions(+), 16 deletions(-) diff --git a/modules/github.py b/modules/github.py index 2ea4d412..62654b83 100644 --- a/modules/github.py +++ b/modules/github.py @@ -5,8 +5,7 @@ COMMIT_URL = "https://github.com/%s/commit/%s" @utils.export("channelset", {"setting": "github-hook", "help": ("Disable/Enable showing BitBot's github commits in the " - "current channel"), "validate": utils.bool_or_none, - "hidden": True}) + "current channel"), "hidden": True}) class Module(ModuleManager.BaseModule): @utils.hook("api.post.github") def github(self, event): diff --git a/modules/set.py b/modules/set.py index 738ac76d..0920da22 100644 --- a/modules/set.py +++ b/modules/set.py @@ -1,7 +1,11 @@ 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 Module(ModuleManager.BaseModule): - def _set(self, category, event, target): + def _set(self, category, event, target, array): settings = self.exports.get_all(category) settings_dict = dict([(setting["setting"], setting ) for setting in settings]) @@ -14,7 +18,13 @@ class Module(ModuleManager.BaseModule): lambda x: x)(value) if not value == None: - target.set_setting(setting, value) + if array: + current_value = target.get_setting(setting, []) + current_value.append(value) + target.set_setting(setting, current_value) + else: + target.set_setting(setting, value) + self.events.on("set").on(category).on(setting).call( value=value, target=target) @@ -31,45 +41,58 @@ class Module(ModuleManager.BaseModule): event["stdout"].write("Available settings: %s" % ( ", ".join(shown_settings))) - @utils.hook("received.command.set") + @utils.hook("received.command.set", help="Set a specified user setting") + @utils.hook("received.command.setadd", + help="Add to a specified user setting") def set(self, event): """ - :help: Set a specified user setting :usage: """ - self._set("set", event, event["user"]) + self._set("set", event, event["user"], event["command"]=="setadd") @utils.hook("received.command.channelset", channel_only=True, - require_mode="o") + require_mode="o", help=CHANNELSET_HELP) @utils.hook("received.command.channelsetoverride", channel_only=True, - permission="channelsetoverride") + permission="channelsetoverride", help=CHANNELSET_HELP) + @utils.hook("received.command.channelsetadd", channel_only=True, + require_mode="o", help=CHANNELSETADD_HELP) + @utils.hook("received.command.channelsetaddoverride", channel_only=True, + permission="channelsetoverride", help=CHANNELSETADD_HELP) def channel_set(self, event): """ - :help: Get a specified channel setting for the current channel :usage: """ - self._set("channelset", event, event["target"]) + self._set("channelset", event, event["target"], False, + event["command"].startswith("channelsetadd")) - @utils.hook("received.command.serverset") + @utils.hook("received.command.serverset", + help="Set a specified server setting for the current server") + @utils.hook("received.command.serversetadd", + help="Add to a specified server setting for the current server") def server_set(self, event): """ - :help: Set a specified server setting for the current server :usage: :permission: serverset """ - self._set("serverset", event, event["server"]) + self._set("serverset", event, event["server"], + event["command"]="serversetadd") - @utils.hook("received.command.botset") + @utils.hook("received.command.botset", help="Set a specified bot setting") + @utils.hook("received.command.botsetadd", + help="Add to a specified bot setting") def bot_set(self, event): """ :help: Set a specified bot setting :usage: :permission: botset """ - self._set("botset", event, self.bot) + self._set("botset", event, self.bot, False, + event["command"]=="botsetadd") def _get(self, event, setting, qualifier, value): if not value == None: + if array: + value = ", ".join(value) event["stdout"].write("'%s'%s: %s" % (setting, qualifier, str(value))) else: