2020-10-06 02:08:48 +08:00
|
|
|
|
"""
|
|
|
|
|
事件响应器
|
|
|
|
|
==========
|
|
|
|
|
|
2021-03-18 19:16:18 +08:00
|
|
|
|
该模块实现事件响应器的创建与运行,并提供一些快捷方法来帮助用户更好的与机器人进行对话 。
|
2020-10-06 02:08:48 +08:00
|
|
|
|
"""
|
2020-06-30 10:13:58 +08:00
|
|
|
|
|
2020-05-02 20:03:36 +08:00
|
|
|
|
from functools import wraps
|
2021-05-10 18:39:59 +08:00
|
|
|
|
from types import ModuleType
|
2020-08-06 17:54:55 +08:00
|
|
|
|
from datetime import datetime
|
2020-09-22 11:28:38 +08:00
|
|
|
|
from contextvars import ContextVar
|
2020-06-30 10:13:58 +08:00
|
|
|
|
from collections import defaultdict
|
2021-03-24 00:28:37 +08:00
|
|
|
|
from typing import (Any, Type, List, Dict, Union, Mapping, Iterable, Callable,
|
|
|
|
|
Optional, NoReturn, TYPE_CHECKING)
|
2020-05-02 20:03:36 +08:00
|
|
|
|
|
2020-08-17 16:09:41 +08:00
|
|
|
|
from nonebot.rule import Rule
|
2020-12-06 02:30:19 +08:00
|
|
|
|
from nonebot.log import logger
|
2021-03-18 19:16:18 +08:00
|
|
|
|
from nonebot.handler import Handler
|
2020-08-18 11:28:33 +08:00
|
|
|
|
from nonebot.permission import Permission, USER
|
2021-03-24 00:28:37 +08:00
|
|
|
|
from nonebot.typing import (T_State, T_StateFactory, T_Handler, T_ArgsParser,
|
|
|
|
|
T_TypeUpdater, T_PermissionUpdater)
|
|
|
|
|
from nonebot.exception import (PausedException, RejectedException,
|
|
|
|
|
FinishedException, StopPropagation)
|
2020-05-02 20:03:36 +08:00
|
|
|
|
|
2020-12-06 02:30:19 +08:00
|
|
|
|
if TYPE_CHECKING:
|
2020-12-07 00:06:09 +08:00
|
|
|
|
from nonebot.adapters import Bot, Event, Message, MessageSegment
|
2020-12-06 02:30:19 +08:00
|
|
|
|
|
2020-06-30 10:13:58 +08:00
|
|
|
|
matchers: Dict[int, List[Type["Matcher"]]] = defaultdict(list)
|
2020-10-05 23:10:20 +08:00
|
|
|
|
"""
|
|
|
|
|
:类型: ``Dict[int, List[Type[Matcher]]]``
|
2020-10-06 02:08:48 +08:00
|
|
|
|
:说明: 用于存储当前所有的事件响应器
|
2020-10-05 23:10:20 +08:00
|
|
|
|
"""
|
2020-08-25 15:23:10 +08:00
|
|
|
|
current_bot: ContextVar = ContextVar("current_bot")
|
|
|
|
|
current_event: ContextVar = ContextVar("current_event")
|
2020-06-30 10:13:58 +08:00
|
|
|
|
|
2020-05-02 20:03:36 +08:00
|
|
|
|
|
2020-08-27 16:43:58 +08:00
|
|
|
|
class MatcherMeta(type):
|
2021-03-18 19:16:18 +08:00
|
|
|
|
if TYPE_CHECKING:
|
|
|
|
|
module: Optional[str]
|
2021-05-10 18:39:59 +08:00
|
|
|
|
plugin_name: Optional[str]
|
|
|
|
|
module_name: Optional[str]
|
|
|
|
|
module_prefix: Optional[str]
|
2021-03-18 19:16:18 +08:00
|
|
|
|
type: str
|
|
|
|
|
rule: Rule
|
|
|
|
|
permission: Permission
|
|
|
|
|
handlers: List[T_Handler]
|
|
|
|
|
priority: int
|
|
|
|
|
block: bool
|
|
|
|
|
temp: bool
|
|
|
|
|
expire_time: Optional[datetime]
|
2020-08-27 16:43:58 +08:00
|
|
|
|
|
|
|
|
|
def __repr__(self) -> str:
|
2021-05-25 20:46:27 +08:00
|
|
|
|
return (f"<Matcher from {self.module_name or 'unknown'}, "
|
2021-03-18 19:16:18 +08:00
|
|
|
|
f"type={self.type}, priority={self.priority}, "
|
|
|
|
|
f"temp={self.temp}>")
|
2020-08-27 16:43:58 +08:00
|
|
|
|
|
|
|
|
|
def __str__(self) -> str:
|
2021-03-25 18:22:56 +08:00
|
|
|
|
return repr(self)
|
2020-08-27 16:43:58 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class Matcher(metaclass=MatcherMeta):
|
2020-10-06 02:08:48 +08:00
|
|
|
|
"""事件响应器类"""
|
2021-05-10 18:39:59 +08:00
|
|
|
|
module: Optional[ModuleType] = None
|
2020-10-06 02:08:48 +08:00
|
|
|
|
"""
|
2021-05-10 18:39:59 +08:00
|
|
|
|
:类型: ``Optional[ModuleType]``
|
|
|
|
|
:说明: 事件响应器所在模块
|
2020-10-06 02:08:48 +08:00
|
|
|
|
"""
|
2021-05-10 18:39:59 +08:00
|
|
|
|
plugin_name: Optional[str] = module and getattr(module, "__plugin_name__",
|
|
|
|
|
None)
|
2021-05-10 18:42:32 +08:00
|
|
|
|
"""
|
|
|
|
|
:类型: ``Optional[str]``
|
|
|
|
|
:说明: 事件响应器所在插件名
|
|
|
|
|
"""
|
2021-05-10 18:39:59 +08:00
|
|
|
|
module_name: Optional[str] = module and getattr(module, "__module_name__",
|
|
|
|
|
None)
|
2021-05-10 18:42:32 +08:00
|
|
|
|
"""
|
|
|
|
|
:类型: ``Optional[str]``
|
|
|
|
|
:说明: 事件响应器所在模块名
|
|
|
|
|
"""
|
2021-05-10 18:39:59 +08:00
|
|
|
|
module_prefix: Optional[str] = module and getattr(module,
|
|
|
|
|
"__module_prefix__", None)
|
2021-05-10 18:42:32 +08:00
|
|
|
|
"""
|
|
|
|
|
:类型: ``Optional[str]``
|
|
|
|
|
:说明: 事件响应器所在模块前缀
|
|
|
|
|
"""
|
2020-05-02 20:03:36 +08:00
|
|
|
|
|
2020-08-20 17:15:05 +08:00
|
|
|
|
type: str = ""
|
2020-10-06 02:08:48 +08:00
|
|
|
|
"""
|
|
|
|
|
:类型: ``str``
|
|
|
|
|
:说明: 事件响应器类型
|
|
|
|
|
"""
|
2020-08-17 16:09:41 +08:00
|
|
|
|
rule: Rule = Rule()
|
2020-10-06 02:08:48 +08:00
|
|
|
|
"""
|
|
|
|
|
:类型: ``Rule``
|
|
|
|
|
:说明: 事件响应器匹配规则
|
|
|
|
|
"""
|
2020-08-17 16:09:41 +08:00
|
|
|
|
permission: Permission = Permission()
|
2020-10-06 02:08:48 +08:00
|
|
|
|
"""
|
|
|
|
|
:类型: ``Permission``
|
|
|
|
|
:说明: 事件响应器触发权限
|
|
|
|
|
"""
|
2021-03-18 19:16:18 +08:00
|
|
|
|
handlers: List[Handler] = []
|
2020-10-06 02:08:48 +08:00
|
|
|
|
"""
|
2021-03-18 19:16:18 +08:00
|
|
|
|
:类型: ``List[Handler]``
|
2020-10-06 02:08:48 +08:00
|
|
|
|
:说明: 事件响应器拥有的事件处理函数列表
|
|
|
|
|
"""
|
2020-06-30 10:13:58 +08:00
|
|
|
|
priority: int = 1
|
2020-10-06 02:08:48 +08:00
|
|
|
|
"""
|
|
|
|
|
:类型: ``int``
|
|
|
|
|
:说明: 事件响应器优先级
|
|
|
|
|
"""
|
2020-08-21 14:24:32 +08:00
|
|
|
|
block: bool = False
|
2020-10-06 02:08:48 +08:00
|
|
|
|
"""
|
|
|
|
|
:类型: ``bool``
|
|
|
|
|
:说明: 事件响应器是否阻止事件传播
|
|
|
|
|
"""
|
|
|
|
|
temp: bool = False
|
|
|
|
|
"""
|
|
|
|
|
:类型: ``bool``
|
|
|
|
|
:说明: 事件响应器是否为临时
|
|
|
|
|
"""
|
|
|
|
|
expire_time: Optional[datetime] = None
|
|
|
|
|
"""
|
|
|
|
|
:类型: ``Optional[datetime]``
|
|
|
|
|
:说明: 事件响应器过期时间点
|
|
|
|
|
"""
|
2020-05-05 16:11:05 +08:00
|
|
|
|
|
2020-12-17 21:09:30 +08:00
|
|
|
|
_default_state: T_State = {}
|
2020-10-06 02:08:48 +08:00
|
|
|
|
"""
|
2020-12-17 21:09:30 +08:00
|
|
|
|
:类型: ``T_State``
|
2020-10-06 02:08:48 +08:00
|
|
|
|
:说明: 事件响应器默认状态
|
|
|
|
|
"""
|
2020-12-20 11:59:23 +08:00
|
|
|
|
_default_state_factory: Optional[T_StateFactory] = None
|
|
|
|
|
"""
|
|
|
|
|
:类型: ``Optional[T_State]``
|
|
|
|
|
:说明: 事件响应器默认工厂函数
|
|
|
|
|
"""
|
2020-06-30 10:13:58 +08:00
|
|
|
|
|
2020-12-17 21:09:30 +08:00
|
|
|
|
_default_parser: Optional[T_ArgsParser] = None
|
2020-10-06 02:08:48 +08:00
|
|
|
|
"""
|
2020-12-17 21:09:30 +08:00
|
|
|
|
:类型: ``Optional[T_ArgsParser]``
|
2020-10-06 02:08:48 +08:00
|
|
|
|
:说明: 事件响应器默认参数解析函数
|
|
|
|
|
"""
|
2021-03-03 16:06:19 +08:00
|
|
|
|
_default_type_updater: Optional[T_TypeUpdater] = None
|
|
|
|
|
"""
|
2021-03-12 16:28:56 +08:00
|
|
|
|
:类型: ``Optional[T_TypeUpdater]``
|
2021-03-03 16:06:19 +08:00
|
|
|
|
:说明: 事件响应器类型更新函数
|
|
|
|
|
"""
|
|
|
|
|
_default_permission_updater: Optional[T_PermissionUpdater] = None
|
|
|
|
|
"""
|
2021-03-12 16:28:56 +08:00
|
|
|
|
:类型: ``Optional[T_PermissionUpdater]``
|
2021-03-03 16:06:19 +08:00
|
|
|
|
:说明: 事件响应器权限更新函数
|
|
|
|
|
"""
|
2020-05-05 16:11:05 +08:00
|
|
|
|
|
|
|
|
|
def __init__(self):
|
2020-11-30 11:08:00 +08:00
|
|
|
|
"""实例化 Matcher 以便运行"""
|
2020-05-05 16:11:05 +08:00
|
|
|
|
self.handlers = self.handlers.copy()
|
|
|
|
|
self.state = self._default_state.copy()
|
|
|
|
|
|
2020-08-24 17:59:36 +08:00
|
|
|
|
def __repr__(self) -> str:
|
2021-05-10 18:39:59 +08:00
|
|
|
|
return (
|
|
|
|
|
f"<Matcher from {self.module_name or 'unknown'}, type={self.type}, "
|
|
|
|
|
f"priority={self.priority}, temp={self.temp}>")
|
2020-08-24 17:59:36 +08:00
|
|
|
|
|
|
|
|
|
def __str__(self) -> str:
|
2021-03-25 18:22:56 +08:00
|
|
|
|
return repr(self)
|
2020-08-24 17:59:36 +08:00
|
|
|
|
|
2020-05-05 16:11:05 +08:00
|
|
|
|
@classmethod
|
2021-05-30 10:55:34 +08:00
|
|
|
|
def new(
|
|
|
|
|
cls,
|
|
|
|
|
type_: str = "",
|
|
|
|
|
rule: Optional[Rule] = None,
|
|
|
|
|
permission: Optional[Permission] = None,
|
|
|
|
|
handlers: Optional[Union[List[T_Handler], List[Handler],
|
|
|
|
|
List[Union[T_Handler, Handler]]]] = None,
|
|
|
|
|
temp: bool = False,
|
|
|
|
|
priority: int = 1,
|
|
|
|
|
block: bool = False,
|
|
|
|
|
*,
|
|
|
|
|
module: Optional[ModuleType] = None,
|
|
|
|
|
expire_time: Optional[datetime] = None,
|
|
|
|
|
default_state: Optional[T_State] = None,
|
|
|
|
|
default_state_factory: Optional[T_StateFactory] = None,
|
|
|
|
|
default_parser: Optional[T_ArgsParser] = None,
|
|
|
|
|
default_type_updater: Optional[T_TypeUpdater] = None,
|
|
|
|
|
default_permission_updater: Optional[T_PermissionUpdater] = None
|
|
|
|
|
) -> Type["Matcher"]:
|
2020-10-06 02:08:48 +08:00
|
|
|
|
"""
|
|
|
|
|
:说明:
|
2020-11-30 11:08:00 +08:00
|
|
|
|
|
2020-10-06 02:08:48 +08:00
|
|
|
|
创建一个新的事件响应器,并存储至 `matchers <#matchers>`_
|
2020-11-30 11:08:00 +08:00
|
|
|
|
|
2020-10-06 02:08:48 +08:00
|
|
|
|
:参数:
|
2020-11-30 11:08:00 +08:00
|
|
|
|
|
2020-12-31 17:58:09 +08:00
|
|
|
|
* ``type_: str``: 事件响应器类型,与 ``event.get_type()`` 一致时触发,空字符串表示任意
|
2020-10-06 02:08:48 +08:00
|
|
|
|
* ``rule: Optional[Rule]``: 匹配规则
|
|
|
|
|
* ``permission: Optional[Permission]``: 权限
|
2020-12-17 21:09:30 +08:00
|
|
|
|
* ``handlers: Optional[List[T_Handler]]``: 事件处理函数列表
|
2020-10-06 02:08:48 +08:00
|
|
|
|
* ``temp: bool``: 是否为临时事件响应器,即触发一次后删除
|
|
|
|
|
* ``priority: int``: 响应优先级
|
|
|
|
|
* ``block: bool``: 是否阻止事件向更低优先级的响应器传播
|
|
|
|
|
* ``module: Optional[str]``: 事件响应器所在模块名称
|
2020-12-17 21:09:30 +08:00
|
|
|
|
* ``default_state: Optional[T_State]``: 默认状态 ``state``
|
2020-12-20 11:59:23 +08:00
|
|
|
|
* ``default_state_factory: Optional[T_StateFactory]``: 默认状态 ``state`` 的工厂函数
|
2020-10-06 02:08:48 +08:00
|
|
|
|
* ``expire_time: Optional[datetime]``: 事件响应器最终有效时间点,过时即被删除
|
2020-11-30 11:08:00 +08:00
|
|
|
|
|
2020-10-06 02:08:48 +08:00
|
|
|
|
:返回:
|
2020-11-30 11:08:00 +08:00
|
|
|
|
|
2020-10-06 02:08:48 +08:00
|
|
|
|
- ``Type[Matcher]``: 新的事件响应器类
|
2020-07-04 22:51:10 +08:00
|
|
|
|
"""
|
2020-05-05 16:11:05 +08:00
|
|
|
|
|
|
|
|
|
NewMatcher = type(
|
2020-06-24 21:56:49 +08:00
|
|
|
|
"Matcher", (Matcher,), {
|
2020-12-20 12:09:30 +08:00
|
|
|
|
"module":
|
|
|
|
|
module,
|
2021-05-10 18:39:59 +08:00
|
|
|
|
"plugin_name":
|
|
|
|
|
module and getattr(module, "__plugin_name__", None),
|
|
|
|
|
"module_name":
|
|
|
|
|
module and getattr(module, "__module_name__", None),
|
|
|
|
|
"module_prefix":
|
|
|
|
|
module and getattr(module, "__module_prefix__", None),
|
2020-12-20 12:09:30 +08:00
|
|
|
|
"type":
|
|
|
|
|
type_,
|
|
|
|
|
"rule":
|
|
|
|
|
rule or Rule(),
|
|
|
|
|
"permission":
|
|
|
|
|
permission or Permission(),
|
2021-02-02 12:48:16 +08:00
|
|
|
|
"handlers": [
|
2021-03-18 19:16:18 +08:00
|
|
|
|
handler
|
|
|
|
|
if isinstance(handler, Handler) else Handler(handler)
|
|
|
|
|
for handler in handlers
|
2021-02-02 12:48:16 +08:00
|
|
|
|
] if handlers else [],
|
2020-12-20 12:09:30 +08:00
|
|
|
|
"temp":
|
|
|
|
|
temp,
|
|
|
|
|
"expire_time":
|
|
|
|
|
expire_time,
|
|
|
|
|
"priority":
|
|
|
|
|
priority,
|
|
|
|
|
"block":
|
|
|
|
|
block,
|
|
|
|
|
"_default_state":
|
|
|
|
|
default_state or {},
|
|
|
|
|
"_default_state_factory":
|
|
|
|
|
staticmethod(default_state_factory)
|
2021-05-30 10:55:34 +08:00
|
|
|
|
if default_state_factory else None,
|
|
|
|
|
"_default_parser":
|
|
|
|
|
default_parser,
|
|
|
|
|
"_default_type_updater":
|
|
|
|
|
default_type_updater,
|
|
|
|
|
"_default_permission_updater":
|
|
|
|
|
default_permission_updater
|
2020-05-05 16:11:05 +08:00
|
|
|
|
})
|
|
|
|
|
|
2020-06-30 10:13:58 +08:00
|
|
|
|
matchers[priority].append(NewMatcher)
|
|
|
|
|
|
2020-05-05 16:11:05 +08:00
|
|
|
|
return NewMatcher
|
|
|
|
|
|
2020-07-04 22:51:10 +08:00
|
|
|
|
@classmethod
|
2020-12-06 02:30:19 +08:00
|
|
|
|
async def check_perm(cls, bot: "Bot", event: "Event") -> bool:
|
2020-10-06 02:08:48 +08:00
|
|
|
|
"""
|
|
|
|
|
:说明:
|
2020-11-30 11:08:00 +08:00
|
|
|
|
|
2020-10-06 02:08:48 +08:00
|
|
|
|
检查是否满足触发权限
|
2020-11-30 11:08:00 +08:00
|
|
|
|
|
2020-10-06 02:08:48 +08:00
|
|
|
|
:参数:
|
2020-11-30 11:08:00 +08:00
|
|
|
|
|
2020-10-06 02:08:48 +08:00
|
|
|
|
* ``bot: Bot``: Bot 对象
|
|
|
|
|
* ``event: Event``: 上报事件
|
2020-11-30 11:08:00 +08:00
|
|
|
|
|
2020-10-06 02:08:48 +08:00
|
|
|
|
:返回:
|
2020-11-30 11:08:00 +08:00
|
|
|
|
|
2020-10-06 02:08:48 +08:00
|
|
|
|
- ``bool``: 是否满足权限
|
|
|
|
|
"""
|
2021-02-02 12:48:16 +08:00
|
|
|
|
event_type = event.get_type()
|
|
|
|
|
return (event_type == (cls.type or event_type) and
|
|
|
|
|
await cls.permission(bot, event))
|
2020-08-17 16:09:41 +08:00
|
|
|
|
|
|
|
|
|
@classmethod
|
2020-12-17 21:09:30 +08:00
|
|
|
|
async def check_rule(cls, bot: "Bot", event: "Event",
|
|
|
|
|
state: T_State) -> bool:
|
2020-10-06 02:08:48 +08:00
|
|
|
|
"""
|
|
|
|
|
:说明:
|
2020-11-30 11:08:00 +08:00
|
|
|
|
|
2020-10-06 02:08:48 +08:00
|
|
|
|
检查是否满足匹配规则
|
2020-11-30 11:08:00 +08:00
|
|
|
|
|
2020-10-06 02:08:48 +08:00
|
|
|
|
:参数:
|
2020-11-30 11:08:00 +08:00
|
|
|
|
|
2020-10-06 02:08:48 +08:00
|
|
|
|
* ``bot: Bot``: Bot 对象
|
|
|
|
|
* ``event: Event``: 上报事件
|
2020-12-17 21:09:30 +08:00
|
|
|
|
* ``state: T_State``: 当前状态
|
2020-11-30 11:08:00 +08:00
|
|
|
|
|
2020-10-06 02:08:48 +08:00
|
|
|
|
:返回:
|
2020-11-30 11:08:00 +08:00
|
|
|
|
|
2020-10-06 02:08:48 +08:00
|
|
|
|
- ``bool``: 是否满足匹配规则
|
2020-07-04 22:51:10 +08:00
|
|
|
|
"""
|
2020-12-09 19:57:49 +08:00
|
|
|
|
event_type = event.get_type()
|
|
|
|
|
return (event_type == (cls.type or event_type) and
|
2020-09-22 00:18:57 +08:00
|
|
|
|
await cls.rule(bot, event, state))
|
2020-07-04 22:51:10 +08:00
|
|
|
|
|
2020-08-18 11:28:33 +08:00
|
|
|
|
@classmethod
|
2020-12-17 21:09:30 +08:00
|
|
|
|
def args_parser(cls, func: T_ArgsParser) -> T_ArgsParser:
|
2020-10-06 02:08:48 +08:00
|
|
|
|
"""
|
|
|
|
|
:说明:
|
2020-11-30 11:08:00 +08:00
|
|
|
|
|
2020-10-06 17:03:05 +08:00
|
|
|
|
装饰一个函数来更改当前事件响应器的默认参数解析函数
|
2020-11-30 11:08:00 +08:00
|
|
|
|
|
2020-10-06 02:08:48 +08:00
|
|
|
|
:参数:
|
2020-11-30 11:08:00 +08:00
|
|
|
|
|
2020-12-17 21:09:30 +08:00
|
|
|
|
* ``func: T_ArgsParser``: 参数解析函数
|
2020-10-06 02:08:48 +08:00
|
|
|
|
"""
|
2020-08-18 11:28:33 +08:00
|
|
|
|
cls._default_parser = func
|
|
|
|
|
return func
|
2020-06-30 10:13:58 +08:00
|
|
|
|
|
2021-03-03 16:06:19 +08:00
|
|
|
|
@classmethod
|
|
|
|
|
def type_updater(cls, func: T_TypeUpdater) -> T_TypeUpdater:
|
|
|
|
|
"""
|
|
|
|
|
:说明:
|
|
|
|
|
|
|
|
|
|
装饰一个函数来更改当前事件响应器的默认响应事件类型更新函数
|
|
|
|
|
|
|
|
|
|
:参数:
|
|
|
|
|
|
|
|
|
|
* ``func: T_TypeUpdater``: 响应事件类型更新函数
|
|
|
|
|
"""
|
|
|
|
|
cls._default_type_updater = func
|
|
|
|
|
return func
|
|
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
|
def permission_updater(cls,
|
|
|
|
|
func: T_PermissionUpdater) -> T_PermissionUpdater:
|
|
|
|
|
"""
|
|
|
|
|
:说明:
|
|
|
|
|
|
|
|
|
|
装饰一个函数来更改当前事件响应器的默认会话权限更新函数
|
|
|
|
|
|
|
|
|
|
:参数:
|
|
|
|
|
|
|
|
|
|
* ``func: T_PermissionUpdater``: 会话权限更新函数
|
|
|
|
|
"""
|
|
|
|
|
cls._default_permission_updater = func
|
|
|
|
|
return func
|
|
|
|
|
|
2020-12-16 23:13:00 +08:00
|
|
|
|
@classmethod
|
2021-03-19 14:59:59 +08:00
|
|
|
|
def append_handler(cls, handler: T_Handler) -> Handler:
|
|
|
|
|
handler_ = Handler(handler)
|
2021-03-18 19:16:18 +08:00
|
|
|
|
cls.handlers.append(handler_)
|
|
|
|
|
return handler_
|
2020-12-16 23:13:00 +08:00
|
|
|
|
|
2020-05-05 16:11:05 +08:00
|
|
|
|
@classmethod
|
2020-12-17 21:09:30 +08:00
|
|
|
|
def handle(cls) -> Callable[[T_Handler], T_Handler]:
|
2020-10-06 17:03:05 +08:00
|
|
|
|
"""
|
|
|
|
|
:说明:
|
2020-11-30 11:08:00 +08:00
|
|
|
|
|
2020-10-06 17:03:05 +08:00
|
|
|
|
装饰一个函数来向事件响应器直接添加一个处理函数
|
2020-11-30 11:08:00 +08:00
|
|
|
|
|
2020-10-06 17:03:05 +08:00
|
|
|
|
:参数:
|
2020-11-30 11:08:00 +08:00
|
|
|
|
|
2020-10-06 17:03:05 +08:00
|
|
|
|
* 无
|
|
|
|
|
"""
|
2020-06-30 10:13:58 +08:00
|
|
|
|
|
2020-12-17 21:09:30 +08:00
|
|
|
|
def _decorator(func: T_Handler) -> T_Handler:
|
2020-12-16 23:13:00 +08:00
|
|
|
|
cls.append_handler(func)
|
2020-06-30 10:13:58 +08:00
|
|
|
|
return func
|
|
|
|
|
|
|
|
|
|
return _decorator
|
2020-05-02 20:03:36 +08:00
|
|
|
|
|
2020-05-05 16:11:05 +08:00
|
|
|
|
@classmethod
|
2020-12-17 21:09:30 +08:00
|
|
|
|
def receive(cls) -> Callable[[T_Handler], T_Handler]:
|
2020-10-06 17:03:05 +08:00
|
|
|
|
"""
|
|
|
|
|
:说明:
|
2020-11-30 11:08:00 +08:00
|
|
|
|
|
2020-10-06 17:03:05 +08:00
|
|
|
|
装饰一个函数来指示 NoneBot 在接收用户新的一条消息后继续运行该函数
|
2020-11-30 11:08:00 +08:00
|
|
|
|
|
2020-10-06 17:03:05 +08:00
|
|
|
|
:参数:
|
2020-11-30 11:08:00 +08:00
|
|
|
|
|
2020-10-06 17:03:05 +08:00
|
|
|
|
* 无
|
|
|
|
|
"""
|
2020-05-02 20:03:36 +08:00
|
|
|
|
|
2021-01-06 18:29:24 +08:00
|
|
|
|
async def _receive(bot: "Bot", event: "Event") -> NoReturn:
|
2020-08-22 15:22:46 +08:00
|
|
|
|
raise PausedException
|
2020-05-02 20:03:36 +08:00
|
|
|
|
|
2020-08-22 15:22:46 +08:00
|
|
|
|
if cls.handlers:
|
|
|
|
|
# 已有前置handlers则接受一条新的消息,否则视为接收初始消息
|
2021-03-18 19:16:18 +08:00
|
|
|
|
receive_handler = cls.append_handler(_receive)
|
|
|
|
|
else:
|
|
|
|
|
receive_handler = None
|
2020-05-05 16:11:05 +08:00
|
|
|
|
|
2020-12-17 21:09:30 +08:00
|
|
|
|
def _decorator(func: T_Handler) -> T_Handler:
|
2020-08-24 17:59:36 +08:00
|
|
|
|
if not cls.handlers or cls.handlers[-1] is not func:
|
2021-03-18 19:16:18 +08:00
|
|
|
|
func_handler = cls.append_handler(func)
|
2021-03-18 21:30:52 +08:00
|
|
|
|
if receive_handler:
|
|
|
|
|
receive_handler.update_signature(
|
|
|
|
|
bot=func_handler.bot_type,
|
|
|
|
|
event=func_handler.event_type)
|
2021-01-06 18:29:24 +08:00
|
|
|
|
|
2020-05-02 20:03:36 +08:00
|
|
|
|
return func
|
|
|
|
|
|
|
|
|
|
return _decorator
|
|
|
|
|
|
2020-08-18 11:28:33 +08:00
|
|
|
|
@classmethod
|
|
|
|
|
def got(
|
|
|
|
|
cls,
|
|
|
|
|
key: str,
|
2020-12-06 02:30:19 +08:00
|
|
|
|
prompt: Optional[Union[str, "Message", "MessageSegment"]] = None,
|
2020-12-17 21:09:30 +08:00
|
|
|
|
args_parser: Optional[T_ArgsParser] = None
|
|
|
|
|
) -> Callable[[T_Handler], T_Handler]:
|
2020-10-06 17:03:05 +08:00
|
|
|
|
"""
|
|
|
|
|
:说明:
|
2020-11-30 11:08:00 +08:00
|
|
|
|
|
2020-10-06 17:03:05 +08:00
|
|
|
|
装饰一个函数来指示 NoneBot 当要获取的 ``key`` 不存在时接收用户新的一条消息并经过 ``ArgsParser`` 处理后再运行该函数,如果 ``key`` 已存在则直接继续运行
|
2020-11-30 11:08:00 +08:00
|
|
|
|
|
2020-10-06 17:03:05 +08:00
|
|
|
|
:参数:
|
2020-11-30 11:08:00 +08:00
|
|
|
|
|
2020-10-06 17:03:05 +08:00
|
|
|
|
* ``key: str``: 参数名
|
|
|
|
|
* ``prompt: Optional[Union[str, Message, MessageSegment]]``: 在参数不存在时向用户发送的消息
|
2020-12-17 21:09:30 +08:00
|
|
|
|
* ``args_parser: Optional[T_ArgsParser]``: 可选参数解析函数,空则使用默认解析函数
|
2020-10-06 17:03:05 +08:00
|
|
|
|
"""
|
2020-05-02 20:03:36 +08:00
|
|
|
|
|
2020-12-17 21:09:30 +08:00
|
|
|
|
async def _key_getter(bot: "Bot", event: "Event", state: T_State):
|
2020-09-26 17:36:04 +08:00
|
|
|
|
state["_current_key"] = key
|
2020-08-22 15:22:46 +08:00
|
|
|
|
if key not in state:
|
|
|
|
|
if prompt:
|
2021-03-02 14:35:02 +08:00
|
|
|
|
if isinstance(prompt, str):
|
|
|
|
|
await bot.send(event=event,
|
|
|
|
|
message=prompt.format(**state))
|
|
|
|
|
elif isinstance(prompt, Mapping):
|
|
|
|
|
if prompt.is_text():
|
|
|
|
|
await bot.send(event=event,
|
|
|
|
|
message=str(prompt).format(**state))
|
|
|
|
|
else:
|
|
|
|
|
await bot.send(event=event, message=prompt)
|
|
|
|
|
elif isinstance(prompt, Iterable):
|
|
|
|
|
await bot.send(
|
|
|
|
|
event=event,
|
|
|
|
|
message=prompt.__class__(
|
|
|
|
|
str(prompt).format(**state)) # type: ignore
|
|
|
|
|
)
|
|
|
|
|
else:
|
|
|
|
|
logger.warning("Unknown prompt type, ignored.")
|
2020-08-22 15:22:46 +08:00
|
|
|
|
raise PausedException
|
2020-09-26 17:36:04 +08:00
|
|
|
|
else:
|
|
|
|
|
state["_skip_key"] = True
|
2020-05-02 20:03:36 +08:00
|
|
|
|
|
2020-12-17 21:09:30 +08:00
|
|
|
|
async def _key_parser(bot: "Bot", event: "Event", state: T_State):
|
2020-09-26 17:36:04 +08:00
|
|
|
|
if key in state and state.get("_skip_key"):
|
|
|
|
|
del state["_skip_key"]
|
2020-09-26 16:33:30 +08:00
|
|
|
|
return
|
2020-08-22 15:22:46 +08:00
|
|
|
|
parser = args_parser or cls._default_parser
|
|
|
|
|
if parser:
|
2020-12-17 21:09:30 +08:00
|
|
|
|
# parser = cast(T_ArgsParser["Bot", "Event"], parser)
|
2020-08-22 15:22:46 +08:00
|
|
|
|
await parser(bot, event, state)
|
|
|
|
|
else:
|
2020-12-09 19:57:49 +08:00
|
|
|
|
state[state["_current_key"]] = str(event.get_message())
|
2020-08-22 15:22:46 +08:00
|
|
|
|
|
2021-03-18 19:16:18 +08:00
|
|
|
|
getter_handler = cls.append_handler(_key_getter)
|
|
|
|
|
parser_handler = cls.append_handler(_key_parser)
|
2020-08-22 15:22:46 +08:00
|
|
|
|
|
2020-12-17 21:09:30 +08:00
|
|
|
|
def _decorator(func: T_Handler) -> T_Handler:
|
2020-08-24 17:59:36 +08:00
|
|
|
|
if not hasattr(cls.handlers[-1], "__wrapped__"):
|
|
|
|
|
parser = cls.handlers.pop()
|
2021-04-01 20:05:31 +08:00
|
|
|
|
func_handler = Handler(func)
|
2020-08-24 17:59:36 +08:00
|
|
|
|
|
|
|
|
|
@wraps(func)
|
2020-12-24 22:19:08 +08:00
|
|
|
|
async def wrapper(bot: "Bot", event: "Event", state: T_State,
|
|
|
|
|
matcher: Matcher):
|
2021-03-18 19:16:18 +08:00
|
|
|
|
await parser(matcher, bot, event, state)
|
|
|
|
|
await func_handler(matcher, bot, event, state)
|
2020-09-26 16:33:30 +08:00
|
|
|
|
if "_current_key" in state:
|
|
|
|
|
del state["_current_key"]
|
2020-08-24 17:59:36 +08:00
|
|
|
|
|
2021-04-01 20:05:31 +08:00
|
|
|
|
wrapper_handler = cls.append_handler(wrapper)
|
2021-03-18 19:16:18 +08:00
|
|
|
|
|
2021-04-01 20:05:31 +08:00
|
|
|
|
getter_handler.update_signature(
|
|
|
|
|
bot=wrapper_handler.bot_type,
|
|
|
|
|
event=wrapper_handler.event_type)
|
|
|
|
|
parser_handler.update_signature(
|
|
|
|
|
bot=wrapper_handler.bot_type,
|
|
|
|
|
event=wrapper_handler.event_type)
|
2021-01-06 18:29:24 +08:00
|
|
|
|
|
2020-08-18 11:28:33 +08:00
|
|
|
|
return func
|
2020-05-02 20:03:36 +08:00
|
|
|
|
|
2020-08-18 11:28:33 +08:00
|
|
|
|
return _decorator
|
2020-05-02 20:03:36 +08:00
|
|
|
|
|
2020-10-06 16:24:04 +08:00
|
|
|
|
@classmethod
|
2020-12-06 02:30:19 +08:00
|
|
|
|
async def send(cls, message: Union[str, "Message", "MessageSegment"],
|
2021-03-24 00:28:37 +08:00
|
|
|
|
**kwargs) -> Any:
|
2020-10-06 17:03:05 +08:00
|
|
|
|
"""
|
|
|
|
|
:说明:
|
2020-11-30 11:08:00 +08:00
|
|
|
|
|
2020-10-06 17:03:05 +08:00
|
|
|
|
发送一条消息给当前交互用户
|
2020-11-30 11:08:00 +08:00
|
|
|
|
|
2020-10-06 17:03:05 +08:00
|
|
|
|
:参数:
|
2020-11-30 11:08:00 +08:00
|
|
|
|
|
2020-10-06 17:03:05 +08:00
|
|
|
|
* ``message: Union[str, Message, MessageSegment]``: 消息内容
|
2020-10-17 19:50:25 +08:00
|
|
|
|
* ``**kwargs``: 其他传递给 ``bot.send`` 的参数,请参考对应 adapter 的 bot 对象 api
|
2020-10-06 17:03:05 +08:00
|
|
|
|
"""
|
2021-03-24 00:28:37 +08:00
|
|
|
|
bot: "Bot" = current_bot.get()
|
2020-10-06 16:24:04 +08:00
|
|
|
|
event = current_event.get()
|
2021-03-24 00:28:37 +08:00
|
|
|
|
return await bot.send(event=event, message=message, **kwargs)
|
2020-10-06 16:24:04 +08:00
|
|
|
|
|
2020-08-18 11:28:33 +08:00
|
|
|
|
@classmethod
|
2020-10-17 19:50:25 +08:00
|
|
|
|
async def finish(cls,
|
2020-12-06 02:30:19 +08:00
|
|
|
|
message: Optional[Union[str, "Message",
|
|
|
|
|
"MessageSegment"]] = None,
|
2020-10-17 19:50:25 +08:00
|
|
|
|
**kwargs) -> NoReturn:
|
2020-10-06 17:03:05 +08:00
|
|
|
|
"""
|
|
|
|
|
:说明:
|
2020-11-30 11:08:00 +08:00
|
|
|
|
|
2020-10-06 17:03:05 +08:00
|
|
|
|
发送一条消息给当前交互用户并结束当前事件响应器
|
2020-11-30 11:08:00 +08:00
|
|
|
|
|
2020-10-06 17:03:05 +08:00
|
|
|
|
:参数:
|
2020-11-30 11:08:00 +08:00
|
|
|
|
|
2020-10-06 17:03:05 +08:00
|
|
|
|
* ``message: Union[str, Message, MessageSegment]``: 消息内容
|
2020-10-17 19:50:25 +08:00
|
|
|
|
* ``**kwargs``: 其他传递给 ``bot.send`` 的参数,请参考对应 adapter 的 bot 对象 api
|
2020-10-06 17:03:05 +08:00
|
|
|
|
"""
|
2020-10-06 16:24:04 +08:00
|
|
|
|
bot = current_bot.get()
|
|
|
|
|
event = current_event.get()
|
2020-10-06 17:03:05 +08:00
|
|
|
|
if message:
|
2020-10-17 19:50:25 +08:00
|
|
|
|
await bot.send(event=event, message=message, **kwargs)
|
2020-08-18 11:28:33 +08:00
|
|
|
|
raise FinishedException
|
2020-05-02 20:03:36 +08:00
|
|
|
|
|
2020-08-18 11:28:33 +08:00
|
|
|
|
@classmethod
|
2020-10-17 19:50:25 +08:00
|
|
|
|
async def pause(cls,
|
2020-12-06 02:30:19 +08:00
|
|
|
|
prompt: Optional[Union[str, "Message",
|
|
|
|
|
"MessageSegment"]] = None,
|
2020-10-17 19:50:25 +08:00
|
|
|
|
**kwargs) -> NoReturn:
|
2020-10-06 17:03:05 +08:00
|
|
|
|
"""
|
|
|
|
|
:说明:
|
2020-11-30 11:08:00 +08:00
|
|
|
|
|
2020-10-06 17:03:05 +08:00
|
|
|
|
发送一条消息给当前交互用户并暂停事件响应器,在接收用户新的一条消息后继续下一个处理函数
|
2020-11-30 11:08:00 +08:00
|
|
|
|
|
2020-10-06 17:03:05 +08:00
|
|
|
|
:参数:
|
2020-11-30 11:08:00 +08:00
|
|
|
|
|
2020-10-06 17:03:05 +08:00
|
|
|
|
* ``prompt: Union[str, Message, MessageSegment]``: 消息内容
|
2020-10-17 19:50:25 +08:00
|
|
|
|
* ``**kwargs``: 其他传递给 ``bot.send`` 的参数,请参考对应 adapter 的 bot 对象 api
|
2020-10-06 17:03:05 +08:00
|
|
|
|
"""
|
2020-10-06 16:24:04 +08:00
|
|
|
|
bot = current_bot.get()
|
|
|
|
|
event = current_event.get()
|
2020-08-25 15:23:10 +08:00
|
|
|
|
if prompt:
|
2020-10-17 19:50:25 +08:00
|
|
|
|
await bot.send(event=event, message=prompt, **kwargs)
|
2020-08-18 11:28:33 +08:00
|
|
|
|
raise PausedException
|
2020-06-30 10:13:58 +08:00
|
|
|
|
|
2020-08-18 11:28:33 +08:00
|
|
|
|
@classmethod
|
2020-10-17 19:50:25 +08:00
|
|
|
|
async def reject(cls,
|
2020-12-06 02:30:19 +08:00
|
|
|
|
prompt: Optional[Union[str, "Message",
|
|
|
|
|
"MessageSegment"]] = None,
|
2020-10-17 19:50:25 +08:00
|
|
|
|
**kwargs) -> NoReturn:
|
2020-10-06 17:03:05 +08:00
|
|
|
|
"""
|
|
|
|
|
:说明:
|
2020-11-30 11:08:00 +08:00
|
|
|
|
|
2020-10-06 17:03:05 +08:00
|
|
|
|
发送一条消息给当前交互用户并暂停事件响应器,在接收用户新的一条消息后重新运行当前处理函数
|
2020-11-30 11:08:00 +08:00
|
|
|
|
|
2020-10-06 17:03:05 +08:00
|
|
|
|
:参数:
|
2020-11-30 11:08:00 +08:00
|
|
|
|
|
2020-10-06 17:03:05 +08:00
|
|
|
|
* ``prompt: Union[str, Message, MessageSegment]``: 消息内容
|
2020-10-17 19:50:25 +08:00
|
|
|
|
* ``**kwargs``: 其他传递给 ``bot.send`` 的参数,请参考对应 adapter 的 bot 对象 api
|
2020-10-06 17:03:05 +08:00
|
|
|
|
"""
|
2020-10-06 16:24:04 +08:00
|
|
|
|
bot = current_bot.get()
|
|
|
|
|
event = current_event.get()
|
2020-08-25 15:23:10 +08:00
|
|
|
|
if prompt:
|
2020-10-17 19:50:25 +08:00
|
|
|
|
await bot.send(event=event, message=prompt, **kwargs)
|
2020-08-18 11:28:33 +08:00
|
|
|
|
raise RejectedException
|
2020-05-02 20:03:36 +08:00
|
|
|
|
|
2020-12-24 22:19:08 +08:00
|
|
|
|
def stop_propagation(self):
|
2021-02-26 17:00:38 +08:00
|
|
|
|
"""
|
|
|
|
|
:说明:
|
|
|
|
|
|
|
|
|
|
阻止事件传播
|
|
|
|
|
"""
|
2020-12-24 22:19:08 +08:00
|
|
|
|
self.block = True
|
|
|
|
|
|
2020-07-04 22:51:10 +08:00
|
|
|
|
# 运行handlers
|
2020-12-17 21:09:30 +08:00
|
|
|
|
async def run(self, bot: "Bot", event: "Event", state: T_State):
|
2020-08-25 15:23:10 +08:00
|
|
|
|
b_t = current_bot.set(bot)
|
|
|
|
|
e_t = current_event.set(event)
|
2020-05-05 16:11:05 +08:00
|
|
|
|
try:
|
2020-08-17 16:09:41 +08:00
|
|
|
|
# Refresh preprocess state
|
2020-12-20 11:59:23 +08:00
|
|
|
|
state_ = await self._default_state_factory(
|
|
|
|
|
bot, event) if self._default_state_factory else self.state
|
|
|
|
|
state_.update(state)
|
2020-08-17 16:09:41 +08:00
|
|
|
|
|
2020-05-05 16:11:05 +08:00
|
|
|
|
for _ in range(len(self.handlers)):
|
|
|
|
|
handler = self.handlers.pop(0)
|
2021-03-18 19:16:18 +08:00
|
|
|
|
await handler(self, bot, event, state_)
|
2020-08-18 11:28:33 +08:00
|
|
|
|
|
2020-05-05 16:11:05 +08:00
|
|
|
|
except RejectedException:
|
2020-06-30 10:13:58 +08:00
|
|
|
|
self.handlers.insert(0, handler) # type: ignore
|
2021-03-12 16:28:56 +08:00
|
|
|
|
updater = self.__class__._default_type_updater
|
|
|
|
|
if updater:
|
2021-05-31 21:49:25 +08:00
|
|
|
|
type_ = await updater(
|
|
|
|
|
bot,
|
|
|
|
|
event,
|
|
|
|
|
state_, # type: ignore
|
|
|
|
|
self.type)
|
2021-03-03 16:06:19 +08:00
|
|
|
|
else:
|
|
|
|
|
type_ = "message"
|
2021-03-12 16:28:56 +08:00
|
|
|
|
|
|
|
|
|
updater = self.__class__._default_permission_updater
|
|
|
|
|
if updater:
|
2021-05-31 21:49:25 +08:00
|
|
|
|
permission = await updater(
|
|
|
|
|
bot,
|
|
|
|
|
event,
|
|
|
|
|
state_, # type: ignore
|
|
|
|
|
self.permission)
|
2021-03-03 16:06:19 +08:00
|
|
|
|
else:
|
|
|
|
|
permission = USER(event.get_session_id(), perm=self.permission)
|
2021-03-12 16:28:56 +08:00
|
|
|
|
|
2021-05-30 10:55:34 +08:00
|
|
|
|
Matcher.new(
|
|
|
|
|
type_,
|
|
|
|
|
Rule(),
|
|
|
|
|
permission,
|
|
|
|
|
self.handlers,
|
|
|
|
|
temp=True,
|
|
|
|
|
priority=0,
|
|
|
|
|
block=True,
|
|
|
|
|
module=self.module,
|
|
|
|
|
expire_time=datetime.now() + bot.config.session_expire_timeout,
|
|
|
|
|
default_state=self.state,
|
|
|
|
|
default_parser=self.__class__._default_parser,
|
|
|
|
|
default_type_updater=self.__class__._default_type_updater,
|
|
|
|
|
default_permission_updater=self.__class__.
|
|
|
|
|
_default_permission_updater)
|
2020-05-05 16:11:05 +08:00
|
|
|
|
except PausedException:
|
2021-03-12 16:28:56 +08:00
|
|
|
|
updater = self.__class__._default_type_updater
|
|
|
|
|
if updater:
|
2021-05-31 21:49:25 +08:00
|
|
|
|
type_ = await updater(
|
|
|
|
|
bot,
|
|
|
|
|
event,
|
|
|
|
|
state_, # type: ignore
|
|
|
|
|
self.type)
|
2021-03-03 16:06:19 +08:00
|
|
|
|
else:
|
|
|
|
|
type_ = "message"
|
2021-03-12 16:28:56 +08:00
|
|
|
|
|
|
|
|
|
updater = self.__class__._default_permission_updater
|
|
|
|
|
if updater:
|
2021-05-31 21:49:25 +08:00
|
|
|
|
permission = await updater(
|
|
|
|
|
bot,
|
|
|
|
|
event,
|
|
|
|
|
state_, # type: ignore
|
|
|
|
|
self.permission)
|
2021-03-03 16:06:19 +08:00
|
|
|
|
else:
|
|
|
|
|
permission = USER(event.get_session_id(), perm=self.permission)
|
2021-03-12 16:28:56 +08:00
|
|
|
|
|
2021-05-30 10:55:34 +08:00
|
|
|
|
Matcher.new(
|
|
|
|
|
type_,
|
|
|
|
|
Rule(),
|
|
|
|
|
permission,
|
|
|
|
|
self.handlers,
|
|
|
|
|
temp=True,
|
|
|
|
|
priority=0,
|
|
|
|
|
block=True,
|
|
|
|
|
module=self.module,
|
|
|
|
|
expire_time=datetime.now() + bot.config.session_expire_timeout,
|
|
|
|
|
default_state=self.state,
|
|
|
|
|
default_parser=self.__class__._default_parser,
|
|
|
|
|
default_type_updater=self.__class__._default_type_updater,
|
|
|
|
|
default_permission_updater=self.__class__.
|
|
|
|
|
_default_permission_updater)
|
2020-05-05 16:11:05 +08:00
|
|
|
|
except FinishedException:
|
2020-08-25 15:23:10 +08:00
|
|
|
|
pass
|
2020-12-24 22:19:08 +08:00
|
|
|
|
except StopPropagation:
|
|
|
|
|
self.block = True
|
2020-08-25 15:23:10 +08:00
|
|
|
|
finally:
|
2020-08-27 16:43:58 +08:00
|
|
|
|
logger.info(f"Matcher {self} running complete")
|
2020-08-25 15:23:10 +08:00
|
|
|
|
current_bot.reset(b_t)
|
|
|
|
|
current_event.reset(e_t)
|