-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path__init__.py
More file actions
134 lines (109 loc) · 3.91 KB
/
Copy path__init__.py
File metadata and controls
134 lines (109 loc) · 3.91 KB
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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
from typing import Literal
from collections import deque
from nonebot import require
from nonebot.rule import to_me
require("nonebot_plugin_saa")
require("nonebot_plugin_alconna")
from nonebot_plugin_saa import Text
from arclet.alconna.base import Option
from arclet.alconna.core import Alconna
from arclet.alconna.args import Arg, Args
from arclet.alconna.arparma import Arparma
from arclet.alconna.typing import CommandMeta
from arclet.alconna.manager import command_manager
from nonebot_plugin_alconna import Match, on_alconna, store_true
from nonebot_plugin_alconna.extension import Extension, add_global_extension
from .config import Config
config = Config.load_config()
_help = Alconna(
"help",
Args["plugin?", str],
Option("--hide", action=store_true, default=False, help_text="是否列出隐藏命令"),
meta=CommandMeta(
description="获取 MagicBot 帮助文档",
usage="@bot /help",
example="@bot /help",
compact=True,
),
)
_statis = Alconna(
"statis",
Arg("type", Literal["show", "most", "least"], "most"),
Args["count", int, config.message_count],
meta=CommandMeta(
description="命令统计",
usage="@bot /statis most",
example="@bot /statis most",
compact=True,
),
)
record = deque(maxlen=256)
class HelperExtension(Extension):
@property
def priority(self) -> int:
return 5
@property
def id(self) -> str:
return "MagicBot:HelperExtension"
async def parse_wrapper(self, bot, state, event, res: Arparma) -> None:
if res.source != _statis.path:
record.append((res.source, res.origin))
add_global_extension(HelperExtension())
help_cmd = on_alconna(_help, auto_send_output=True, rule=to_me())
statis_cmd = on_alconna(_statis, auto_send_output=True, rule=to_me())
statis_cmd.shortcut("消息统计", {"args": ["show"], "prefix": True})
statis_cmd.shortcut("命令统计", {"args": ["most"], "prefix": True})
@help_cmd.handle()
async def _(plugin: Match[str]):
if plugin.available:
plugin_help = command_manager.command_help(plugin.result)
if plugin_help is None:
await Text("唔...没有找到帮助呢").finish(at_sender=True)
else:
await Text(plugin_help).finish(at_sender=True)
await Text(
command_manager.all_command_help(show_index=True, namespace="Alconna")
).send(at_sender=True)
await help_cmd.finish()
@statis_cmd.assign("type", "show")
async def statis_cmd_show(count: int):
if not record:
await statis_cmd.finish("暂无命令记录")
await statis_cmd.finish(
"最近的命令记录为:\n"
+ "\n".join([f"[{i}]: {record[i][1]}" for i in range(min(count, len(record)))])
)
@statis_cmd.assign("type", "most")
async def statis_cmd_most(arp: Arparma):
if not record:
await statis_cmd.finish("暂无命令记录")
length = len(record)
table = {}
for i, r in enumerate(record):
source = r[0]
if source not in table:
table[source] = 0
table[source] += i / length
sort = sorted(table.items(), key=lambda x: x[1], reverse=True)
sort = sort[: arp.query[int]("count")]
await statis_cmd.finish(
"以下按照命令使用频率排序\n"
+ "\n".join(f"[{k}] {v[0]}" for k, v in enumerate(sort))
)
@statis_cmd.assign("type", "least")
async def statis_cmd_least(arp: Arparma):
if not record:
await statis_cmd.finish("暂无命令记录")
length = len(record)
table = {}
for i, r in enumerate(record):
source = r[0]
if r[source] not in table:
table[source] = 0
table[source] += i / length
sort = sorted(table.items(), key=lambda x: x[1], reverse=False)
sort = sort[: arp.query[int]("count")]
await statis_cmd.finish(
"以下按照命令使用频率排序\n"
+ "\n".join(f"[{k}] {v[0]}" for k, v in enumerate(sort))
)