ErisPulse 2.1.15__py3-none-any.whl → 2.2.0__py3-none-any.whl
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.
- ErisPulse/Core/Event/__init__.py +51 -0
- ErisPulse/Core/Event/base.py +100 -0
- ErisPulse/Core/Event/command.py +310 -0
- ErisPulse/Core/Event/exceptions.py +37 -0
- ErisPulse/Core/Event/message.py +84 -0
- ErisPulse/Core/Event/meta.py +77 -0
- ErisPulse/Core/Event/notice.py +91 -0
- ErisPulse/Core/Event/request.py +61 -0
- ErisPulse/Core/__init__.py +7 -2
- ErisPulse/Core/logger.py +2 -2
- ErisPulse/Core/module.py +150 -0
- ErisPulse/Core/{mods.py → module_registry.py} +41 -37
- ErisPulse/__init__.py +16 -9
- ErisPulse/__main__.py +5 -5
- {erispulse-2.1.15.dist-info → erispulse-2.2.0.dist-info}/METADATA +1 -1
- erispulse-2.2.0.dist-info/RECORD +26 -0
- erispulse-2.1.15.dist-info/RECORD +0 -17
- {erispulse-2.1.15.dist-info → erispulse-2.2.0.dist-info}/WHEEL +0 -0
- {erispulse-2.1.15.dist-info → erispulse-2.2.0.dist-info}/entry_points.txt +0 -0
- {erispulse-2.1.15.dist-info → erispulse-2.2.0.dist-info}/licenses/LICENSE +0 -0
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
"""
|
|
2
|
+
ErisPulse 事件处理模块
|
|
3
|
+
|
|
4
|
+
提供统一的事件处理接口,支持命令、消息、通知、请求和元事件处理
|
|
5
|
+
|
|
6
|
+
{!--< tips >!--}
|
|
7
|
+
1. 所有事件处理都基于OneBot12标准事件格式
|
|
8
|
+
2. 通过装饰器方式注册事件处理器
|
|
9
|
+
3. 支持优先级和条件过滤
|
|
10
|
+
{!--< /tips >!--}
|
|
11
|
+
"""
|
|
12
|
+
|
|
13
|
+
from .command import command
|
|
14
|
+
from .message import message
|
|
15
|
+
from .notice import notice
|
|
16
|
+
from .request import request
|
|
17
|
+
from .meta import meta
|
|
18
|
+
from . import exceptions
|
|
19
|
+
from .. import config
|
|
20
|
+
|
|
21
|
+
# 初始化默认配置
|
|
22
|
+
def _setup_default_config():
|
|
23
|
+
"""
|
|
24
|
+
{!--< internal-use >!--}
|
|
25
|
+
设置默认配置
|
|
26
|
+
"""
|
|
27
|
+
default_config = {
|
|
28
|
+
"command": {
|
|
29
|
+
"prefix": "/",
|
|
30
|
+
"case_sensitive": True,
|
|
31
|
+
"allow_space_prefix": False
|
|
32
|
+
},
|
|
33
|
+
"message": {
|
|
34
|
+
"ignore_self": True
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
current_config = config.getConfig("ErisPulse.event")
|
|
39
|
+
if current_config is None:
|
|
40
|
+
config.setConfig("ErisPulse.event", default_config)
|
|
41
|
+
|
|
42
|
+
_setup_default_config()
|
|
43
|
+
|
|
44
|
+
__all__ = [
|
|
45
|
+
"command",
|
|
46
|
+
"message",
|
|
47
|
+
"notice",
|
|
48
|
+
"request",
|
|
49
|
+
"meta",
|
|
50
|
+
"exceptions"
|
|
51
|
+
]
|
|
@@ -0,0 +1,100 @@
|
|
|
1
|
+
"""
|
|
2
|
+
ErisPulse 事件处理基础模块
|
|
3
|
+
|
|
4
|
+
提供事件处理的核心功能,包括事件注册和处理
|
|
5
|
+
|
|
6
|
+
{!--< tips >!--}
|
|
7
|
+
1. 所有事件处理都基于OneBot12标准事件格式
|
|
8
|
+
2. 通过适配器系统进行事件分发和接收
|
|
9
|
+
{!--< /tips >!--}
|
|
10
|
+
"""
|
|
11
|
+
|
|
12
|
+
from .. import adapter, logger
|
|
13
|
+
from typing import Callable, Any, Dict, List, Optional
|
|
14
|
+
import asyncio
|
|
15
|
+
|
|
16
|
+
class BaseEventHandler:
|
|
17
|
+
"""
|
|
18
|
+
基础事件处理器
|
|
19
|
+
|
|
20
|
+
提供事件处理的基本功能,包括处理器注册等
|
|
21
|
+
"""
|
|
22
|
+
|
|
23
|
+
def __init__(self, event_type: str, module_name: str = None):
|
|
24
|
+
"""
|
|
25
|
+
初始化事件处理器
|
|
26
|
+
|
|
27
|
+
:param event_type: 事件类型
|
|
28
|
+
:param module_name: 模块名称
|
|
29
|
+
"""
|
|
30
|
+
self.event_type = event_type
|
|
31
|
+
self.module_name = module_name
|
|
32
|
+
self.handlers: List[Dict] = []
|
|
33
|
+
|
|
34
|
+
def register(self, handler: Callable, priority: int = 0, condition: Callable = None):
|
|
35
|
+
"""
|
|
36
|
+
注册事件处理器
|
|
37
|
+
|
|
38
|
+
:param handler: 事件处理器函数
|
|
39
|
+
:param priority: 处理器优先级,数值越小优先级越高
|
|
40
|
+
:param condition: 处理器条件函数,返回True时才会执行处理器
|
|
41
|
+
"""
|
|
42
|
+
handler_info = {
|
|
43
|
+
"func": handler,
|
|
44
|
+
"priority": priority,
|
|
45
|
+
"condition": condition,
|
|
46
|
+
"module": self.module_name
|
|
47
|
+
}
|
|
48
|
+
self.handlers.append(handler_info)
|
|
49
|
+
# 按优先级排序
|
|
50
|
+
self.handlers.sort(key=lambda x: x["priority"])
|
|
51
|
+
|
|
52
|
+
# 注册到适配器
|
|
53
|
+
if self.event_type:
|
|
54
|
+
adapter.on(self.event_type)(self._process_event)
|
|
55
|
+
|
|
56
|
+
def __call__(self, priority: int = 0, condition: Callable = None):
|
|
57
|
+
"""
|
|
58
|
+
装饰器方式注册事件处理器
|
|
59
|
+
|
|
60
|
+
:param priority: 处理器优先级
|
|
61
|
+
:param condition: 处理器条件函数
|
|
62
|
+
:return: 装饰器函数
|
|
63
|
+
"""
|
|
64
|
+
def decorator(func: Callable):
|
|
65
|
+
self.register(func, priority, condition)
|
|
66
|
+
return func
|
|
67
|
+
return decorator
|
|
68
|
+
|
|
69
|
+
async def _process_event(self, event: Dict[str, Any]):
|
|
70
|
+
"""
|
|
71
|
+
处理事件
|
|
72
|
+
|
|
73
|
+
{!--< internal-use >!--}
|
|
74
|
+
内部使用的方法,用于处理事件
|
|
75
|
+
|
|
76
|
+
:param event: 事件数据
|
|
77
|
+
"""
|
|
78
|
+
# 执行处理器
|
|
79
|
+
for handler_info in self.handlers:
|
|
80
|
+
condition = handler_info.get("condition")
|
|
81
|
+
# 检查条件
|
|
82
|
+
if condition and not condition(event):
|
|
83
|
+
continue
|
|
84
|
+
|
|
85
|
+
handler = handler_info["func"]
|
|
86
|
+
try:
|
|
87
|
+
if asyncio.iscoroutinefunction(handler):
|
|
88
|
+
await handler(event)
|
|
89
|
+
else:
|
|
90
|
+
handler(event)
|
|
91
|
+
except Exception as e:
|
|
92
|
+
logger.error(f"事件处理器执行错误: {e}")
|
|
93
|
+
|
|
94
|
+
def unregister(self, handler: Callable):
|
|
95
|
+
"""
|
|
96
|
+
注销事件处理器
|
|
97
|
+
|
|
98
|
+
:param handler: 要注销的事件处理器
|
|
99
|
+
"""
|
|
100
|
+
self.handlers = [h for h in self.handlers if h["func"] != handler]
|
|
@@ -0,0 +1,310 @@
|
|
|
1
|
+
# ErisPulse/Core/Event/cmd.py
|
|
2
|
+
"""
|
|
3
|
+
ErisPulse 命令处理模块
|
|
4
|
+
|
|
5
|
+
提供基于装饰器的命令注册和处理功能
|
|
6
|
+
|
|
7
|
+
{!--< tips >!--}
|
|
8
|
+
1. 支持命令别名和命令组
|
|
9
|
+
2. 支持命令权限控制
|
|
10
|
+
3. 支持命令帮助系统
|
|
11
|
+
{!--< /tips >!--}
|
|
12
|
+
"""
|
|
13
|
+
|
|
14
|
+
from .base import BaseEventHandler
|
|
15
|
+
from .. import adapter, config, logger
|
|
16
|
+
from typing import Callable, Union, List, Dict, Any, Optional
|
|
17
|
+
import asyncio
|
|
18
|
+
import re
|
|
19
|
+
|
|
20
|
+
class CommandHandler:
|
|
21
|
+
def __init__(self):
|
|
22
|
+
self.commands: Dict[str, Dict] = {}
|
|
23
|
+
self.aliases: Dict[str, str] = {} # 别名映射
|
|
24
|
+
self.groups: Dict[str, List[str]] = {} # 命令组
|
|
25
|
+
self.permissions: Dict[str, Callable] = {} # 权限检查函数
|
|
26
|
+
self.prefix = config.getConfig("ErisPulse.event.command.prefix", "/")
|
|
27
|
+
self.case_sensitive = config.getConfig("ErisPulse.event.command.case_sensitive", True)
|
|
28
|
+
self.allow_space_prefix = config.getConfig("ErisPulse.event.command.allow_space_prefix", False)
|
|
29
|
+
|
|
30
|
+
# 创建消息事件处理器
|
|
31
|
+
self.handler = BaseEventHandler("message", "command")
|
|
32
|
+
# 注册消息处理器
|
|
33
|
+
self.handler.register(self._handle_message)
|
|
34
|
+
|
|
35
|
+
def __call__(self,
|
|
36
|
+
name: Union[str, List[str]] = None,
|
|
37
|
+
aliases: List[str] = None,
|
|
38
|
+
group: str = None,
|
|
39
|
+
priority: int = 0,
|
|
40
|
+
permission: Callable = None,
|
|
41
|
+
help: str = None,
|
|
42
|
+
usage: str = None,
|
|
43
|
+
hidden: bool = False):
|
|
44
|
+
"""
|
|
45
|
+
命令装饰器
|
|
46
|
+
|
|
47
|
+
:param name: 命令名称,可以是字符串或字符串列表
|
|
48
|
+
:param aliases: 命令别名列表
|
|
49
|
+
:param group: 命令组名称
|
|
50
|
+
:param priority: 处理器优先级
|
|
51
|
+
:param permission: 权限检查函数,返回True时允许执行命令
|
|
52
|
+
:param help: 命令帮助信息
|
|
53
|
+
:param usage: 命令使用方法
|
|
54
|
+
:param hidden: 是否在帮助中隐藏命令
|
|
55
|
+
:return: 装饰器函数
|
|
56
|
+
"""
|
|
57
|
+
def decorator(func: Callable):
|
|
58
|
+
cmd_names = []
|
|
59
|
+
if isinstance(name, str):
|
|
60
|
+
cmd_names = [name]
|
|
61
|
+
elif isinstance(name, list):
|
|
62
|
+
cmd_names = name
|
|
63
|
+
else:
|
|
64
|
+
# 使用函数名作为命令名
|
|
65
|
+
cmd_names = [func.__name__]
|
|
66
|
+
|
|
67
|
+
main_name = cmd_names[0]
|
|
68
|
+
|
|
69
|
+
# 添加别名
|
|
70
|
+
alias_list = aliases or []
|
|
71
|
+
if len(cmd_names) > 1:
|
|
72
|
+
alias_list.extend(cmd_names[1:])
|
|
73
|
+
|
|
74
|
+
# 注册命令
|
|
75
|
+
for cmd_name in cmd_names:
|
|
76
|
+
self.commands[cmd_name] = {
|
|
77
|
+
"func": func,
|
|
78
|
+
"help": help,
|
|
79
|
+
"usage": usage,
|
|
80
|
+
"group": group,
|
|
81
|
+
"permission": permission,
|
|
82
|
+
"hidden": hidden,
|
|
83
|
+
"main_name": main_name
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
# 注册别名映射
|
|
87
|
+
if cmd_name != main_name:
|
|
88
|
+
self.aliases[cmd_name] = main_name
|
|
89
|
+
|
|
90
|
+
# 注册权限检查函数
|
|
91
|
+
if permission and cmd_name not in self.permissions:
|
|
92
|
+
self.permissions[cmd_name] = permission
|
|
93
|
+
|
|
94
|
+
# 添加到命令组
|
|
95
|
+
if group:
|
|
96
|
+
if group not in self.groups:
|
|
97
|
+
self.groups[group] = []
|
|
98
|
+
for cmd_name in cmd_names:
|
|
99
|
+
if cmd_name not in self.groups[group]:
|
|
100
|
+
self.groups[group].append(cmd_name)
|
|
101
|
+
|
|
102
|
+
return func
|
|
103
|
+
return decorator
|
|
104
|
+
|
|
105
|
+
async def _handle_message(self, event: Dict[str, Any]):
|
|
106
|
+
"""
|
|
107
|
+
处理消息事件中的命令
|
|
108
|
+
|
|
109
|
+
{!--< internal-use >!--}
|
|
110
|
+
内部使用的方法,用于从消息中解析并执行命令
|
|
111
|
+
|
|
112
|
+
:param event: 消息事件数据
|
|
113
|
+
"""
|
|
114
|
+
# 检查是否为文本消息
|
|
115
|
+
if event.get("type") != "message":
|
|
116
|
+
return
|
|
117
|
+
|
|
118
|
+
message_segments = event.get("message", [])
|
|
119
|
+
text_content = ""
|
|
120
|
+
for segment in message_segments:
|
|
121
|
+
if segment.get("type") == "text":
|
|
122
|
+
text_content = segment.get("data", {}).get("text", "")
|
|
123
|
+
break
|
|
124
|
+
|
|
125
|
+
if not text_content:
|
|
126
|
+
return
|
|
127
|
+
|
|
128
|
+
# 处理大小写敏感性
|
|
129
|
+
check_text = text_content if self.case_sensitive else text_content.lower()
|
|
130
|
+
prefix = self.prefix if self.case_sensitive else self.prefix.lower()
|
|
131
|
+
|
|
132
|
+
# 检查前缀
|
|
133
|
+
if not check_text.startswith(prefix):
|
|
134
|
+
# 检查是否允许空格前缀 (例如: "/ command")
|
|
135
|
+
if self.allow_space_prefix and check_text.startswith(prefix + " "):
|
|
136
|
+
pass
|
|
137
|
+
else:
|
|
138
|
+
return
|
|
139
|
+
|
|
140
|
+
# 解析命令和参数
|
|
141
|
+
command_text = check_text[len(prefix):].strip()
|
|
142
|
+
parts = command_text.split()
|
|
143
|
+
if not parts:
|
|
144
|
+
return
|
|
145
|
+
|
|
146
|
+
cmd_name = parts[0]
|
|
147
|
+
args = parts[1:] if len(parts) > 1 else []
|
|
148
|
+
|
|
149
|
+
# 处理大小写敏感性
|
|
150
|
+
if not self.case_sensitive:
|
|
151
|
+
cmd_name = cmd_name.lower()
|
|
152
|
+
|
|
153
|
+
# 处理别名
|
|
154
|
+
actual_cmd_name = self.aliases.get(cmd_name, cmd_name)
|
|
155
|
+
|
|
156
|
+
# 查找命令处理器
|
|
157
|
+
if actual_cmd_name in self.commands:
|
|
158
|
+
cmd_info = self.commands[actual_cmd_name]
|
|
159
|
+
handler = cmd_info["func"]
|
|
160
|
+
|
|
161
|
+
# 检查权限
|
|
162
|
+
permission_func = cmd_info.get("permission") or self.permissions.get(actual_cmd_name)
|
|
163
|
+
if permission_func:
|
|
164
|
+
try:
|
|
165
|
+
has_permission = permission_func(event) if not asyncio.iscoroutinefunction(permission_func) \
|
|
166
|
+
else await permission_func(event)
|
|
167
|
+
if not has_permission:
|
|
168
|
+
await self._send_permission_denied(event)
|
|
169
|
+
return
|
|
170
|
+
except Exception as e:
|
|
171
|
+
logger.error(f"权限检查错误: {e}")
|
|
172
|
+
await self._send_permission_denied(event)
|
|
173
|
+
return
|
|
174
|
+
|
|
175
|
+
# 添加命令相关信息到事件
|
|
176
|
+
command_info = {
|
|
177
|
+
"name": actual_cmd_name,
|
|
178
|
+
"main_name": cmd_info["main_name"],
|
|
179
|
+
"args": args,
|
|
180
|
+
"raw": command_text,
|
|
181
|
+
"help": cmd_info["help"],
|
|
182
|
+
"usage": cmd_info["usage"],
|
|
183
|
+
"group": cmd_info["group"]
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
event["command"] = command_info
|
|
187
|
+
|
|
188
|
+
try:
|
|
189
|
+
if asyncio.iscoroutinefunction(handler):
|
|
190
|
+
await handler(event)
|
|
191
|
+
else:
|
|
192
|
+
handler(event)
|
|
193
|
+
except Exception as e:
|
|
194
|
+
logger.error(f"命令执行错误: {e}")
|
|
195
|
+
await self._send_command_error(event, str(e))
|
|
196
|
+
|
|
197
|
+
async def _send_permission_denied(self, event: Dict[str, Any]):
|
|
198
|
+
"""
|
|
199
|
+
发送权限拒绝消息
|
|
200
|
+
|
|
201
|
+
{!--< internal-use >!--}
|
|
202
|
+
内部使用的方法
|
|
203
|
+
|
|
204
|
+
:param event: 事件数据
|
|
205
|
+
"""
|
|
206
|
+
try:
|
|
207
|
+
platform = event.get("platform")
|
|
208
|
+
user_id = event.get("user_id")
|
|
209
|
+
group_id = event.get("group_id")
|
|
210
|
+
detail_type = "group" if group_id else "private"
|
|
211
|
+
target_id = group_id or user_id
|
|
212
|
+
|
|
213
|
+
if platform and hasattr(adapter, platform):
|
|
214
|
+
adapter_instance = getattr(adapter, platform)
|
|
215
|
+
await adapter_instance.Send.To(detail_type, target_id).Text("权限不足,无法执行该命令")
|
|
216
|
+
except Exception as e:
|
|
217
|
+
logger.error(f"发送权限拒绝消息失败: {e}")
|
|
218
|
+
|
|
219
|
+
async def _send_command_error(self, event: Dict[str, Any], error: str):
|
|
220
|
+
"""
|
|
221
|
+
发送命令错误消息
|
|
222
|
+
|
|
223
|
+
{!--< internal-use >!--}
|
|
224
|
+
内部使用的方法
|
|
225
|
+
|
|
226
|
+
:param event: 事件数据
|
|
227
|
+
:param error: 错误信息
|
|
228
|
+
"""
|
|
229
|
+
try:
|
|
230
|
+
platform = event.get("platform")
|
|
231
|
+
user_id = event.get("user_id")
|
|
232
|
+
group_id = event.get("group_id")
|
|
233
|
+
detail_type = "group" if group_id else "private"
|
|
234
|
+
target_id = group_id or user_id
|
|
235
|
+
|
|
236
|
+
if platform and hasattr(adapter, platform):
|
|
237
|
+
adapter_instance = getattr(adapter, platform)
|
|
238
|
+
await adapter_instance.Send.To(detail_type, target_id).Text(f"命令执行出错: {error}")
|
|
239
|
+
except Exception as e:
|
|
240
|
+
logger.error(f"发送命令错误消息失败: {e}")
|
|
241
|
+
|
|
242
|
+
def get_command(self, name: str) -> Optional[Dict]:
|
|
243
|
+
"""
|
|
244
|
+
获取命令信息
|
|
245
|
+
|
|
246
|
+
:param name: 命令名称
|
|
247
|
+
:return: 命令信息字典,如果不存在则返回None
|
|
248
|
+
"""
|
|
249
|
+
actual_name = self.aliases.get(name, name)
|
|
250
|
+
return self.commands.get(actual_name)
|
|
251
|
+
|
|
252
|
+
def get_commands(self) -> Dict[str, Dict]:
|
|
253
|
+
"""
|
|
254
|
+
获取所有命令
|
|
255
|
+
|
|
256
|
+
:return: 命令信息字典
|
|
257
|
+
"""
|
|
258
|
+
return self.commands
|
|
259
|
+
|
|
260
|
+
def get_group_commands(self, group: str) -> List[str]:
|
|
261
|
+
"""
|
|
262
|
+
获取命令组中的命令
|
|
263
|
+
|
|
264
|
+
:param group: 命令组名称
|
|
265
|
+
:return: 命令名称列表
|
|
266
|
+
"""
|
|
267
|
+
return self.groups.get(group, [])
|
|
268
|
+
|
|
269
|
+
def get_visible_commands(self) -> Dict[str, Dict]:
|
|
270
|
+
"""
|
|
271
|
+
获取所有可见命令(非隐藏命令)
|
|
272
|
+
|
|
273
|
+
:return: 可见命令信息字典
|
|
274
|
+
"""
|
|
275
|
+
return {name: info for name, info in self.commands.items()
|
|
276
|
+
if not info.get("hidden", False) and name == info["main_name"]}
|
|
277
|
+
|
|
278
|
+
def help(self, command_name: str = None, show_hidden: bool = False) -> str:
|
|
279
|
+
"""
|
|
280
|
+
生成帮助信息
|
|
281
|
+
|
|
282
|
+
:param command_name: 命令名称,如果为None则生成所有命令的帮助
|
|
283
|
+
:param show_hidden: 是否显示隐藏命令
|
|
284
|
+
:return: 帮助信息字符串
|
|
285
|
+
"""
|
|
286
|
+
if command_name:
|
|
287
|
+
cmd_info = self.get_command(command_name)
|
|
288
|
+
if cmd_info:
|
|
289
|
+
help_text = cmd_info.get("help", "无帮助信息")
|
|
290
|
+
usage = cmd_info.get("usage", f"{self.prefix}{command_name}")
|
|
291
|
+
return f"命令: {command_name}\n用法: {usage}\n说明: {help_text}"
|
|
292
|
+
else:
|
|
293
|
+
return f"未找到命令: {command_name}"
|
|
294
|
+
else:
|
|
295
|
+
# 生成所有命令的帮助
|
|
296
|
+
commands_to_show = self.get_visible_commands() if not show_hidden else {
|
|
297
|
+
name: info for name, info in self.commands.items()
|
|
298
|
+
if name == info["main_name"]
|
|
299
|
+
}
|
|
300
|
+
|
|
301
|
+
if not commands_to_show:
|
|
302
|
+
return "暂无可用命令"
|
|
303
|
+
|
|
304
|
+
help_lines = ["可用命令:"]
|
|
305
|
+
for cmd_name, cmd_info in commands_to_show.items():
|
|
306
|
+
help_text = cmd_info.get("help", "无说明")
|
|
307
|
+
help_lines.append(f" {self.prefix}{cmd_name} - {help_text}")
|
|
308
|
+
return "\n".join(help_lines)
|
|
309
|
+
|
|
310
|
+
command = CommandHandler()
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
"""
|
|
2
|
+
ErisPulse 事件系统异常处理模块
|
|
3
|
+
|
|
4
|
+
提供事件系统中可能发生的各种异常类型定义
|
|
5
|
+
"""
|
|
6
|
+
|
|
7
|
+
class EventException(Exception):
|
|
8
|
+
"""
|
|
9
|
+
事件系统基础异常
|
|
10
|
+
|
|
11
|
+
所有事件系统相关异常的基类
|
|
12
|
+
"""
|
|
13
|
+
pass
|
|
14
|
+
|
|
15
|
+
class CommandException(EventException):
|
|
16
|
+
"""
|
|
17
|
+
命令处理异常
|
|
18
|
+
|
|
19
|
+
当命令处理过程中发生错误时抛出
|
|
20
|
+
"""
|
|
21
|
+
pass
|
|
22
|
+
|
|
23
|
+
class EventHandlerException(EventException):
|
|
24
|
+
"""
|
|
25
|
+
事件处理器异常
|
|
26
|
+
|
|
27
|
+
当事件处理器执行过程中发生错误时抛出
|
|
28
|
+
"""
|
|
29
|
+
pass
|
|
30
|
+
|
|
31
|
+
class EventNotFoundException(EventException):
|
|
32
|
+
"""
|
|
33
|
+
事件未找到异常
|
|
34
|
+
|
|
35
|
+
当尝试获取不存在的事件处理器时抛出
|
|
36
|
+
"""
|
|
37
|
+
pass
|
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
"""
|
|
2
|
+
ErisPulse 消息处理模块
|
|
3
|
+
|
|
4
|
+
提供基于装饰器的消息事件处理功能
|
|
5
|
+
|
|
6
|
+
{!--< tips >!--}
|
|
7
|
+
1. 支持私聊、群聊消息分类处理
|
|
8
|
+
2. 支持@消息特殊处理
|
|
9
|
+
3. 支持自定义条件过滤
|
|
10
|
+
{!--< /tips >!--}
|
|
11
|
+
"""
|
|
12
|
+
|
|
13
|
+
from .base import BaseEventHandler
|
|
14
|
+
from typing import Callable, Dict, Any
|
|
15
|
+
|
|
16
|
+
class MessageHandler:
|
|
17
|
+
def __init__(self):
|
|
18
|
+
self.handler = BaseEventHandler("message", "message")
|
|
19
|
+
|
|
20
|
+
def on_message(self, priority: int = 0):
|
|
21
|
+
"""
|
|
22
|
+
消息事件装饰器
|
|
23
|
+
|
|
24
|
+
:param priority: 处理器优先级
|
|
25
|
+
:return: 装饰器函数
|
|
26
|
+
"""
|
|
27
|
+
def decorator(func: Callable):
|
|
28
|
+
self.handler.register(func, priority)
|
|
29
|
+
return func
|
|
30
|
+
return decorator
|
|
31
|
+
|
|
32
|
+
def on_private_message(self, priority: int = 0):
|
|
33
|
+
"""
|
|
34
|
+
私聊消息事件装饰器
|
|
35
|
+
|
|
36
|
+
:param priority: 处理器优先级
|
|
37
|
+
:return: 装饰器函数
|
|
38
|
+
"""
|
|
39
|
+
def condition(event: Dict[str, Any]) -> bool:
|
|
40
|
+
return event.get("detail_type") == "private"
|
|
41
|
+
|
|
42
|
+
def decorator(func: Callable):
|
|
43
|
+
self.handler.register(func, priority, condition)
|
|
44
|
+
return func
|
|
45
|
+
return decorator
|
|
46
|
+
|
|
47
|
+
def on_group_message(self, priority: int = 0):
|
|
48
|
+
"""
|
|
49
|
+
群聊消息事件装饰器
|
|
50
|
+
|
|
51
|
+
:param priority: 处理器优先级
|
|
52
|
+
:return: 装饰器函数
|
|
53
|
+
"""
|
|
54
|
+
def condition(event: Dict[str, Any]) -> bool:
|
|
55
|
+
return event.get("detail_type") == "group"
|
|
56
|
+
|
|
57
|
+
def decorator(func: Callable):
|
|
58
|
+
self.handler.register(func, priority, condition)
|
|
59
|
+
return func
|
|
60
|
+
return decorator
|
|
61
|
+
|
|
62
|
+
def on_at_message(self, priority: int = 0):
|
|
63
|
+
"""
|
|
64
|
+
@消息事件装饰器
|
|
65
|
+
|
|
66
|
+
:param priority: 处理器优先级
|
|
67
|
+
:return: 装饰器函数
|
|
68
|
+
"""
|
|
69
|
+
def condition(event: Dict[str, Any]) -> bool:
|
|
70
|
+
# 检查消息中是否有@机器人
|
|
71
|
+
message_segments = event.get("message", [])
|
|
72
|
+
self_id = event.get("self", {}).get("user_id")
|
|
73
|
+
|
|
74
|
+
for segment in message_segments:
|
|
75
|
+
if segment.get("type") == "mention" and segment.get("data", {}).get("user_id") == self_id:
|
|
76
|
+
return True
|
|
77
|
+
return False
|
|
78
|
+
|
|
79
|
+
def decorator(func: Callable):
|
|
80
|
+
self.handler.register(func, priority, condition)
|
|
81
|
+
return func
|
|
82
|
+
return decorator
|
|
83
|
+
|
|
84
|
+
message = MessageHandler()
|
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
# ErisPulse/Core/Event/meta.py
|
|
2
|
+
"""
|
|
3
|
+
ErisPulse 元事件处理模块
|
|
4
|
+
|
|
5
|
+
提供基于装饰器的元事件处理功能
|
|
6
|
+
|
|
7
|
+
{!--< tips >!--}
|
|
8
|
+
1. 支持连接、断开连接等生命周期事件
|
|
9
|
+
2. 适用于系统状态监控和初始化操作
|
|
10
|
+
{!--< /tips >!--}
|
|
11
|
+
"""
|
|
12
|
+
|
|
13
|
+
from .base import BaseEventHandler
|
|
14
|
+
from typing import Callable, Dict, Any
|
|
15
|
+
|
|
16
|
+
class MetaHandler:
|
|
17
|
+
def __init__(self):
|
|
18
|
+
self.handler = BaseEventHandler("meta", "meta")
|
|
19
|
+
|
|
20
|
+
def on_meta(self, priority: int = 0):
|
|
21
|
+
"""
|
|
22
|
+
通用元事件装饰器
|
|
23
|
+
|
|
24
|
+
:param priority: 处理器优先级
|
|
25
|
+
:return: 装饰器函数
|
|
26
|
+
"""
|
|
27
|
+
def decorator(func: Callable):
|
|
28
|
+
self.handler.register(func, priority)
|
|
29
|
+
return func
|
|
30
|
+
return decorator
|
|
31
|
+
|
|
32
|
+
def on_connect(self, priority: int = 0):
|
|
33
|
+
"""
|
|
34
|
+
连接事件装饰器
|
|
35
|
+
|
|
36
|
+
:param priority: 处理器优先级
|
|
37
|
+
:return: 装饰器函数
|
|
38
|
+
"""
|
|
39
|
+
def condition(event: Dict[str, Any]) -> bool:
|
|
40
|
+
return event.get("detail_type") == "connect"
|
|
41
|
+
|
|
42
|
+
def decorator(func: Callable):
|
|
43
|
+
self.handler.register(func, priority, condition)
|
|
44
|
+
return func
|
|
45
|
+
return decorator
|
|
46
|
+
|
|
47
|
+
def on_disconnect(self, priority: int = 0):
|
|
48
|
+
"""
|
|
49
|
+
断开连接事件装饰器
|
|
50
|
+
|
|
51
|
+
:param priority: 处理器优先级
|
|
52
|
+
:return: 装饰器函数
|
|
53
|
+
"""
|
|
54
|
+
def condition(event: Dict[str, Any]) -> bool:
|
|
55
|
+
return event.get("detail_type") == "disconnect"
|
|
56
|
+
|
|
57
|
+
def decorator(func: Callable):
|
|
58
|
+
self.handler.register(func, priority, condition)
|
|
59
|
+
return func
|
|
60
|
+
return decorator
|
|
61
|
+
|
|
62
|
+
def on_heartbeat(self, priority: int = 0):
|
|
63
|
+
"""
|
|
64
|
+
心跳事件装饰器
|
|
65
|
+
|
|
66
|
+
:param priority: 处理器优先级
|
|
67
|
+
:return: 装饰器函数
|
|
68
|
+
"""
|
|
69
|
+
def condition(event: Dict[str, Any]) -> bool:
|
|
70
|
+
return event.get("detail_type") == "heartbeat"
|
|
71
|
+
|
|
72
|
+
def decorator(func: Callable):
|
|
73
|
+
self.handler.register(func, priority, condition)
|
|
74
|
+
return func
|
|
75
|
+
return decorator
|
|
76
|
+
|
|
77
|
+
meta = MetaHandler()
|