summaryrefslogtreecommitdiff
path: root/ATRI/plugins/help/data_source.py
blob: 7b2f6f8b0ee2a26bd3c9c2f457c11eb541f7cb2f (plain)
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
99
100
101
102
103
104
105
106
107
108
import os

from ATRI import __version__
from ATRI.service import Service, SERVICES_DIR, ServiceTools
from ATRI.config import BotSelfConfig
from ATRI.exceptions import ReadFileError


SERVICE_INFO_FORMAT = """
服务名:{service}
说明:{docs}
可用命令:\n    {cmd_list}
是否全局启用:{enabled}
Tip: 帮助 [服务] [命令] 以查看对应命令详细信息
""".strip()


COMMAND_INFO_FORMAT = """
命令:{cmd}
类型:{cmd_type}
说明:{docs}
更多触发方式:{aliases}
""".strip()


class Helper(Service):
    def __init__(self):
        Service.__init__(self, "帮助", "bot的食用指南~")

    @staticmethod
    def menu() -> str:
        return (
            "哦呀?~需要帮助?\n"
            "关于 -查看bot基本信息\n"
            "服务列表 -以查看所有可用服务\n"
            "帮助 [服务] -以查看对应服务帮助\n"
            "Tip: 均需要at触发。菜单 以打开此页面"
        )

    @staticmethod
    def about() -> str:
        temp_list = list()
        for i in BotSelfConfig.nickname:
            temp_list.append(i)
        nickname = "、".join(map(str, temp_list))
        return (
            "唔...是来认识咱的么\n"
            f"可以称呼咱:{nickname}\n"
            f"咱的型号是:{__version__}\n"
            "想进一步了解:\n"
            "https://github.com/Kyomotoi/ATRI"
        )

    @staticmethod
    def service_list() -> str:
        files = os.listdir(SERVICES_DIR)
        temp_list = list()
        for i in files:
            service = i.replace(".json", "")
            temp_list.append(service)

        msg0 = "咱搭载了以下服务~\n"
        services = "、".join(map(str, temp_list))
        msg0 = msg0 + services
        repo = msg0 + "\n@ 帮助 [服务] -以查看对应服务帮助"
        return repo

    @staticmethod
    def service_info(service: str) -> str:
        try:
            data = ServiceTools().load_service(service)
        except ReadFileError:
            return "请检查是否输入错误呢..."

        service_name = data.get("service", "error")
        service_docs = data.get("docs", "error")
        service_enabled = data.get("enabled", True)

        _service_cmd_list = list(data.get("cmd_list", {"error"}))
        service_cmd_list = "、".join(map(str, _service_cmd_list))

        repo = SERVICE_INFO_FORMAT.format(
            service=service_name,
            docs=service_docs,
            cmd_list=service_cmd_list,
            enabled=service_enabled,
        )
        return repo

    @staticmethod
    def cmd_info(service: str, cmd: str) -> str:
        try:
            data = ServiceTools().load_service(service)
        except ReadFileError:
            return "请检查是否输入错误..."

        cmd_list: dict = data["cmd_list"]
        cmd_info = cmd_list.get(cmd, dict())
        if not cmd_info:
            return "请检查命令是否输入错误..."
        cmd_type = cmd_info.get("type", "ignore")
        docs = cmd_info.get("docs", "ignore")
        aliases = cmd_info.get("aliases", "ignore")

        repo = COMMAND_INFO_FORMAT.format(
            cmd=cmd, cmd_type=cmd_type, docs=docs, aliases=aliases
        )
        return repo