✨ 添加对主流框架的消息io支持
This commit is contained in:
@ -7,4 +7,27 @@ Copyright (C) 2020-2024 LiteyukiStudio. All Rights Reserved
|
||||
@Email : snowykami@outlook.com
|
||||
@File : rule.py
|
||||
@Software: PyCharm
|
||||
"""
|
||||
"""
|
||||
|
||||
from typing import Optional, TypeAlias, Callable, Coroutine
|
||||
|
||||
from liteyuki.message.event import Event
|
||||
|
||||
RuleHandler: TypeAlias = Callable[[Event], Coroutine[None, None, bool]]
|
||||
"""规则函数签名"""
|
||||
|
||||
|
||||
class Rule:
|
||||
def __init__(self, handler: Optional[RuleHandler] = None):
|
||||
self.handler = handler
|
||||
|
||||
def __or__(self, other: "Rule") -> "Rule":
|
||||
return Rule(lambda event: self.handler(event) or other.handler(event))
|
||||
|
||||
def __and__(self, other: "Rule") -> "Rule":
|
||||
return Rule(lambda event: self.handler(event) and other.handler(event))
|
||||
|
||||
async def __call__(self, event: Event) -> bool:
|
||||
if self.handler is None:
|
||||
return True
|
||||
return await self.handler(event)
|
||||
|
Reference in New Issue
Block a user