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
|
2024-08-20 06:20:41 +08:00
|
|
|
"""
|
2024-08-22 09:35:02 +08:00
|
|
|
import inspect
|
2024-08-20 06:20:41 +08:00
|
|
|
from typing import Optional, TypeAlias, Callable, Coroutine
|
|
|
|
|
2024-08-20 20:30:50 +08:00
|
|
|
from liteyuki.message.event import MessageEvent
|
2024-08-23 23:47:01 +08:00
|
|
|
from liteyuki import get_config
|
|
|
|
|
|
|
|
_superusers: list[str] = get_config("liteyuki.superusers", [])
|
2024-08-20 06:20:41 +08:00
|
|
|
|
2024-08-22 09:35:02 +08:00
|
|
|
RuleHandlerFunc: TypeAlias = Callable[[MessageEvent], Coroutine[None, None, bool]]
|
2024-08-20 06:20:41 +08:00
|
|
|
"""规则函数签名"""
|
|
|
|
|
|
|
|
|
|
|
|
class Rule:
|
2024-08-22 09:35:02 +08:00
|
|
|
def __init__(self, handler: RuleHandlerFunc):
|
2024-08-20 06:20:41 +08:00
|
|
|
self.handler = handler
|
|
|
|
|
|
|
|
def __or__(self, other: "Rule") -> "Rule":
|
2024-08-22 09:35:02 +08:00
|
|
|
async def combined_handler(event: MessageEvent) -> bool:
|
|
|
|
return await self.handler(event) or await other.handler(event)
|
|
|
|
|
|
|
|
return Rule(combined_handler)
|
2024-08-20 06:20:41 +08:00
|
|
|
|
|
|
|
def __and__(self, other: "Rule") -> "Rule":
|
2024-08-22 09:35:02 +08:00
|
|
|
async def combined_handler(event: MessageEvent) -> bool:
|
|
|
|
return await self.handler(event) and await other.handler(event)
|
|
|
|
|
|
|
|
return Rule(combined_handler)
|
2024-08-20 06:20:41 +08:00
|
|
|
|
2024-08-20 20:30:50 +08:00
|
|
|
async def __call__(self, event: MessageEvent) -> bool:
|
2024-08-20 06:20:41 +08:00
|
|
|
if self.handler is None:
|
|
|
|
return True
|
|
|
|
return await self.handler(event)
|
2024-08-22 09:35:02 +08:00
|
|
|
|
|
|
|
|
|
|
|
@Rule
|
|
|
|
async def empty_rule(event: MessageEvent) -> bool:
|
|
|
|
return True
|
2024-08-23 23:47:01 +08:00
|
|
|
|
|
|
|
@Rule
|
|
|
|
async def is_su_rule(event: MessageEvent) -> bool:
|
|
|
|
return str(event.user_id) in _superusers
|