app/liteyuki/message/matcher.py

64 lines
1.6 KiB
Python
Raw Normal View History

2024-08-19 23:47:39 +08:00
# -*- coding: utf-8 -*-
"""
Copyright (C) 2020-2024 LiteyukiStudio. All Rights Reserved
@Time : 2024/8/19 下午10:51
@Author : snowykami
@Email : snowykami@outlook.com
@File : matcher.py
@Software: PyCharm
"""
import traceback
from typing import Any, TypeAlias, Callable, Coroutine
2024-08-20 20:30:50 +08:00
from liteyuki.message.event import MessageEvent
from liteyuki.message.rule import Rule
2024-08-20 20:30:50 +08:00
EventHandler: TypeAlias = Callable[[MessageEvent], Coroutine[None, None, Any]]
2024-08-19 23:47:39 +08:00
class Matcher:
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] = []
def __str__(self):
return f"Matcher(rule={self.rule}, priority={self.priority}, block={self.block})"
2024-08-22 09:35:02 +08:00
def handle(self) -> Callable[[EventHandler], EventHandler]:
"""
添加处理函数装饰器
Returns:
2024-08-22 09:35:02 +08:00
装饰器 handler
"""
2024-08-22 09:35:02 +08:00
def decorator(handler: EventHandler) -> EventHandler:
self.handlers.append(handler)
return handler
return decorator
2024-08-20 20:38:10 +08:00
async def run(self, event: MessageEvent) -> None:
"""
运行处理函数
Args:
event:
Returns:
"""
if not await self.rule(event):
return
for handler in self.handlers:
try:
await handler(event)
except Exception:
traceback.print_exc()