2016-03-29 11:56:58 +00:00
|
|
|
|
|
|
|
|
|
|
|
class Module(object):
|
2018-08-31 11:55:52 +00:00
|
|
|
def __init__(self, bot, events):
|
2016-03-29 11:56:58 +00:00
|
|
|
self.bot = bot
|
|
|
|
self.settings = {}
|
|
|
|
self.channel_settings = {}
|
2018-08-31 11:55:52 +00:00
|
|
|
|
|
|
|
events.on("postboot").on("configure").on("set").hook(
|
2017-12-26 10:32:36 +00:00
|
|
|
self.postboot_set, replay=True)
|
2018-08-31 11:55:52 +00:00
|
|
|
events.on("postboot").on("configure").on("channelset"
|
2017-12-26 10:32:36 +00:00
|
|
|
).hook(self.postboot_channelset, replay=True)
|
2018-08-28 13:55:57 +00:00
|
|
|
|
2018-08-31 11:55:52 +00:00
|
|
|
events.on("received").on("command").on("set").hook(
|
2016-04-05 17:41:33 +00:00
|
|
|
self.set, help="Set a specified user setting",
|
|
|
|
usage="<setting> <value>")
|
2018-08-31 11:55:52 +00:00
|
|
|
events.on("received").on("command").on("get").hook(
|
2018-08-08 19:45:57 +00:00
|
|
|
self.get, help="Get a specified user setting",
|
|
|
|
usage="<setting>", min_args=1)
|
2018-08-28 13:55:57 +00:00
|
|
|
|
2018-08-31 11:55:52 +00:00
|
|
|
events.on("received").on("command").on("channelset"
|
2016-03-29 13:37:35 +00:00
|
|
|
).hook(self.channel_set, channel_only=True,
|
2016-04-05 17:41:33 +00:00
|
|
|
help="Set a specified setting for the current channel",
|
2018-08-08 19:45:57 +00:00
|
|
|
usage="<setting> <value>", require_mode="o")
|
2018-08-31 11:55:52 +00:00
|
|
|
events.on("received").on("command").on("channelsetoverride"
|
2018-08-28 13:55:57 +00:00
|
|
|
).hook(self.channel_set, channel_only=True,
|
|
|
|
help="Set a specified setting for the current channel",
|
|
|
|
usage="<setting> <value>", permission="channelsetoverride")
|
2018-08-31 11:55:52 +00:00
|
|
|
events.on("received").on("command").on("channelget"
|
2018-08-08 19:45:57 +00:00
|
|
|
).hook(self.channel_get, channel_only=True,
|
|
|
|
help="Get a specified setting for the current channel",
|
|
|
|
usage="<setting>", min_args=1, require_mode="o")
|
2016-03-29 11:56:58 +00:00
|
|
|
|
|
|
|
def _postboot_set(self, settings, event):
|
|
|
|
settings[event["setting"]] = {}
|
|
|
|
settings[event["setting"]]["validate"] = event.get(
|
|
|
|
"validate", lambda s: s)
|
|
|
|
settings[event["setting"]]["help"] = event.get("help",
|
|
|
|
"")
|
|
|
|
def postboot_set(self, event):
|
|
|
|
self._postboot_set(self.settings, event)
|
|
|
|
def postboot_channelset(self, event):
|
|
|
|
self._postboot_set(self.channel_settings, event)
|
|
|
|
|
2016-04-03 12:20:05 +00:00
|
|
|
def _set(self, settings, event, target):
|
2016-03-29 11:56:58 +00:00
|
|
|
if len(event["args_split"]) > 1:
|
|
|
|
setting = event["args_split"][0].lower()
|
|
|
|
if setting in settings:
|
|
|
|
value = " ".join(event["args_split"][1:])
|
|
|
|
value = settings[setting]["validate"](value)
|
|
|
|
if not value == None:
|
2016-04-03 12:20:05 +00:00
|
|
|
target.set_setting(setting, value)
|
2016-03-29 11:56:58 +00:00
|
|
|
event["stdout"].write("Saved setting")
|
|
|
|
else:
|
|
|
|
event["stderr"].write("Invalid value")
|
|
|
|
else:
|
|
|
|
event["stderr"].write("Unknown setting")
|
|
|
|
elif len(event["args_split"]) == 1:
|
|
|
|
event["stderr"].write("Please provide a value")
|
|
|
|
else:
|
|
|
|
event["stdout"].write("Available settings: %s" % (
|
|
|
|
", ".join(settings.keys())))
|
|
|
|
def set(self, event):
|
2016-04-03 12:20:05 +00:00
|
|
|
self._set(self.settings, event, event["user"])
|
2016-03-29 11:56:58 +00:00
|
|
|
|
|
|
|
def channel_set(self, event):
|
2018-08-08 19:45:57 +00:00
|
|
|
self._set(self.channel_settings, event, event["target"])
|
|
|
|
|
|
|
|
def _get(self, event, setting, qualifier, value):
|
|
|
|
if not value == None:
|
|
|
|
event["stdout"].write("'%s'%s: %s" % (setting,
|
2018-08-08 19:49:34 +00:00
|
|
|
qualifier, str(value)))
|
2016-03-29 11:56:58 +00:00
|
|
|
else:
|
2018-08-08 19:45:57 +00:00
|
|
|
event["stdout"].write("'%s' has no value set" % setting)
|
|
|
|
|
|
|
|
def channel_get(self, event):
|
|
|
|
setting = event["args_split"][0]
|
2018-08-08 19:49:34 +00:00
|
|
|
self._get(event, setting, " for %s" % event["target"].name,
|
|
|
|
event["target"].get_setting(setting, None))
|
2018-08-08 19:45:57 +00:00
|
|
|
|
|
|
|
def get(self, event):
|
|
|
|
setting = event["args_split"][0]
|
|
|
|
self._get(event, setting, "", event["user"].get_setting(
|
|
|
|
setting, None))
|