55 lines
1.7 KiB
Python
55 lines
1.7 KiB
Python
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 <entry> - 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 <entry> - displays specified manual entry", None
|