aboutsummaryrefslogtreecommitdiffstats
path: root/jeeves/config.py
blob: 58d6484849f6cc26789e627a8cd7bf0b052aa8c5 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
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)