1
0
forked from bot/app
LiteyukiBot/liteyuki/message/rule.py

34 lines
956 B
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:55
@Author : snowykami
@Email : snowykami@outlook.com
@File : rule.py
@Software: PyCharm
"""
from typing import Optional, TypeAlias, Callable, Coroutine
2024-08-20 20:30:50 +08:00
from liteyuki.message.event import MessageEvent
2024-08-20 20:30:50 +08:00
RuleHandler: TypeAlias = Callable[[MessageEvent], 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))
2024-08-20 20:30:50 +08:00
async def __call__(self, event: MessageEvent) -> bool:
if self.handler is None:
return True
return await self.handler(event)