2024-08-21 17:59:21 +08:00
|
|
|
---
|
|
|
|
title: liteyuki.message.matcher
|
|
|
|
---
|
2024-08-29 14:19:39 +08:00
|
|
|
### **class** `Matcher`
|
|
|
|
### *method* `__init__(self, rule: Rule, priority: int, block: bool)`
|
2024-08-21 17:59:21 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
2024-08-29 14:19:39 +08:00
|
|
|
**Description**: 匹配器
|
2024-08-21 17:59:21 +08:00
|
|
|
|
2024-08-29 14:19:39 +08:00
|
|
|
**Arguments**:
|
|
|
|
> - rule: 规则
|
|
|
|
> - priority: 优先级 >= 0
|
|
|
|
> - block: 是否阻断后续优先级更低的匹配器
|
2024-08-21 17:59:21 +08:00
|
|
|
|
|
|
|
|
|
|
|
<details>
|
2024-08-29 14:19:39 +08:00
|
|
|
<summary> <b>Source code</b> </summary>
|
2024-08-21 17:59:21 +08:00
|
|
|
|
|
|
|
```python
|
|
|
|
def __init__(self, rule: Rule, priority: int, block: bool):
|
|
|
|
"""
|
|
|
|
匹配器
|
|
|
|
Args:
|
|
|
|
rule: 规则
|
|
|
|
priority: 优先级 >= 0
|
|
|
|
block: 是否阻断后续优先级更低的匹配器
|
|
|
|
"""
|
|
|
|
self.rule = rule
|
|
|
|
self.priority = priority
|
|
|
|
self.block = block
|
|
|
|
self.handlers: list[EventHandler] = []
|
|
|
|
```
|
|
|
|
</details>
|
|
|
|
|
2024-08-29 14:19:39 +08:00
|
|
|
### *method* `handle(self) -> Callable[[EventHandler], EventHandler]`
|
2024-08-21 17:59:21 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
2024-08-29 14:19:39 +08:00
|
|
|
**Description**: 添加处理函数,装饰器
|
2024-08-21 17:59:21 +08:00
|
|
|
|
2024-08-29 14:19:39 +08:00
|
|
|
**Return**: 装饰器 handler
|
2024-08-21 17:59:21 +08:00
|
|
|
|
|
|
|
|
|
|
|
<details>
|
2024-08-29 14:19:39 +08:00
|
|
|
<summary> <b>Source code</b> </summary>
|
2024-08-21 17:59:21 +08:00
|
|
|
|
|
|
|
```python
|
2024-08-29 14:19:39 +08:00
|
|
|
def handle(self) -> Callable[[EventHandler], EventHandler]:
|
2024-08-21 17:59:21 +08:00
|
|
|
"""
|
|
|
|
添加处理函数,装饰器
|
2024-08-29 14:19:39 +08:00
|
|
|
Returns:
|
|
|
|
装饰器 handler
|
|
|
|
"""
|
|
|
|
|
|
|
|
def decorator(handler: EventHandler) -> EventHandler:
|
|
|
|
self.handlers.append(handler)
|
|
|
|
return handler
|
|
|
|
return decorator
|
|
|
|
```
|
|
|
|
</details>
|
|
|
|
|
|
|
|
### *async method* `run(self, event: MessageEvent) -> None`
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
**Description**: 运行处理函数
|
|
|
|
|
|
|
|
**Arguments**:
|
|
|
|
> - event:
|
|
|
|
|
|
|
|
|
|
|
|
<details>
|
|
|
|
<summary> <b>Source code</b> </summary>
|
|
|
|
|
|
|
|
```python
|
|
|
|
async def run(self, event: MessageEvent) -> None:
|
|
|
|
"""
|
|
|
|
运行处理函数
|
2024-08-21 17:59:21 +08:00
|
|
|
Args:
|
2024-08-29 14:19:39 +08:00
|
|
|
event:
|
2024-08-21 17:59:21 +08:00
|
|
|
Returns:
|
|
|
|
"""
|
2024-08-29 14:19:39 +08:00
|
|
|
if not await self.rule(event):
|
|
|
|
return
|
|
|
|
for handler in self.handlers:
|
|
|
|
try:
|
|
|
|
await handler(event)
|
|
|
|
except Exception:
|
|
|
|
traceback.print_exc()
|
2024-08-21 17:59:21 +08:00
|
|
|
```
|
|
|
|
</details>
|
|
|
|
|
2024-08-29 14:19:39 +08:00
|
|
|
### ***var*** `EventHandler = Callable[[MessageEvent], Coroutine[None, None, Any]]`
|
|
|
|
|
|
|
|
- **Type**: `TypeAlias`
|
|
|
|
|