2023-09-09 13:46:09 +08:00
|
|
|
|
import sys
|
|
|
|
|
import inspect
|
|
|
|
|
import warnings
|
|
|
|
|
from pathlib import Path
|
2022-02-06 14:52:50 +08:00
|
|
|
|
from types import ModuleType
|
2023-09-09 13:46:09 +08:00
|
|
|
|
from dataclasses import dataclass
|
2022-02-06 14:52:50 +08:00
|
|
|
|
from contextvars import ContextVar
|
2023-03-17 15:48:48 +08:00
|
|
|
|
from typing_extensions import Self
|
2024-04-16 00:33:48 +08:00
|
|
|
|
from collections.abc import Iterable
|
2022-07-20 10:21:31 +08:00
|
|
|
|
from datetime import datetime, timedelta
|
2022-11-15 10:50:52 +08:00
|
|
|
|
from contextlib import AsyncExitStack, contextmanager
|
2024-04-16 00:33:48 +08:00
|
|
|
|
from typing import ( # noqa: UP035
|
2022-02-06 14:52:50 +08:00
|
|
|
|
TYPE_CHECKING,
|
|
|
|
|
Any,
|
|
|
|
|
Type,
|
|
|
|
|
Union,
|
|
|
|
|
TypeVar,
|
|
|
|
|
Callable,
|
2023-03-16 15:51:48 +08:00
|
|
|
|
ClassVar,
|
2022-02-06 14:52:50 +08:00
|
|
|
|
NoReturn,
|
|
|
|
|
Optional,
|
2022-08-14 19:41:00 +08:00
|
|
|
|
overload,
|
2022-02-06 14:52:50 +08:00
|
|
|
|
)
|
|
|
|
|
|
2024-10-26 15:36:01 +08:00
|
|
|
|
from exceptiongroup import BaseExceptionGroup, catch
|
|
|
|
|
|
2022-02-06 14:52:50 +08:00
|
|
|
|
from nonebot.log import logger
|
2022-11-21 18:44:55 +08:00
|
|
|
|
from nonebot.internal.rule import Rule
|
2023-09-09 20:58:50 +08:00
|
|
|
|
from nonebot.dependencies import Param, Dependent
|
2023-03-19 15:45:32 +08:00
|
|
|
|
from nonebot.internal.permission import User, Permission
|
2024-10-26 15:36:01 +08:00
|
|
|
|
from nonebot.utils import classproperty, flatten_exception_group
|
2022-11-21 18:44:55 +08:00
|
|
|
|
from nonebot.internal.adapter import (
|
|
|
|
|
Bot,
|
|
|
|
|
Event,
|
|
|
|
|
Message,
|
|
|
|
|
MessageSegment,
|
|
|
|
|
MessageTemplate,
|
|
|
|
|
)
|
2022-02-06 14:52:50 +08:00
|
|
|
|
from nonebot.typing import (
|
|
|
|
|
T_State,
|
|
|
|
|
T_Handler,
|
|
|
|
|
T_TypeUpdater,
|
|
|
|
|
T_DependencyCache,
|
|
|
|
|
T_PermissionUpdater,
|
|
|
|
|
)
|
2023-06-24 14:47:35 +08:00
|
|
|
|
from nonebot.consts import (
|
|
|
|
|
ARG_KEY,
|
|
|
|
|
RECEIVE_KEY,
|
|
|
|
|
REJECT_TARGET,
|
|
|
|
|
LAST_RECEIVE_KEY,
|
|
|
|
|
REJECT_CACHE_TARGET,
|
|
|
|
|
)
|
2022-02-06 14:52:50 +08:00
|
|
|
|
from nonebot.exception import (
|
|
|
|
|
PausedException,
|
|
|
|
|
StopPropagation,
|
|
|
|
|
SkippedException,
|
|
|
|
|
FinishedException,
|
|
|
|
|
RejectedException,
|
|
|
|
|
)
|
2022-11-21 18:44:55 +08:00
|
|
|
|
from nonebot.internal.params import (
|
2022-02-06 14:52:50 +08:00
|
|
|
|
Depends,
|
|
|
|
|
ArgParam,
|
|
|
|
|
BotParam,
|
|
|
|
|
EventParam,
|
|
|
|
|
StateParam,
|
|
|
|
|
DependParam,
|
|
|
|
|
DefaultParam,
|
|
|
|
|
MatcherParam,
|
|
|
|
|
)
|
|
|
|
|
|
2022-11-21 18:44:55 +08:00
|
|
|
|
from . import matchers
|
|
|
|
|
|
2022-02-06 14:52:50 +08:00
|
|
|
|
if TYPE_CHECKING:
|
|
|
|
|
from nonebot.plugin import Plugin
|
|
|
|
|
|
|
|
|
|
T = TypeVar("T")
|
|
|
|
|
|
|
|
|
|
current_bot: ContextVar[Bot] = ContextVar("current_bot")
|
|
|
|
|
current_event: ContextVar[Event] = ContextVar("current_event")
|
|
|
|
|
current_matcher: ContextVar["Matcher"] = ContextVar("current_matcher")
|
2024-08-06 14:19:17 +08:00
|
|
|
|
current_handler: ContextVar[Dependent[Any]] = ContextVar("current_handler")
|
2022-02-06 14:52:50 +08:00
|
|
|
|
|
|
|
|
|
|
2023-09-09 13:46:09 +08:00
|
|
|
|
@dataclass
|
|
|
|
|
class MatcherSource:
|
|
|
|
|
"""Matcher 源代码上下文信息"""
|
|
|
|
|
|
2024-04-20 14:47:12 +08:00
|
|
|
|
plugin_id: Optional[str] = None
|
|
|
|
|
"""事件响应器所在插件标识符"""
|
2023-09-09 13:46:09 +08:00
|
|
|
|
module_name: Optional[str] = None
|
|
|
|
|
"""事件响应器所在插件模块的路径名"""
|
|
|
|
|
lineno: Optional[int] = None
|
|
|
|
|
"""事件响应器所在行号"""
|
|
|
|
|
|
|
|
|
|
@property
|
|
|
|
|
def plugin(self) -> Optional["Plugin"]:
|
|
|
|
|
"""事件响应器所在插件"""
|
|
|
|
|
from nonebot.plugin import get_plugin
|
|
|
|
|
|
2024-04-20 14:47:12 +08:00
|
|
|
|
if self.plugin_id is not None:
|
|
|
|
|
return get_plugin(self.plugin_id)
|
|
|
|
|
|
|
|
|
|
@property
|
|
|
|
|
def plugin_name(self) -> Optional[str]:
|
|
|
|
|
"""事件响应器所在插件名"""
|
|
|
|
|
return self.plugin and self.plugin.name
|
2023-09-09 13:46:09 +08:00
|
|
|
|
|
|
|
|
|
@property
|
|
|
|
|
def module(self) -> Optional[ModuleType]:
|
|
|
|
|
if self.module_name is not None:
|
|
|
|
|
return sys.modules.get(self.module_name)
|
|
|
|
|
|
|
|
|
|
@property
|
|
|
|
|
def file(self) -> Optional[Path]:
|
|
|
|
|
if self.module is not None and (file := inspect.getsourcefile(self.module)):
|
|
|
|
|
return Path(file).absolute()
|
|
|
|
|
|
|
|
|
|
|
2022-02-06 14:52:50 +08:00
|
|
|
|
class MatcherMeta(type):
|
|
|
|
|
if TYPE_CHECKING:
|
|
|
|
|
type: str
|
2023-09-09 13:46:09 +08:00
|
|
|
|
_source: Optional[MatcherSource]
|
|
|
|
|
module_name: Optional[str]
|
2022-02-06 14:52:50 +08:00
|
|
|
|
|
|
|
|
|
def __repr__(self) -> str:
|
|
|
|
|
return (
|
2023-03-17 15:48:48 +08:00
|
|
|
|
f"{self.__name__}(type={self.type!r}"
|
2022-09-09 11:52:57 +08:00
|
|
|
|
+ (f", module={self.module_name}" if self.module_name else "")
|
2023-09-09 13:46:09 +08:00
|
|
|
|
+ (
|
|
|
|
|
f", lineno={self._source.lineno}"
|
|
|
|
|
if self._source and self._source.lineno is not None
|
|
|
|
|
else ""
|
|
|
|
|
)
|
2022-09-09 11:52:57 +08:00
|
|
|
|
+ ")"
|
2022-02-06 14:52:50 +08:00
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class Matcher(metaclass=MatcherMeta):
|
|
|
|
|
"""事件响应器类"""
|
|
|
|
|
|
2023-09-09 13:46:09 +08:00
|
|
|
|
_source: ClassVar[Optional[MatcherSource]] = None
|
2022-02-06 14:52:50 +08:00
|
|
|
|
|
2023-03-16 15:51:48 +08:00
|
|
|
|
type: ClassVar[str] = ""
|
2022-02-06 14:52:50 +08:00
|
|
|
|
"""事件响应器类型"""
|
2023-03-16 15:51:48 +08:00
|
|
|
|
rule: ClassVar[Rule] = Rule()
|
2022-02-06 14:52:50 +08:00
|
|
|
|
"""事件响应器匹配规则"""
|
2023-03-16 15:51:48 +08:00
|
|
|
|
permission: ClassVar[Permission] = Permission()
|
2022-02-06 14:52:50 +08:00
|
|
|
|
"""事件响应器触发权限"""
|
2024-04-16 00:33:48 +08:00
|
|
|
|
handlers: ClassVar[list[Dependent[Any]]] = []
|
2022-02-06 14:52:50 +08:00
|
|
|
|
"""事件响应器拥有的事件处理函数列表"""
|
2023-03-16 15:51:48 +08:00
|
|
|
|
priority: ClassVar[int] = 1
|
2022-02-06 14:52:50 +08:00
|
|
|
|
"""事件响应器优先级"""
|
|
|
|
|
block: bool = False
|
|
|
|
|
"""事件响应器是否阻止事件传播"""
|
2023-03-16 15:51:48 +08:00
|
|
|
|
temp: ClassVar[bool] = False
|
2022-02-06 14:52:50 +08:00
|
|
|
|
"""事件响应器是否为临时"""
|
2023-03-16 15:51:48 +08:00
|
|
|
|
expire_time: ClassVar[Optional[datetime]] = None
|
2022-02-06 14:52:50 +08:00
|
|
|
|
"""事件响应器过期时间点"""
|
|
|
|
|
|
2023-03-16 15:51:48 +08:00
|
|
|
|
_default_state: ClassVar[T_State] = {}
|
2022-02-06 14:52:50 +08:00
|
|
|
|
"""事件响应器默认状态"""
|
|
|
|
|
|
2023-03-16 15:51:48 +08:00
|
|
|
|
_default_type_updater: ClassVar[Optional[Dependent[str]]] = None
|
2022-02-06 14:52:50 +08:00
|
|
|
|
"""事件响应器类型更新函数"""
|
2023-03-16 15:51:48 +08:00
|
|
|
|
_default_permission_updater: ClassVar[Optional[Dependent[Permission]]] = None
|
2022-02-06 14:52:50 +08:00
|
|
|
|
"""事件响应器权限更新函数"""
|
|
|
|
|
|
2024-04-16 00:33:48 +08:00
|
|
|
|
HANDLER_PARAM_TYPES: ClassVar[tuple[Type[Param], ...]] = ( # noqa: UP006
|
2022-02-06 14:52:50 +08:00
|
|
|
|
DependParam,
|
|
|
|
|
BotParam,
|
|
|
|
|
EventParam,
|
|
|
|
|
StateParam,
|
|
|
|
|
ArgParam,
|
|
|
|
|
MatcherParam,
|
|
|
|
|
DefaultParam,
|
2022-09-07 09:59:05 +08:00
|
|
|
|
)
|
2022-02-06 14:52:50 +08:00
|
|
|
|
|
|
|
|
|
def __init__(self):
|
2024-04-16 00:33:48 +08:00
|
|
|
|
self.remain_handlers: list[Dependent[Any]] = self.handlers.copy()
|
2022-02-06 14:52:50 +08:00
|
|
|
|
self.state = self._default_state.copy()
|
|
|
|
|
|
|
|
|
|
def __repr__(self) -> str:
|
|
|
|
|
return (
|
2023-03-17 15:48:48 +08:00
|
|
|
|
f"{self.__class__.__name__}(type={self.type!r}"
|
2022-09-09 11:52:57 +08:00
|
|
|
|
+ (f", module={self.module_name}" if self.module_name else "")
|
2023-09-09 13:46:09 +08:00
|
|
|
|
+ (
|
|
|
|
|
f", lineno={self._source.lineno}"
|
|
|
|
|
if self._source and self._source.lineno is not None
|
|
|
|
|
else ""
|
|
|
|
|
)
|
2022-09-09 11:52:57 +08:00
|
|
|
|
+ ")"
|
2022-02-06 14:52:50 +08:00
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
|
def new(
|
|
|
|
|
cls,
|
|
|
|
|
type_: str = "",
|
|
|
|
|
rule: Optional[Rule] = None,
|
|
|
|
|
permission: Optional[Permission] = None,
|
2024-04-16 00:33:48 +08:00
|
|
|
|
handlers: Optional[list[Union[T_Handler, Dependent[Any]]]] = None,
|
2022-02-06 14:52:50 +08:00
|
|
|
|
temp: bool = False,
|
|
|
|
|
priority: int = 1,
|
|
|
|
|
block: bool = False,
|
|
|
|
|
*,
|
|
|
|
|
plugin: Optional["Plugin"] = None,
|
|
|
|
|
module: Optional[ModuleType] = None,
|
2023-09-09 13:46:09 +08:00
|
|
|
|
source: Optional[MatcherSource] = None,
|
2022-07-20 10:21:31 +08:00
|
|
|
|
expire_time: Optional[Union[datetime, timedelta]] = None,
|
2022-02-06 14:52:50 +08:00
|
|
|
|
default_state: Optional[T_State] = None,
|
|
|
|
|
default_type_updater: Optional[Union[T_TypeUpdater, Dependent[str]]] = None,
|
|
|
|
|
default_permission_updater: Optional[
|
|
|
|
|
Union[T_PermissionUpdater, Dependent[Permission]]
|
|
|
|
|
] = None,
|
2024-04-16 00:33:48 +08:00
|
|
|
|
) -> Type[Self]: # noqa: UP006
|
2022-02-06 14:52:50 +08:00
|
|
|
|
"""
|
|
|
|
|
创建一个新的事件响应器,并存储至 `matchers <#matchers>`_
|
|
|
|
|
|
|
|
|
|
参数:
|
|
|
|
|
type_: 事件响应器类型,与 `event.get_type()` 一致时触发,空字符串表示任意
|
|
|
|
|
rule: 匹配规则
|
|
|
|
|
permission: 权限
|
|
|
|
|
handlers: 事件处理函数列表
|
|
|
|
|
temp: 是否为临时事件响应器,即触发一次后删除
|
|
|
|
|
priority: 响应优先级
|
|
|
|
|
block: 是否阻止事件向更低优先级的响应器传播
|
2023-09-09 13:46:09 +08:00
|
|
|
|
plugin: **Deprecated.** 事件响应器所在插件
|
|
|
|
|
module: **Deprecated.** 事件响应器所在模块
|
|
|
|
|
source: 事件响应器源代码上下文信息
|
2022-02-06 14:52:50 +08:00
|
|
|
|
expire_time: 事件响应器最终有效时间点,过时即被删除
|
2023-09-09 13:46:09 +08:00
|
|
|
|
default_state: 默认状态 `state`
|
|
|
|
|
default_type_updater: 默认事件类型更新函数
|
|
|
|
|
default_permission_updater: 默认会话权限更新函数
|
2022-02-06 14:52:50 +08:00
|
|
|
|
|
|
|
|
|
返回:
|
|
|
|
|
Type[Matcher]: 新的事件响应器类
|
|
|
|
|
"""
|
2023-09-09 13:46:09 +08:00
|
|
|
|
if plugin is not None:
|
|
|
|
|
warnings.warn(
|
|
|
|
|
(
|
|
|
|
|
"Pass `plugin` context info to create Matcher is deprecated. "
|
|
|
|
|
"Use `source` instead."
|
|
|
|
|
),
|
|
|
|
|
DeprecationWarning,
|
|
|
|
|
)
|
|
|
|
|
if module is not None:
|
|
|
|
|
warnings.warn(
|
|
|
|
|
(
|
|
|
|
|
"Pass `module` context info to create Matcher is deprecated. "
|
|
|
|
|
"Use `source` instead."
|
|
|
|
|
),
|
|
|
|
|
DeprecationWarning,
|
|
|
|
|
)
|
|
|
|
|
source = source or (
|
|
|
|
|
MatcherSource(
|
2024-04-20 14:47:12 +08:00
|
|
|
|
plugin_id=plugin and plugin.id_,
|
2023-09-09 13:46:09 +08:00
|
|
|
|
module_name=module and module.__name__,
|
|
|
|
|
)
|
|
|
|
|
if plugin is not None or module is not None
|
|
|
|
|
else None
|
|
|
|
|
)
|
|
|
|
|
|
2022-02-06 14:52:50 +08:00
|
|
|
|
NewMatcher = type(
|
2023-03-17 15:48:48 +08:00
|
|
|
|
cls.__name__,
|
|
|
|
|
(cls,),
|
2022-02-06 14:52:50 +08:00
|
|
|
|
{
|
2023-09-09 13:46:09 +08:00
|
|
|
|
"_source": source,
|
2022-02-06 14:52:50 +08:00
|
|
|
|
"type": type_,
|
|
|
|
|
"rule": rule or Rule(),
|
|
|
|
|
"permission": permission or Permission(),
|
2024-02-06 12:48:23 +08:00
|
|
|
|
"handlers": (
|
|
|
|
|
[
|
|
|
|
|
(
|
|
|
|
|
handler
|
|
|
|
|
if isinstance(handler, Dependent)
|
|
|
|
|
else Dependent[Any].parse(
|
|
|
|
|
call=handler, allow_types=cls.HANDLER_PARAM_TYPES
|
|
|
|
|
)
|
|
|
|
|
)
|
|
|
|
|
for handler in handlers
|
|
|
|
|
]
|
|
|
|
|
if handlers
|
|
|
|
|
else []
|
|
|
|
|
),
|
2022-02-06 14:52:50 +08:00
|
|
|
|
"temp": temp,
|
2022-07-20 10:21:31 +08:00
|
|
|
|
"expire_time": (
|
|
|
|
|
expire_time
|
2022-09-07 09:59:05 +08:00
|
|
|
|
and (
|
|
|
|
|
expire_time
|
|
|
|
|
if isinstance(expire_time, datetime)
|
|
|
|
|
else datetime.now() + expire_time
|
|
|
|
|
)
|
2022-07-20 10:21:31 +08:00
|
|
|
|
),
|
2022-02-06 14:52:50 +08:00
|
|
|
|
"priority": priority,
|
|
|
|
|
"block": block,
|
|
|
|
|
"_default_state": default_state or {},
|
|
|
|
|
"_default_type_updater": (
|
|
|
|
|
default_type_updater
|
2022-09-07 09:59:05 +08:00
|
|
|
|
and (
|
|
|
|
|
default_type_updater
|
|
|
|
|
if isinstance(default_type_updater, Dependent)
|
|
|
|
|
else Dependent[str].parse(
|
|
|
|
|
call=default_type_updater,
|
|
|
|
|
allow_types=cls.HANDLER_PARAM_TYPES,
|
|
|
|
|
)
|
2022-02-06 14:52:50 +08:00
|
|
|
|
)
|
|
|
|
|
),
|
|
|
|
|
"_default_permission_updater": (
|
|
|
|
|
default_permission_updater
|
2022-09-07 09:59:05 +08:00
|
|
|
|
and (
|
|
|
|
|
default_permission_updater
|
|
|
|
|
if isinstance(default_permission_updater, Dependent)
|
|
|
|
|
else Dependent[Permission].parse(
|
|
|
|
|
call=default_permission_updater,
|
|
|
|
|
allow_types=cls.HANDLER_PARAM_TYPES,
|
|
|
|
|
)
|
2022-02-06 14:52:50 +08:00
|
|
|
|
)
|
|
|
|
|
),
|
|
|
|
|
},
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
logger.trace(f"Define new matcher {NewMatcher}")
|
|
|
|
|
|
|
|
|
|
matchers[priority].append(NewMatcher)
|
|
|
|
|
|
2023-12-08 15:03:59 +08:00
|
|
|
|
return NewMatcher # type: ignore
|
2022-02-06 14:52:50 +08:00
|
|
|
|
|
2022-12-06 14:19:48 +08:00
|
|
|
|
@classmethod
|
|
|
|
|
def destroy(cls) -> None:
|
|
|
|
|
"""销毁当前的事件响应器"""
|
|
|
|
|
matchers[cls.priority].remove(cls)
|
|
|
|
|
|
2023-09-09 13:46:09 +08:00
|
|
|
|
@classproperty
|
|
|
|
|
def plugin(cls) -> Optional["Plugin"]:
|
|
|
|
|
"""事件响应器所在插件"""
|
|
|
|
|
return cls._source and cls._source.plugin
|
|
|
|
|
|
|
|
|
|
@classproperty
|
2024-04-20 14:47:12 +08:00
|
|
|
|
def plugin_id(cls) -> Optional[str]:
|
|
|
|
|
"""事件响应器所在插件标识符"""
|
|
|
|
|
return cls._source and cls._source.plugin_id
|
2023-09-09 13:46:09 +08:00
|
|
|
|
|
|
|
|
|
@classproperty
|
|
|
|
|
def plugin_name(cls) -> Optional[str]:
|
|
|
|
|
"""事件响应器所在插件名"""
|
|
|
|
|
return cls._source and cls._source.plugin_name
|
|
|
|
|
|
2024-04-20 14:47:12 +08:00
|
|
|
|
@classproperty
|
|
|
|
|
def module(cls) -> Optional[ModuleType]:
|
|
|
|
|
"""事件响应器所在插件模块"""
|
|
|
|
|
return cls._source and cls._source.module
|
|
|
|
|
|
2023-09-09 13:46:09 +08:00
|
|
|
|
@classproperty
|
|
|
|
|
def module_name(cls) -> Optional[str]:
|
|
|
|
|
"""事件响应器所在插件模块路径"""
|
|
|
|
|
return cls._source and cls._source.module_name
|
|
|
|
|
|
2022-02-06 14:52:50 +08:00
|
|
|
|
@classmethod
|
|
|
|
|
async def check_perm(
|
|
|
|
|
cls,
|
|
|
|
|
bot: Bot,
|
|
|
|
|
event: Event,
|
|
|
|
|
stack: Optional[AsyncExitStack] = None,
|
|
|
|
|
dependency_cache: Optional[T_DependencyCache] = None,
|
|
|
|
|
) -> bool:
|
|
|
|
|
"""检查是否满足触发权限
|
|
|
|
|
|
|
|
|
|
参数:
|
|
|
|
|
bot: Bot 对象
|
|
|
|
|
event: 上报事件
|
|
|
|
|
stack: 异步上下文栈
|
|
|
|
|
dependency_cache: 依赖缓存
|
|
|
|
|
|
|
|
|
|
返回:
|
|
|
|
|
是否满足权限
|
|
|
|
|
"""
|
|
|
|
|
event_type = event.get_type()
|
|
|
|
|
return event_type == (cls.type or event_type) and await cls.permission(
|
|
|
|
|
bot, event, stack, dependency_cache
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
|
async def check_rule(
|
|
|
|
|
cls,
|
|
|
|
|
bot: Bot,
|
|
|
|
|
event: Event,
|
|
|
|
|
state: T_State,
|
|
|
|
|
stack: Optional[AsyncExitStack] = None,
|
|
|
|
|
dependency_cache: Optional[T_DependencyCache] = None,
|
|
|
|
|
) -> bool:
|
|
|
|
|
"""检查是否满足匹配规则
|
|
|
|
|
|
|
|
|
|
参数:
|
|
|
|
|
bot: Bot 对象
|
|
|
|
|
event: 上报事件
|
|
|
|
|
state: 当前状态
|
|
|
|
|
stack: 异步上下文栈
|
|
|
|
|
dependency_cache: 依赖缓存
|
|
|
|
|
|
|
|
|
|
返回:
|
|
|
|
|
是否满足匹配规则
|
|
|
|
|
"""
|
|
|
|
|
event_type = event.get_type()
|
|
|
|
|
return event_type == (cls.type or event_type) and await cls.rule(
|
|
|
|
|
bot, event, state, stack, dependency_cache
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
|
def type_updater(cls, func: T_TypeUpdater) -> T_TypeUpdater:
|
|
|
|
|
"""装饰一个函数来更改当前事件响应器的默认响应事件类型更新函数
|
|
|
|
|
|
|
|
|
|
参数:
|
|
|
|
|
func: 响应事件类型更新函数
|
|
|
|
|
"""
|
|
|
|
|
cls._default_type_updater = Dependent[str].parse(
|
|
|
|
|
call=func, allow_types=cls.HANDLER_PARAM_TYPES
|
|
|
|
|
)
|
|
|
|
|
return func
|
|
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
|
def permission_updater(cls, func: T_PermissionUpdater) -> T_PermissionUpdater:
|
|
|
|
|
"""装饰一个函数来更改当前事件响应器的默认会话权限更新函数
|
|
|
|
|
|
|
|
|
|
参数:
|
|
|
|
|
func: 会话权限更新函数
|
|
|
|
|
"""
|
|
|
|
|
cls._default_permission_updater = Dependent[Permission].parse(
|
|
|
|
|
call=func, allow_types=cls.HANDLER_PARAM_TYPES
|
|
|
|
|
)
|
|
|
|
|
return func
|
|
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
|
def append_handler(
|
2022-09-07 09:59:05 +08:00
|
|
|
|
cls, handler: T_Handler, parameterless: Optional[Iterable[Any]] = None
|
2022-02-06 14:52:50 +08:00
|
|
|
|
) -> Dependent[Any]:
|
|
|
|
|
handler_ = Dependent[Any].parse(
|
|
|
|
|
call=handler,
|
|
|
|
|
parameterless=parameterless,
|
|
|
|
|
allow_types=cls.HANDLER_PARAM_TYPES,
|
|
|
|
|
)
|
|
|
|
|
cls.handlers.append(handler_)
|
|
|
|
|
return handler_
|
|
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
|
def handle(
|
2022-09-07 09:59:05 +08:00
|
|
|
|
cls, parameterless: Optional[Iterable[Any]] = None
|
2022-02-06 14:52:50 +08:00
|
|
|
|
) -> Callable[[T_Handler], T_Handler]:
|
|
|
|
|
"""装饰一个函数来向事件响应器直接添加一个处理函数
|
|
|
|
|
|
|
|
|
|
参数:
|
|
|
|
|
parameterless: 非参数类型依赖列表
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
def _decorator(func: T_Handler) -> T_Handler:
|
|
|
|
|
cls.append_handler(func, parameterless=parameterless)
|
|
|
|
|
return func
|
|
|
|
|
|
|
|
|
|
return _decorator
|
|
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
|
def receive(
|
2022-09-07 09:59:05 +08:00
|
|
|
|
cls, id: str = "", parameterless: Optional[Iterable[Any]] = None
|
2022-02-06 14:52:50 +08:00
|
|
|
|
) -> Callable[[T_Handler], T_Handler]:
|
|
|
|
|
"""装饰一个函数来指示 NoneBot 在接收用户新的一条消息后继续运行该函数
|
|
|
|
|
|
|
|
|
|
参数:
|
|
|
|
|
id: 消息 ID
|
|
|
|
|
parameterless: 非参数类型依赖列表
|
|
|
|
|
"""
|
|
|
|
|
|
2024-03-07 14:57:26 +08:00
|
|
|
|
async def _receive(event: Event, matcher: "Matcher") -> None:
|
2022-02-06 14:52:50 +08:00
|
|
|
|
matcher.set_target(RECEIVE_KEY.format(id=id))
|
|
|
|
|
if matcher.get_target() == RECEIVE_KEY.format(id=id):
|
|
|
|
|
matcher.set_receive(id, event)
|
|
|
|
|
return
|
|
|
|
|
if matcher.get_receive(id, ...) is not ...:
|
|
|
|
|
return
|
|
|
|
|
await matcher.reject()
|
|
|
|
|
|
2023-06-24 14:47:35 +08:00
|
|
|
|
_parameterless = (Depends(_receive), *(parameterless or ()))
|
2022-02-06 14:52:50 +08:00
|
|
|
|
|
|
|
|
|
def _decorator(func: T_Handler) -> T_Handler:
|
|
|
|
|
if cls.handlers and cls.handlers[-1].call is func:
|
|
|
|
|
func_handler = cls.handlers[-1]
|
2022-09-07 09:59:05 +08:00
|
|
|
|
new_handler = Dependent(
|
|
|
|
|
call=func_handler.call,
|
|
|
|
|
params=func_handler.params,
|
|
|
|
|
parameterless=Dependent.parse_parameterless(
|
|
|
|
|
tuple(_parameterless), cls.HANDLER_PARAM_TYPES
|
|
|
|
|
)
|
|
|
|
|
+ func_handler.parameterless,
|
|
|
|
|
)
|
|
|
|
|
cls.handlers[-1] = new_handler
|
2022-02-06 14:52:50 +08:00
|
|
|
|
else:
|
|
|
|
|
cls.append_handler(func, parameterless=_parameterless)
|
|
|
|
|
|
|
|
|
|
return func
|
|
|
|
|
|
|
|
|
|
return _decorator
|
|
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
|
def got(
|
|
|
|
|
cls,
|
|
|
|
|
key: str,
|
|
|
|
|
prompt: Optional[Union[str, Message, MessageSegment, MessageTemplate]] = None,
|
2022-09-07 09:59:05 +08:00
|
|
|
|
parameterless: Optional[Iterable[Any]] = None,
|
2022-02-06 14:52:50 +08:00
|
|
|
|
) -> Callable[[T_Handler], T_Handler]:
|
|
|
|
|
"""装饰一个函数来指示 NoneBot 获取一个参数 `key`
|
|
|
|
|
|
2023-06-24 14:47:35 +08:00
|
|
|
|
当要获取的 `key` 不存在时接收用户新的一条消息再运行该函数,
|
|
|
|
|
如果 `key` 已存在则直接继续运行
|
2022-02-06 14:52:50 +08:00
|
|
|
|
|
|
|
|
|
参数:
|
|
|
|
|
key: 参数名
|
|
|
|
|
prompt: 在参数不存在时向用户发送的消息
|
|
|
|
|
parameterless: 非参数类型依赖列表
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
async def _key_getter(event: Event, matcher: "Matcher"):
|
|
|
|
|
matcher.set_target(ARG_KEY.format(key=key))
|
|
|
|
|
if matcher.get_target() == ARG_KEY.format(key=key):
|
|
|
|
|
matcher.set_arg(key, event.get_message())
|
|
|
|
|
return
|
|
|
|
|
if matcher.get_arg(key, ...) is not ...:
|
|
|
|
|
return
|
|
|
|
|
await matcher.reject(prompt)
|
|
|
|
|
|
2023-06-24 14:47:35 +08:00
|
|
|
|
_parameterless = (Depends(_key_getter), *(parameterless or ()))
|
2022-02-06 14:52:50 +08:00
|
|
|
|
|
|
|
|
|
def _decorator(func: T_Handler) -> T_Handler:
|
|
|
|
|
if cls.handlers and cls.handlers[-1].call is func:
|
|
|
|
|
func_handler = cls.handlers[-1]
|
2022-09-07 09:59:05 +08:00
|
|
|
|
new_handler = Dependent(
|
|
|
|
|
call=func_handler.call,
|
|
|
|
|
params=func_handler.params,
|
|
|
|
|
parameterless=Dependent.parse_parameterless(
|
|
|
|
|
tuple(_parameterless), cls.HANDLER_PARAM_TYPES
|
|
|
|
|
)
|
|
|
|
|
+ func_handler.parameterless,
|
|
|
|
|
)
|
|
|
|
|
cls.handlers[-1] = new_handler
|
2022-02-06 14:52:50 +08:00
|
|
|
|
else:
|
|
|
|
|
cls.append_handler(func, parameterless=_parameterless)
|
|
|
|
|
|
|
|
|
|
return func
|
|
|
|
|
|
|
|
|
|
return _decorator
|
|
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
|
async def send(
|
|
|
|
|
cls,
|
|
|
|
|
message: Union[str, Message, MessageSegment, MessageTemplate],
|
|
|
|
|
**kwargs: Any,
|
|
|
|
|
) -> Any:
|
|
|
|
|
"""发送一条消息给当前交互用户
|
|
|
|
|
|
|
|
|
|
参数:
|
|
|
|
|
message: 消息内容
|
2023-06-24 14:47:35 +08:00
|
|
|
|
kwargs: {ref}`nonebot.adapters.Bot.send` 的参数,
|
|
|
|
|
请参考对应 adapter 的 bot 对象 api
|
2022-02-06 14:52:50 +08:00
|
|
|
|
"""
|
|
|
|
|
bot = current_bot.get()
|
|
|
|
|
event = current_event.get()
|
|
|
|
|
state = current_matcher.get().state
|
|
|
|
|
if isinstance(message, MessageTemplate):
|
|
|
|
|
_message = message.format(**state)
|
|
|
|
|
else:
|
|
|
|
|
_message = message
|
|
|
|
|
return await bot.send(event=event, message=_message, **kwargs)
|
|
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
|
async def finish(
|
|
|
|
|
cls,
|
|
|
|
|
message: Optional[Union[str, Message, MessageSegment, MessageTemplate]] = None,
|
|
|
|
|
**kwargs,
|
|
|
|
|
) -> NoReturn:
|
|
|
|
|
"""发送一条消息给当前交互用户并结束当前事件响应器
|
|
|
|
|
|
|
|
|
|
参数:
|
|
|
|
|
message: 消息内容
|
2023-06-24 14:47:35 +08:00
|
|
|
|
kwargs: {ref}`nonebot.adapters.Bot.send` 的参数,
|
|
|
|
|
请参考对应 adapter 的 bot 对象 api
|
2022-02-06 14:52:50 +08:00
|
|
|
|
"""
|
|
|
|
|
if message is not None:
|
|
|
|
|
await cls.send(message, **kwargs)
|
|
|
|
|
raise FinishedException
|
|
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
|
async def pause(
|
|
|
|
|
cls,
|
|
|
|
|
prompt: Optional[Union[str, Message, MessageSegment, MessageTemplate]] = None,
|
|
|
|
|
**kwargs,
|
|
|
|
|
) -> NoReturn:
|
|
|
|
|
"""发送一条消息给当前交互用户并暂停事件响应器,在接收用户新的一条消息后继续下一个处理函数
|
|
|
|
|
|
|
|
|
|
参数:
|
|
|
|
|
prompt: 消息内容
|
2023-06-24 14:47:35 +08:00
|
|
|
|
kwargs: {ref}`nonebot.adapters.Bot.send` 的参数,
|
|
|
|
|
请参考对应 adapter 的 bot 对象 api
|
2022-02-06 14:52:50 +08:00
|
|
|
|
"""
|
|
|
|
|
if prompt is not None:
|
|
|
|
|
await cls.send(prompt, **kwargs)
|
|
|
|
|
raise PausedException
|
|
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
|
async def reject(
|
|
|
|
|
cls,
|
|
|
|
|
prompt: Optional[Union[str, Message, MessageSegment, MessageTemplate]] = None,
|
|
|
|
|
**kwargs,
|
|
|
|
|
) -> NoReturn:
|
|
|
|
|
"""最近使用 `got` / `receive` 接收的消息不符合预期,
|
|
|
|
|
发送一条消息给当前交互用户并将当前事件处理流程中断在当前位置,在接收用户新的一个事件后从头开始执行当前处理函数
|
|
|
|
|
|
|
|
|
|
参数:
|
|
|
|
|
prompt: 消息内容
|
2023-06-24 14:47:35 +08:00
|
|
|
|
kwargs: {ref}`nonebot.adapters.Bot.send` 的参数,
|
|
|
|
|
请参考对应 adapter 的 bot 对象 api
|
2022-02-06 14:52:50 +08:00
|
|
|
|
"""
|
|
|
|
|
if prompt is not None:
|
|
|
|
|
await cls.send(prompt, **kwargs)
|
|
|
|
|
raise RejectedException
|
|
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
|
async def reject_arg(
|
|
|
|
|
cls,
|
|
|
|
|
key: str,
|
|
|
|
|
prompt: Optional[Union[str, Message, MessageSegment, MessageTemplate]] = None,
|
|
|
|
|
**kwargs,
|
|
|
|
|
) -> NoReturn:
|
|
|
|
|
"""最近使用 `got` 接收的消息不符合预期,
|
|
|
|
|
发送一条消息给当前交互用户并将当前事件处理流程中断在当前位置,在接收用户新的一条消息后从头开始执行当前处理函数
|
|
|
|
|
|
|
|
|
|
参数:
|
|
|
|
|
key: 参数名
|
|
|
|
|
prompt: 消息内容
|
2023-06-24 14:47:35 +08:00
|
|
|
|
kwargs: {ref}`nonebot.adapters.Bot.send` 的参数,
|
|
|
|
|
请参考对应 adapter 的 bot 对象 api
|
2022-02-06 14:52:50 +08:00
|
|
|
|
"""
|
|
|
|
|
matcher = current_matcher.get()
|
|
|
|
|
matcher.set_target(ARG_KEY.format(key=key))
|
|
|
|
|
if prompt is not None:
|
|
|
|
|
await cls.send(prompt, **kwargs)
|
|
|
|
|
raise RejectedException
|
|
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
|
async def reject_receive(
|
|
|
|
|
cls,
|
|
|
|
|
id: str = "",
|
|
|
|
|
prompt: Optional[Union[str, Message, MessageSegment, MessageTemplate]] = None,
|
|
|
|
|
**kwargs,
|
|
|
|
|
) -> NoReturn:
|
|
|
|
|
"""最近使用 `receive` 接收的消息不符合预期,
|
|
|
|
|
发送一条消息给当前交互用户并将当前事件处理流程中断在当前位置,在接收用户新的一个事件后从头开始执行当前处理函数
|
|
|
|
|
|
|
|
|
|
参数:
|
|
|
|
|
id: 消息 id
|
|
|
|
|
prompt: 消息内容
|
2023-06-24 14:47:35 +08:00
|
|
|
|
kwargs: {ref}`nonebot.adapters.Bot.send` 的参数,
|
|
|
|
|
请参考对应 adapter 的 bot 对象 api
|
2022-02-06 14:52:50 +08:00
|
|
|
|
"""
|
|
|
|
|
matcher = current_matcher.get()
|
|
|
|
|
matcher.set_target(RECEIVE_KEY.format(id=id))
|
|
|
|
|
if prompt is not None:
|
|
|
|
|
await cls.send(prompt, **kwargs)
|
|
|
|
|
raise RejectedException
|
|
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
|
def skip(cls) -> NoReturn:
|
|
|
|
|
"""跳过当前事件处理函数,继续下一个处理函数
|
|
|
|
|
|
|
|
|
|
通常在事件处理函数的依赖中使用。
|
|
|
|
|
"""
|
|
|
|
|
raise SkippedException
|
|
|
|
|
|
2022-08-14 19:41:00 +08:00
|
|
|
|
@overload
|
2024-02-06 12:48:23 +08:00
|
|
|
|
def get_receive(self, id: str) -> Union[Event, None]: ...
|
2022-08-14 19:41:00 +08:00
|
|
|
|
|
|
|
|
|
@overload
|
2024-02-06 12:48:23 +08:00
|
|
|
|
def get_receive(self, id: str, default: T) -> Union[Event, T]: ...
|
2022-08-14 19:41:00 +08:00
|
|
|
|
|
|
|
|
|
def get_receive(
|
|
|
|
|
self, id: str, default: Optional[T] = None
|
|
|
|
|
) -> Optional[Union[Event, T]]:
|
2022-02-06 14:52:50 +08:00
|
|
|
|
"""获取一个 `receive` 事件
|
|
|
|
|
|
|
|
|
|
如果没有找到对应的事件,返回 `default` 值
|
|
|
|
|
"""
|
|
|
|
|
return self.state.get(RECEIVE_KEY.format(id=id), default)
|
|
|
|
|
|
|
|
|
|
def set_receive(self, id: str, event: Event) -> None:
|
|
|
|
|
"""设置一个 `receive` 事件"""
|
|
|
|
|
self.state[RECEIVE_KEY.format(id=id)] = event
|
|
|
|
|
self.state[LAST_RECEIVE_KEY] = event
|
|
|
|
|
|
2022-08-14 19:41:00 +08:00
|
|
|
|
@overload
|
2024-02-06 12:48:23 +08:00
|
|
|
|
def get_last_receive(self) -> Union[Event, None]: ...
|
2022-08-14 19:41:00 +08:00
|
|
|
|
|
|
|
|
|
@overload
|
2024-02-06 12:48:23 +08:00
|
|
|
|
def get_last_receive(self, default: T) -> Union[Event, T]: ...
|
2022-08-14 19:41:00 +08:00
|
|
|
|
|
|
|
|
|
def get_last_receive(
|
|
|
|
|
self, default: Optional[T] = None
|
|
|
|
|
) -> Optional[Union[Event, T]]:
|
2022-02-06 14:52:50 +08:00
|
|
|
|
"""获取最近一次 `receive` 事件
|
|
|
|
|
|
|
|
|
|
如果没有事件,返回 `default` 值
|
|
|
|
|
"""
|
|
|
|
|
return self.state.get(LAST_RECEIVE_KEY, default)
|
|
|
|
|
|
2022-08-14 19:41:00 +08:00
|
|
|
|
@overload
|
2024-02-06 12:48:23 +08:00
|
|
|
|
def get_arg(self, key: str) -> Union[Message, None]: ...
|
2022-08-14 19:41:00 +08:00
|
|
|
|
|
|
|
|
|
@overload
|
2024-02-06 12:48:23 +08:00
|
|
|
|
def get_arg(self, key: str, default: T) -> Union[Message, T]: ...
|
2022-08-14 19:41:00 +08:00
|
|
|
|
|
|
|
|
|
def get_arg(
|
|
|
|
|
self, key: str, default: Optional[T] = None
|
|
|
|
|
) -> Optional[Union[Message, T]]:
|
2022-02-06 14:52:50 +08:00
|
|
|
|
"""获取一个 `got` 消息
|
|
|
|
|
|
|
|
|
|
如果没有找到对应的消息,返回 `default` 值
|
|
|
|
|
"""
|
|
|
|
|
return self.state.get(ARG_KEY.format(key=key), default)
|
|
|
|
|
|
|
|
|
|
def set_arg(self, key: str, message: Message) -> None:
|
|
|
|
|
"""设置一个 `got` 消息"""
|
|
|
|
|
self.state[ARG_KEY.format(key=key)] = message
|
|
|
|
|
|
|
|
|
|
def set_target(self, target: str, cache: bool = True) -> None:
|
|
|
|
|
if cache:
|
|
|
|
|
self.state[REJECT_CACHE_TARGET] = target
|
|
|
|
|
else:
|
|
|
|
|
self.state[REJECT_TARGET] = target
|
|
|
|
|
|
2022-08-14 19:41:00 +08:00
|
|
|
|
@overload
|
2024-02-06 12:48:23 +08:00
|
|
|
|
def get_target(self) -> Union[str, None]: ...
|
2022-08-14 19:41:00 +08:00
|
|
|
|
|
|
|
|
|
@overload
|
2024-02-06 12:48:23 +08:00
|
|
|
|
def get_target(self, default: T) -> Union[str, T]: ...
|
2022-08-14 19:41:00 +08:00
|
|
|
|
|
|
|
|
|
def get_target(self, default: Optional[T] = None) -> Optional[Union[str, T]]:
|
2022-02-06 14:52:50 +08:00
|
|
|
|
return self.state.get(REJECT_TARGET, default)
|
|
|
|
|
|
|
|
|
|
def stop_propagation(self):
|
|
|
|
|
"""阻止事件传播"""
|
|
|
|
|
self.block = True
|
|
|
|
|
|
2023-03-16 15:51:48 +08:00
|
|
|
|
async def update_type(
|
|
|
|
|
self,
|
|
|
|
|
bot: Bot,
|
|
|
|
|
event: Event,
|
|
|
|
|
stack: Optional[AsyncExitStack] = None,
|
|
|
|
|
dependency_cache: Optional[T_DependencyCache] = None,
|
|
|
|
|
) -> str:
|
2022-02-06 14:52:50 +08:00
|
|
|
|
updater = self.__class__._default_type_updater
|
2022-09-01 10:41:43 +08:00
|
|
|
|
return (
|
2023-03-16 15:51:48 +08:00
|
|
|
|
await updater(
|
|
|
|
|
bot=bot,
|
|
|
|
|
event=event,
|
|
|
|
|
state=self.state,
|
|
|
|
|
matcher=self,
|
|
|
|
|
stack=stack,
|
|
|
|
|
dependency_cache=dependency_cache,
|
|
|
|
|
)
|
2022-09-01 10:41:43 +08:00
|
|
|
|
if updater
|
|
|
|
|
else "message"
|
|
|
|
|
)
|
2022-02-06 14:52:50 +08:00
|
|
|
|
|
2023-03-16 15:51:48 +08:00
|
|
|
|
async def update_permission(
|
|
|
|
|
self,
|
|
|
|
|
bot: Bot,
|
|
|
|
|
event: Event,
|
|
|
|
|
stack: Optional[AsyncExitStack] = None,
|
|
|
|
|
dependency_cache: Optional[T_DependencyCache] = None,
|
|
|
|
|
) -> Permission:
|
2022-09-01 10:41:43 +08:00
|
|
|
|
if updater := self.__class__._default_permission_updater:
|
2023-03-16 15:51:48 +08:00
|
|
|
|
return await updater(
|
|
|
|
|
bot=bot,
|
|
|
|
|
event=event,
|
|
|
|
|
state=self.state,
|
|
|
|
|
matcher=self,
|
|
|
|
|
stack=stack,
|
|
|
|
|
dependency_cache=dependency_cache,
|
|
|
|
|
)
|
2023-03-19 15:45:32 +08:00
|
|
|
|
return Permission(User.from_event(event, perm=self.permission))
|
2022-02-06 14:52:50 +08:00
|
|
|
|
|
|
|
|
|
async def resolve_reject(self):
|
|
|
|
|
handler = current_handler.get()
|
2024-03-07 14:57:26 +08:00
|
|
|
|
self.remain_handlers.insert(0, handler)
|
2022-02-06 14:52:50 +08:00
|
|
|
|
if REJECT_CACHE_TARGET in self.state:
|
|
|
|
|
self.state[REJECT_TARGET] = self.state[REJECT_CACHE_TARGET]
|
|
|
|
|
|
2022-11-15 10:50:52 +08:00
|
|
|
|
@contextmanager
|
|
|
|
|
def ensure_context(self, bot: Bot, event: Event):
|
|
|
|
|
b_t = current_bot.set(bot)
|
|
|
|
|
e_t = current_event.set(event)
|
|
|
|
|
m_t = current_matcher.set(self)
|
|
|
|
|
try:
|
|
|
|
|
yield
|
|
|
|
|
finally:
|
|
|
|
|
current_bot.reset(b_t)
|
|
|
|
|
current_event.reset(e_t)
|
|
|
|
|
current_matcher.reset(m_t)
|
|
|
|
|
|
2022-02-06 14:52:50 +08:00
|
|
|
|
async def simple_run(
|
|
|
|
|
self,
|
|
|
|
|
bot: Bot,
|
|
|
|
|
event: Event,
|
|
|
|
|
state: T_State,
|
|
|
|
|
stack: Optional[AsyncExitStack] = None,
|
|
|
|
|
dependency_cache: Optional[T_DependencyCache] = None,
|
|
|
|
|
):
|
|
|
|
|
logger.trace(
|
2022-09-09 11:52:57 +08:00
|
|
|
|
f"{self} run with incoming args: "
|
|
|
|
|
f"bot={bot}, event={event!r}, state={state!r}"
|
2022-02-06 14:52:50 +08:00
|
|
|
|
)
|
2022-11-15 10:50:52 +08:00
|
|
|
|
|
2024-10-26 15:36:01 +08:00
|
|
|
|
def _handle_stop_propagation(exc_group: BaseExceptionGroup[StopPropagation]):
|
|
|
|
|
self.block = True
|
|
|
|
|
|
2022-11-15 10:50:52 +08:00
|
|
|
|
with self.ensure_context(bot, event):
|
|
|
|
|
try:
|
2024-10-26 15:36:01 +08:00
|
|
|
|
with catch({StopPropagation: _handle_stop_propagation}):
|
|
|
|
|
# Refresh preprocess state
|
|
|
|
|
self.state.update(state)
|
|
|
|
|
|
|
|
|
|
while self.remain_handlers:
|
|
|
|
|
handler = self.remain_handlers.pop(0)
|
|
|
|
|
current_handler.set(handler)
|
|
|
|
|
logger.debug(f"Running handler {handler}")
|
|
|
|
|
|
|
|
|
|
def _handle_skipped(
|
|
|
|
|
exc_group: BaseExceptionGroup[SkippedException],
|
|
|
|
|
):
|
|
|
|
|
logger.debug(f"Handler {handler} skipped")
|
|
|
|
|
|
|
|
|
|
with catch({SkippedException: _handle_skipped}):
|
|
|
|
|
await handler(
|
|
|
|
|
matcher=self,
|
|
|
|
|
bot=bot,
|
|
|
|
|
event=event,
|
|
|
|
|
state=self.state,
|
|
|
|
|
stack=stack,
|
|
|
|
|
dependency_cache=dependency_cache,
|
|
|
|
|
)
|
2022-11-15 10:50:52 +08:00
|
|
|
|
finally:
|
|
|
|
|
logger.info(f"{self} running complete")
|
2022-02-06 14:52:50 +08:00
|
|
|
|
|
|
|
|
|
# 运行handlers
|
|
|
|
|
async def run(
|
|
|
|
|
self,
|
|
|
|
|
bot: Bot,
|
|
|
|
|
event: Event,
|
|
|
|
|
state: T_State,
|
|
|
|
|
stack: Optional[AsyncExitStack] = None,
|
|
|
|
|
dependency_cache: Optional[T_DependencyCache] = None,
|
|
|
|
|
):
|
2024-10-26 15:36:01 +08:00
|
|
|
|
exc: Optional[Union[FinishedException, RejectedException, PausedException]] = (
|
|
|
|
|
None
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
def _handle_special_exception(
|
|
|
|
|
exc_group: BaseExceptionGroup[
|
|
|
|
|
Union[FinishedException, RejectedException, PausedException]
|
|
|
|
|
]
|
|
|
|
|
):
|
|
|
|
|
nonlocal exc
|
|
|
|
|
excs = list(flatten_exception_group(exc_group))
|
|
|
|
|
if len(excs) > 1:
|
|
|
|
|
logger.warning(
|
|
|
|
|
"Multiple session control exceptions occurred. "
|
|
|
|
|
"NoneBot will choose the proper one."
|
|
|
|
|
)
|
|
|
|
|
finished_exc = next(
|
|
|
|
|
(e for e in excs if isinstance(e, FinishedException)),
|
|
|
|
|
None,
|
|
|
|
|
)
|
|
|
|
|
rejected_exc = next(
|
|
|
|
|
(e for e in excs if isinstance(e, RejectedException)),
|
|
|
|
|
None,
|
|
|
|
|
)
|
|
|
|
|
paused_exc = next(
|
|
|
|
|
(e for e in excs if isinstance(e, PausedException)),
|
|
|
|
|
None,
|
|
|
|
|
)
|
|
|
|
|
exc = finished_exc or rejected_exc or paused_exc
|
|
|
|
|
elif isinstance(
|
|
|
|
|
excs[0], (FinishedException, RejectedException, PausedException)
|
|
|
|
|
):
|
|
|
|
|
exc = excs[0]
|
|
|
|
|
|
|
|
|
|
with catch(
|
|
|
|
|
{
|
|
|
|
|
(
|
|
|
|
|
FinishedException,
|
|
|
|
|
RejectedException,
|
|
|
|
|
PausedException,
|
|
|
|
|
): _handle_special_exception
|
|
|
|
|
}
|
|
|
|
|
):
|
2022-02-06 14:52:50 +08:00
|
|
|
|
await self.simple_run(bot, event, state, stack, dependency_cache)
|
|
|
|
|
|
2024-10-26 15:36:01 +08:00
|
|
|
|
if isinstance(exc, FinishedException):
|
|
|
|
|
pass
|
|
|
|
|
elif isinstance(exc, RejectedException):
|
2022-02-06 14:52:50 +08:00
|
|
|
|
await self.resolve_reject()
|
2023-03-16 15:51:48 +08:00
|
|
|
|
type_ = await self.update_type(bot, event, stack, dependency_cache)
|
|
|
|
|
permission = await self.update_permission(
|
|
|
|
|
bot, event, stack, dependency_cache
|
|
|
|
|
)
|
2022-02-06 14:52:50 +08:00
|
|
|
|
|
2023-03-17 15:48:48 +08:00
|
|
|
|
self.new(
|
2022-02-06 14:52:50 +08:00
|
|
|
|
type_,
|
|
|
|
|
Rule(),
|
|
|
|
|
permission,
|
2024-03-07 14:57:26 +08:00
|
|
|
|
self.remain_handlers,
|
2022-02-06 14:52:50 +08:00
|
|
|
|
temp=True,
|
|
|
|
|
priority=0,
|
|
|
|
|
block=True,
|
2023-09-09 13:46:09 +08:00
|
|
|
|
source=self.__class__._source,
|
2022-07-20 10:21:31 +08:00
|
|
|
|
expire_time=bot.config.session_expire_timeout,
|
2022-02-06 14:52:50 +08:00
|
|
|
|
default_state=self.state,
|
|
|
|
|
default_type_updater=self.__class__._default_type_updater,
|
|
|
|
|
default_permission_updater=self.__class__._default_permission_updater,
|
|
|
|
|
)
|
2024-10-26 15:36:01 +08:00
|
|
|
|
elif isinstance(exc, PausedException):
|
2023-03-16 15:51:48 +08:00
|
|
|
|
type_ = await self.update_type(bot, event, stack, dependency_cache)
|
|
|
|
|
permission = await self.update_permission(
|
|
|
|
|
bot, event, stack, dependency_cache
|
|
|
|
|
)
|
2022-02-06 14:52:50 +08:00
|
|
|
|
|
2023-03-17 15:48:48 +08:00
|
|
|
|
self.new(
|
2022-02-06 14:52:50 +08:00
|
|
|
|
type_,
|
|
|
|
|
Rule(),
|
|
|
|
|
permission,
|
2024-03-07 14:57:26 +08:00
|
|
|
|
self.remain_handlers,
|
2022-02-06 14:52:50 +08:00
|
|
|
|
temp=True,
|
|
|
|
|
priority=0,
|
|
|
|
|
block=True,
|
2023-09-09 13:46:09 +08:00
|
|
|
|
source=self.__class__._source,
|
2022-07-20 10:21:31 +08:00
|
|
|
|
expire_time=bot.config.session_expire_timeout,
|
2022-02-06 14:52:50 +08:00
|
|
|
|
default_state=self.state,
|
|
|
|
|
default_type_updater=self.__class__._default_type_updater,
|
|
|
|
|
default_permission_updater=self.__class__._default_permission_updater,
|
|
|
|
|
)
|