From e830bae282247b0304e68a2943dfea984f36346a Mon Sep 17 00:00:00 2001 From: dymik739 Date: Sat, 4 Nov 2023 13:48:40 +0200 Subject: [PATCH] add new module: help (allows to get manual pages for commands) --- modules/help/db/hardsub.txt | 5 +++ modules/help/db/hardunsub.txt | 5 +++ modules/help/db/schedule-ctl.txt | 5 +++ modules/help/db/sub.txt | 7 +++++ modules/help/db/unsub.txt | 5 +++ modules/help/index.py | 54 ++++++++++++++++++++++++++++++++ modules/help/meta.json | 6 ++++ 7 files changed, 87 insertions(+) create mode 100644 modules/help/db/hardsub.txt create mode 100644 modules/help/db/hardunsub.txt create mode 100644 modules/help/db/schedule-ctl.txt create mode 100644 modules/help/db/sub.txt create mode 100644 modules/help/db/unsub.txt create mode 100644 modules/help/index.py create mode 100644 modules/help/meta.json diff --git a/modules/help/db/hardsub.txt b/modules/help/db/hardsub.txt new file mode 100644 index 0000000..e5b16ae --- /dev/null +++ b/modules/help/db/hardsub.txt @@ -0,0 +1,5 @@ +Usage: !hardsub [event_name] + +Permanently subscribes to [event_name]. If blank, uses "default". +To unsubscribe, use !hardunsub +For one-time subscriptions, use !sub diff --git a/modules/help/db/hardunsub.txt b/modules/help/db/hardunsub.txt new file mode 100644 index 0000000..133477c --- /dev/null +++ b/modules/help/db/hardunsub.txt @@ -0,0 +1,5 @@ +Usage: !hardunsub [event_name] + +Remove permanent subscription to [event_name]. If blank, uses "default". +To remove one-time subscription, use !unsub +To add permanent subscription, use !hardsub diff --git a/modules/help/db/schedule-ctl.txt b/modules/help/db/schedule-ctl.txt new file mode 100644 index 0000000..a78dd17 --- /dev/null +++ b/modules/help/db/schedule-ctl.txt @@ -0,0 +1,5 @@ +Usage: + !schedule-ctl list - see all settings + !schedule-ctl get - see specific personal setting + !schedule-ctl set - change to + !schedule-ctl clear - restore to default value diff --git a/modules/help/db/sub.txt b/modules/help/db/sub.txt new file mode 100644 index 0000000..b46d829 --- /dev/null +++ b/modules/help/db/sub.txt @@ -0,0 +1,7 @@ +Usage: !sub [event_name] + +Soft-subscribes to [event_name]. If black, uses "default". +If you want to remove soft substription, use !unsub + +Soft subscription is automatically removed after the event is triggered once. +If you want to subscribe permanently, use !hardsub diff --git a/modules/help/db/unsub.txt b/modules/help/db/unsub.txt new file mode 100644 index 0000000..6975c96 --- /dev/null +++ b/modules/help/db/unsub.txt @@ -0,0 +1,5 @@ +Usage: !unsub [event_name] + +Removes one-time subscription to [event_name]. If blank, uses "default". +Does not affect hard subscriptions. To remove hard subscription, use !hardunsub +To add one-time supscription, use !sub diff --git a/modules/help/index.py b/modules/help/index.py new file mode 100644 index 0000000..4d9674a --- /dev/null +++ b/modules/help/index.py @@ -0,0 +1,54 @@ +import json +import os + +module_path = "" + +def readfile(filename): + if os.path.exists(module_path + filename): + return open(module_path + filename).read() + else: + return False + +def list_entries(search_query): + files = [] + for p, d, f in os.walk(module_path + "db/"): + for i in f: + entry_name = f"{p.split('/', 3)[3]}/{i}" + if search_query in entry_name: + files.append(f"{p.split('/', 3)[3]}/{i}".rsplit(".", 1)[0].replace("/", ".").lstrip(".")) + + files.sort() + return files + +def process(message, path): + if message.text[:5] != "!help": + return None, None + + global module_path + module_path = path + + cmd = message.text[1:].split() + + if len(cmd) == 1: + return "Usage:\n```\n!help show - displays specified manual entry\n!help list [query] - lists out available entries (optionally, filtered by [query])```", "Markdown" + + elif cmd[1] == "list": + if len(cmd) == 2: + return "Available entries:\n" + "\n".join(list_entries("")), None + else: + return f"Found entries for {cmd[2]}:\n" + "\n".join(list_entries(cmd[2])), None + + elif cmd[1] == "show": + if len(cmd) >= 3: + result = "" + for entry_name in cmd[2:]: + data = readfile("db/" + entry_name.replace(".", "/") + ".txt") + + if data: + result += f"Manual page for {entry_name}:\n```\n{data}```\n" + else: + result += f"No manual found for {entry_name}\n\n" + + return result, "Markdown" + else: + return "Usage: !help show - displays specified manual entry", None diff --git a/modules/help/meta.json b/modules/help/meta.json new file mode 100644 index 0000000..38c231e --- /dev/null +++ b/modules/help/meta.json @@ -0,0 +1,6 @@ +{ + "start_on_boot": true, + "alias": "help", + "version": 2, + "index_file": "index.py" +}