aboutsummaryrefslogtreecommitdiffstats
path: root/jeeves/config.py
diff options
context:
space:
mode:
Diffstat (limited to 'jeeves/config.py')
-rw-r--r--jeeves/config.py98
1 files changed, 98 insertions, 0 deletions
diff --git a/jeeves/config.py b/jeeves/config.py
new file mode 100644
index 0000000..58d6484
--- /dev/null
+++ b/jeeves/config.py
@@ -0,0 +1,98 @@
+"""
+Sopel plugin: jeeves config
+
+ !set <key> <value> - set a config value (in channel: per-channel; in PM: global)
+ !config [#channel] - show config for a channel
+"""
+
+import os as _os, sys as _sys
+_d = _os.path.dirname(_os.path.abspath(__file__))
+if _d not in _sys.path:
+ _sys.path.insert(0, _d)
+import jv_core as jv
+
+from sopel import plugin
+
+
+def setup(bot):
+ jv.ensure_setup(bot)
+
+
+@plugin.command("set", "s")
+def cmd_set(bot, trigger):
+ if not jv.is_owner(bot, trigger) and not jv.is_op(bot, trigger):
+ return
+
+ args = (trigger.group(2) or "").split()
+ in_chan = str(trigger.sender).startswith("#")
+ channel = str(trigger.sender) if in_chan else None
+
+ if len(args) < 2:
+ keys = ", ".join(sorted(jv.CONFIG_KEYS))
+ bot.notice(f"usage: !set <key> <value> — keys: {keys}", trigger.nick)
+ return
+
+ key, raw = args[0].lower(), args[1]
+
+ if key not in jv.CONFIG_KEYS:
+ bot.notice(f"unknown key '{key}' — use !config to see valid keys", trigger.nick)
+ return
+
+ typ, _, global_only, _ = jv.CONFIG_KEYS[key]
+
+ if global_only and in_chan:
+ bot.notice(f"{key} is a global setting — use in PM to change it", trigger.nick)
+ return
+
+ if global_only and not jv.is_owner(bot, trigger):
+ return
+
+ try:
+ value = typ(raw)
+ except ValueError:
+ bot.notice(f"invalid value for {key}: expected {typ.__name__}", trigger.nick)
+ return
+
+ with bot.memory["jv_lock"]:
+ db = bot.memory["jv_db"]
+ if in_chan:
+ jv.chan_db(bot, channel).setdefault("config", {})[key] = value
+ bot.notice(f"set {key} = {value} for {channel}", trigger.nick)
+ else:
+ db.setdefault("config", {})[key] = value
+ bot.notice(f"set global {key} = {value}", trigger.nick)
+ jv.save(bot)
+
+
+@plugin.command("config", "cfg")
+def cmd_config(bot, trigger):
+ if not jv.is_owner(bot, trigger) and not jv.is_op(bot, trigger):
+ return
+
+ in_chan = str(trigger.sender).startswith("#")
+ arg = (trigger.group(2) or "").strip()
+
+ if arg.startswith("#"):
+ channel = arg
+ elif in_chan:
+ channel = str(trigger.sender)
+ else:
+ bot.notice("usage: !config [#channel]", trigger.nick)
+ return
+
+ db = bot.memory.get("jv_db", {})
+ global_cfg = db.get("config", {})
+ chan_cfg = db.get("channels", {}).get(channel.lower(), {}).get("config", {})
+
+ bot.say(f"config for {channel}:", trigger.nick)
+ for key in sorted(jv.CONFIG_KEYS):
+ _, default, global_only, desc = jv.CONFIG_KEYS[key]
+ g_val = global_cfg.get(key)
+ c_val = chan_cfg.get(key)
+ scope = "global-only" if global_only else "per-channel"
+ if c_val is not None:
+ bot.say(f"{key} = {c_val} (channel; global: {g_val if g_val is not None else default}) [{scope}]", trigger.nick)
+ elif g_val is not None:
+ bot.say(f"{key} = {g_val} (global; default: {default}) [{scope}]", trigger.nick)
+ else:
+ bot.say(f"{key} = {default} (default) [{scope}]", trigger.nick)