ErisPulse-HelpModule 2.1.0__tar.gz → 2.1.1__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,26 +1,29 @@
1
+ from typing import Dict, List, Optional
2
+
1
3
  from ErisPulse import sdk
2
- from ErisPulse.Core.Event import command
3
4
  from ErisPulse.Core import config
4
5
  from ErisPulse.Core.Bases import BaseModule
5
- from typing import Dict, List, Optional
6
+ from ErisPulse.Core.Event import command
7
+
6
8
  from .templates import HelpTemplates
7
9
 
10
+
8
11
  class HelpModule(BaseModule):
9
12
  def __init__(self):
10
13
  self.sdk = sdk
11
14
  self.logger = sdk.logger.get_child("HelpModule")
12
15
  self.command_list = []
13
16
  self.command_map = {}
14
-
17
+
15
18
  @staticmethod
16
19
  def should_eager_load():
17
20
  return True
18
-
21
+
19
22
  async def on_load(self, event):
20
23
  self._register_commands()
21
24
  self.logger.info("HelpModule 已加载")
22
25
  return True
23
-
26
+
24
27
  async def on_unload(self, event):
25
28
  self._unregister_commands()
26
29
  self.logger.info("HelpModule 已卸载")
@@ -29,62 +32,54 @@ class HelpModule(BaseModule):
29
32
  def _get_config(self):
30
33
  module_config = config.getConfig("HelpModule")
31
34
  if not module_config:
32
- default_config = {
33
- "show_hidden_commands": False,
34
- "group_commands": True
35
- }
35
+ default_config = {"show_hidden_commands": False, "group_commands": True}
36
36
  config.setConfig("HelpModule", default_config)
37
37
  self.logger.warning("未找到HelpModule配置,已创建默认配置")
38
38
  return default_config
39
39
  return module_config
40
-
40
+
41
41
  def _get_command_prefix(self) -> str:
42
42
  event_config = config.getConfig("ErisPulse.event", {})
43
43
  command_config = event_config.get("command", {})
44
44
  return command_config.get("prefix", "/")
45
-
45
+
46
46
  def _register_commands(self):
47
47
  self.help_command_func = self._create_help_command()
48
48
  command(
49
- "help",
50
- aliases=["h", "帮助"],
49
+ "help",
50
+ aliases=["h", "帮助"],
51
51
  help="显示帮助信息",
52
- usage="help [序号] - 显示命令列表或查看指定序号的命令详情"
52
+ usage="help [序号] - 显示命令列表或查看指定序号的命令详情",
53
53
  )(self.help_command_func)
54
54
 
55
55
  def _unregister_commands(self):
56
- if hasattr(self, 'help_command_func'):
56
+ if hasattr(self, "help_command_func"):
57
57
  command.unregister(self.help_command_func)
58
-
58
+
59
59
  def _create_help_command(self):
60
60
  async def help_command(event):
61
61
  await self._handle_help_command(event)
62
+
62
63
  return help_command
63
64
 
64
65
  def _build_command_list(self) -> List[Dict]:
65
66
  self.command_list = []
66
67
  module_config = self._get_config()
67
68
  show_hidden = module_config.get("show_hidden_commands", False)
68
-
69
+
69
70
  if show_hidden:
70
71
  all_commands = command.get_commands()
71
72
  for cmd_name in all_commands:
72
73
  cmd_info = command.get_command(cmd_name)
73
74
  if cmd_info and cmd_name == cmd_info.get("main_name"):
74
- self.command_list.append({
75
- "name": cmd_name,
76
- "info": cmd_info
77
- })
75
+ self.command_list.append({"name": cmd_name, "info": cmd_info})
78
76
  else:
79
77
  visible_commands = command.get_visible_commands()
80
78
  for cmd_name in visible_commands:
81
79
  cmd_info = command.get_command(cmd_name)
82
80
  if cmd_info and cmd_name == cmd_info.get("main_name"):
83
- self.command_list.append({
84
- "name": cmd_name,
85
- "info": cmd_info
86
- })
87
-
81
+ self.command_list.append({"name": cmd_name, "info": cmd_info})
82
+
88
83
  return self.command_list
89
84
 
90
85
  def _group_commands_by_category(self, commands: List[Dict]) -> Dict[str, List]:
@@ -96,25 +91,14 @@ class HelpModule(BaseModule):
96
91
  grouped[group].append(cmd)
97
92
  return grouped
98
93
 
99
- async def _handle_help_command(self, event: Dict) -> None:
94
+ async def _handle_help_command(self, event) -> None:
100
95
  try:
101
- platform = event["platform"]
102
- if event.get("detail_type") == "group":
103
- target_type = "group"
104
- target_id = event["group_id"]
105
- elif event.get("detail_type") == "private" or event.get("detail_type") == "user":
106
- target_type = "user"
107
- target_id = event["user_id"]
108
- else:
109
- target_type = event.get("detail_type")
110
- target_id = event.get("target_id")
111
-
112
- args = event.get("command", {}).get("args", [])
113
- adapter = getattr(sdk.adapter, platform)
114
-
96
+ platform = event.get_platform()
97
+ args = event.get_command_args()
98
+
115
99
  commands = self._build_command_list()
116
100
  module_config = self._get_config()
117
-
101
+
118
102
  if args:
119
103
  # 显示命令详情
120
104
  try:
@@ -122,49 +106,44 @@ class HelpModule(BaseModule):
122
106
  if index in self.command_map:
123
107
  # 使用模板构建命令详情
124
108
  templates = HelpTemplates.build_command_detail(
125
- self.command_map[index],
126
- self._get_command_prefix()
109
+ self.command_map[index], self._get_command_prefix()
127
110
  )
128
- help_content = self._select_best_format(platform, templates)
129
111
  else:
130
112
  # 使用错误模板
131
113
  templates = HelpTemplates.build_error(
132
- "序号超出范围",
133
- f"请输入 1-{len(commands)} 之间的序号"
114
+ "序号超出范围", f"请输入 1-{len(commands)} 之间的序号"
134
115
  )
135
- help_content = self._select_best_format(platform, templates)
136
116
  except ValueError:
137
117
  templates = HelpTemplates.build_error(
138
- "参数错误",
139
- "请输入有效的序号"
118
+ "参数错误", "请输入有效的序号"
140
119
  )
141
- help_content = self._select_best_format(platform, templates)
142
120
  else:
143
121
  # 显示命令列表
144
122
  templates = HelpTemplates.build_help_list(
145
123
  commands,
146
124
  self.command_map,
147
125
  self._get_command_prefix(),
148
- module_config.get("group_commands", True)
126
+ module_config.get("group_commands", True),
149
127
  )
150
- help_content = self._select_best_format(platform, templates)
151
-
152
- # 发送消息
153
- await self._send_with_format(adapter, target_type, target_id, help_content)
128
+
129
+ # 根据平台能力选择最佳格式,通过 event.reply 发送
130
+ # event.reply 内部自动解析会话类型(含 channel/guild/thread 等)
131
+ format_name, content = self._select_best_format(platform, templates)
132
+ await event.reply(content, method=format_name)
154
133
  except Exception as e:
155
134
  self.logger.error(f"处理帮助命令时出错: {e}", exc_info=True)
156
-
135
+
157
136
  def _select_best_format(self, platform: str, templates: Dict[str, str]) -> tuple:
158
137
  """
159
138
  根据平台支持的发送方法选择最佳格式
160
139
  优先使用 list_sends,不支持时使用 hasattr 兜底
161
-
140
+
162
141
  返回: (format_name, content)
163
142
  """
164
143
  # 首先尝试使用 list_sends(推荐方式)
165
144
  try:
166
145
  supported_methods = sdk.adapter.list_sends(platform)
167
-
146
+
168
147
  # 优先级: Html > Markdown > Text
169
148
  if "Html" in supported_methods:
170
149
  return ("Html", templates["html"])
@@ -174,15 +153,15 @@ class HelpModule(BaseModule):
174
153
  return ("Text", templates["text"])
175
154
  except Exception as e:
176
155
  self.logger.warning(f"list_sends 检测失败: {e},尝试使用 hasattr 兜底")
177
-
156
+
178
157
  # 使用 hasattr 作为兜底方案
179
158
  adapter = getattr(sdk.adapter, platform)
180
159
  send_obj = adapter.Send if hasattr(adapter, "Send") else None
181
-
160
+
182
161
  if send_obj is None:
183
162
  self.logger.warning(f"平台 {platform} 不支持 Send 接口,使用纯文本格式")
184
163
  return ("Text", templates["text"])
185
-
164
+
186
165
  # 检查支持的方法
187
166
  if hasattr(send_obj, "Html"):
188
167
  return ("Html", templates["html"])
@@ -190,15 +169,3 @@ class HelpModule(BaseModule):
190
169
  return ("Markdown", templates["markdown"])
191
170
  else:
192
171
  return ("Text", templates["text"])
193
-
194
- async def _send_with_format(self, adapter, target_type: str, target_id: str,
195
- format_content: tuple) -> None:
196
- format_name, content = format_content
197
-
198
- # 根据格式调用对应的发送方法
199
- if format_name == "Html":
200
- await adapter.Send.To(target_type, target_id).Html(content)
201
- elif format_name == "Markdown":
202
- await adapter.Send.To(target_type, target_id).Markdown(content)
203
- else: # Text
204
- await adapter.Send.To(target_type, target_id).Text(content)
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: ErisPulse-HelpModule
3
- Version: 2.1.0
3
+ Version: 2.1.1
4
4
  Summary: ErisPulse 帮助命令模块,提供自动化的命令帮助系统,支持查看所有可用命令及其用法说明
5
5
  Author-email: wsu2059q <wsu2059@qq.com>
6
6
  License-Expression: MIT
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: ErisPulse-HelpModule
3
- Version: 2.1.0
3
+ Version: 2.1.1
4
4
  Summary: ErisPulse 帮助命令模块,提供自动化的命令帮助系统,支持查看所有可用命令及其用法说明
5
5
  Author-email: wsu2059q <wsu2059@qq.com>
6
6
  License-Expression: MIT
@@ -1,6 +1,6 @@
1
1
  [project]
2
2
  name = "ErisPulse-HelpModule"
3
- version = "2.1.0"
3
+ version = "2.1.1"
4
4
  description = "ErisPulse 帮助命令模块,提供自动化的命令帮助系统,支持查看所有可用命令及其用法说明"
5
5
  readme = "README.md"
6
6
  requires-python = ">=3.10"