nonebot2/nonebot/rule.py

83 lines
2.2 KiB
Python
Raw Normal View History

2020-06-30 10:13:58 +08:00
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
2020-05-02 20:03:36 +08:00
import re
2020-05-05 16:11:05 +08:00
2020-07-05 20:39:34 +08:00
from nonebot.event import Event
2020-08-07 17:05:08 +08:00
from nonebot.typing import Union, Callable, Optional
2020-05-02 20:03:36 +08:00
class Rule:
2020-07-25 12:28:30 +08:00
def __init__(
self,
checker: Optional[Callable[["BaseBot", Event], # type: ignore
bool]] = None):
self.checker = checker or (lambda bot, event: True)
2020-05-02 20:03:36 +08:00
2020-07-25 12:28:30 +08:00
def __call__(self, bot, event: Event) -> bool:
return self.checker(bot, event)
2020-05-02 20:03:36 +08:00
2020-07-04 22:51:10 +08:00
def __and__(self, other: "Rule") -> "Rule":
2020-07-25 12:28:30 +08:00
return Rule(lambda bot, event: self.checker(bot, event) and other.
checker(bot, event))
2020-05-02 20:03:36 +08:00
2020-07-04 22:51:10 +08:00
def __or__(self, other: "Rule") -> "Rule":
2020-07-25 12:28:30 +08:00
return Rule(lambda bot, event: self.checker(bot, event) or other.
checker(bot, event))
2020-05-02 20:03:36 +08:00
2020-07-04 22:51:10 +08:00
def __neg__(self) -> "Rule":
2020-07-25 12:28:30 +08:00
return Rule(lambda bot, event: not self.checker(bot, event))
def message() -> Rule:
return Rule(lambda bot, event: event.type == "message")
def notice() -> Rule:
return Rule(lambda bot, event: event.type == "notice")
def request() -> Rule:
return Rule(lambda bot, event: event.type == "request")
def metaevent() -> Rule:
return Rule(lambda bot, event: event.type == "meta_event")
2020-05-02 20:03:36 +08:00
def user(*qq: int) -> Rule:
2020-07-25 12:28:30 +08:00
return Rule(lambda bot, event: event.user_id in qq)
2020-05-02 20:03:36 +08:00
def private() -> Rule:
2020-07-25 12:28:30 +08:00
return Rule(lambda bot, event: event.detail_type == "private")
2020-05-02 20:03:36 +08:00
def group(*group: int) -> Rule:
2020-07-25 12:28:30 +08:00
return Rule(lambda bot, event: event.detail_type == "group" and event.
group_id in group)
2020-05-02 20:03:36 +08:00
def discuss(*discuss: int) -> Rule:
2020-07-25 12:28:30 +08:00
return Rule(lambda bot, event: event.detail_type == "discuss" and event.
2020-05-02 20:03:36 +08:00
discuss_id in discuss)
def startswith(msg, start: int = None, end: int = None) -> Rule:
2020-07-25 12:28:30 +08:00
return Rule(lambda bot, event: event.message.startswith(msg, start, end))
2020-05-02 20:03:36 +08:00
def endswith(msg, start: int = None, end: int = None) -> Rule:
2020-07-25 12:28:30 +08:00
return Rule(
lambda bot, event: event.message.endswith(msg, start=None, end=None))
2020-05-02 20:03:36 +08:00
def has(msg: str) -> Rule:
2020-07-25 12:28:30 +08:00
return Rule(lambda bot, event: msg in event.message)
2020-05-02 20:03:36 +08:00
def regex(regex, flags: Union[int, re.RegexFlag] = 0) -> Rule:
pattern = re.compile(regex, flags)
2020-07-25 12:28:30 +08:00
return Rule(lambda bot, event: bool(pattern.search(str(event.message))))