2022-01-18 23:46:10 +08:00
|
|
|
|
"""本模块定义了事件处理主要流程。
|
2020-11-16 15:06:37 +08:00
|
|
|
|
|
|
|
|
|
NoneBot 内部处理并按优先级分发事件给所有事件响应器,提供了多个插槽以进行事件的预处理等。
|
2022-01-16 11:30:09 +08:00
|
|
|
|
|
|
|
|
|
FrontMatter:
|
2024-10-22 10:33:48 +08:00
|
|
|
|
mdx:
|
|
|
|
|
format: md
|
2022-01-16 11:30:09 +08:00
|
|
|
|
sidebar_position: 2
|
|
|
|
|
description: nonebot.message 模块
|
2020-11-13 17:15:45 +08:00
|
|
|
|
"""
|
|
|
|
|
|
2022-08-14 19:41:00 +08:00
|
|
|
|
import contextlib
|
2020-08-06 17:54:55 +08:00
|
|
|
|
from datetime import datetime
|
2021-11-15 21:44:24 +08:00
|
|
|
|
from contextlib import AsyncExitStack
|
2024-10-26 15:36:01 +08:00
|
|
|
|
from typing import TYPE_CHECKING, Any, Callable, Optional
|
|
|
|
|
|
|
|
|
|
import anyio
|
|
|
|
|
from exceptiongroup import BaseExceptionGroup, catch
|
2020-07-25 12:28:30 +08:00
|
|
|
|
|
2020-07-05 20:39:34 +08:00
|
|
|
|
from nonebot.log import logger
|
2020-08-17 16:09:41 +08:00
|
|
|
|
from nonebot.rule import TrieRule
|
2021-12-12 18:19:08 +08:00
|
|
|
|
from nonebot.dependencies import Dependent
|
2021-11-16 18:30:16 +08:00
|
|
|
|
from nonebot.matcher import Matcher, matchers
|
2021-12-06 00:17:52 +08:00
|
|
|
|
from nonebot.exception import (
|
|
|
|
|
NoLogException,
|
|
|
|
|
StopPropagation,
|
|
|
|
|
IgnoredException,
|
|
|
|
|
SkippedException,
|
|
|
|
|
)
|
2024-10-26 15:36:01 +08:00
|
|
|
|
from nonebot.utils import (
|
|
|
|
|
escape_tag,
|
|
|
|
|
run_coro_with_catch,
|
|
|
|
|
run_coro_with_shield,
|
|
|
|
|
flatten_exception_group,
|
|
|
|
|
)
|
2021-11-22 23:21:26 +08:00
|
|
|
|
from nonebot.typing import (
|
|
|
|
|
T_State,
|
2021-12-16 23:22:25 +08:00
|
|
|
|
T_DependencyCache,
|
2021-11-22 23:21:26 +08:00
|
|
|
|
T_RunPreProcessor,
|
|
|
|
|
T_RunPostProcessor,
|
|
|
|
|
T_EventPreProcessor,
|
|
|
|
|
T_EventPostProcessor,
|
|
|
|
|
)
|
2022-02-06 14:52:50 +08:00
|
|
|
|
from nonebot.internal.params import (
|
|
|
|
|
ArgParam,
|
|
|
|
|
BotParam,
|
|
|
|
|
EventParam,
|
|
|
|
|
StateParam,
|
|
|
|
|
DependParam,
|
|
|
|
|
DefaultParam,
|
|
|
|
|
MatcherParam,
|
|
|
|
|
ExceptionParam,
|
|
|
|
|
)
|
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
|
2020-07-25 12:28:30 +08:00
|
|
|
|
|
2024-04-16 00:33:48 +08:00
|
|
|
|
_event_preprocessors: set[Dependent[Any]] = set()
|
|
|
|
|
_event_postprocessors: set[Dependent[Any]] = set()
|
|
|
|
|
_run_preprocessors: set[Dependent[Any]] = set()
|
|
|
|
|
_run_postprocessors: set[Dependent[Any]] = set()
|
2021-11-16 18:30:16 +08:00
|
|
|
|
|
2022-09-09 11:52:57 +08:00
|
|
|
|
EVENT_PCS_PARAMS = (
|
2022-02-06 14:52:50 +08:00
|
|
|
|
DependParam,
|
|
|
|
|
BotParam,
|
|
|
|
|
EventParam,
|
|
|
|
|
StateParam,
|
|
|
|
|
DefaultParam,
|
2022-09-09 11:52:57 +08:00
|
|
|
|
)
|
|
|
|
|
RUN_PREPCS_PARAMS = (
|
2022-02-06 14:52:50 +08:00
|
|
|
|
DependParam,
|
|
|
|
|
BotParam,
|
|
|
|
|
EventParam,
|
|
|
|
|
StateParam,
|
|
|
|
|
ArgParam,
|
|
|
|
|
MatcherParam,
|
|
|
|
|
DefaultParam,
|
2022-09-09 11:52:57 +08:00
|
|
|
|
)
|
|
|
|
|
RUN_POSTPCS_PARAMS = (
|
2022-02-06 14:52:50 +08:00
|
|
|
|
DependParam,
|
|
|
|
|
ExceptionParam,
|
|
|
|
|
BotParam,
|
|
|
|
|
EventParam,
|
|
|
|
|
StateParam,
|
|
|
|
|
ArgParam,
|
|
|
|
|
MatcherParam,
|
|
|
|
|
DefaultParam,
|
2022-09-09 11:52:57 +08:00
|
|
|
|
)
|
2020-07-25 12:28:30 +08:00
|
|
|
|
|
|
|
|
|
|
2020-12-17 21:09:30 +08:00
|
|
|
|
def event_preprocessor(func: T_EventPreProcessor) -> T_EventPreProcessor:
|
2023-05-30 15:20:31 +08:00
|
|
|
|
"""事件预处理。
|
|
|
|
|
|
|
|
|
|
装饰一个函数,使它在每次接收到事件并分发给各响应器之前执行。
|
|
|
|
|
"""
|
2021-12-12 18:19:08 +08:00
|
|
|
|
_event_preprocessors.add(
|
2022-02-04 11:12:17 +08:00
|
|
|
|
Dependent[Any].parse(call=func, allow_types=EVENT_PCS_PARAMS)
|
2021-12-12 18:19:08 +08:00
|
|
|
|
)
|
2020-07-25 12:28:30 +08:00
|
|
|
|
return func
|
2020-07-04 22:51:10 +08:00
|
|
|
|
|
|
|
|
|
|
2020-12-17 21:09:30 +08:00
|
|
|
|
def event_postprocessor(func: T_EventPostProcessor) -> T_EventPostProcessor:
|
2023-05-30 15:20:31 +08:00
|
|
|
|
"""事件后处理。
|
|
|
|
|
|
|
|
|
|
装饰一个函数,使它在每次接收到事件并分发给各响应器之后执行。
|
|
|
|
|
"""
|
2021-12-12 18:19:08 +08:00
|
|
|
|
_event_postprocessors.add(
|
2022-02-04 11:12:17 +08:00
|
|
|
|
Dependent[Any].parse(call=func, allow_types=EVENT_PCS_PARAMS)
|
2021-12-12 18:19:08 +08:00
|
|
|
|
)
|
2020-11-07 17:35:44 +08:00
|
|
|
|
return func
|
|
|
|
|
|
|
|
|
|
|
2020-12-17 21:09:30 +08:00
|
|
|
|
def run_preprocessor(func: T_RunPreProcessor) -> T_RunPreProcessor:
|
2023-05-30 15:20:31 +08:00
|
|
|
|
"""运行预处理。
|
|
|
|
|
|
|
|
|
|
装饰一个函数,使它在每次事件响应器运行前执行。
|
|
|
|
|
"""
|
2021-12-12 18:19:08 +08:00
|
|
|
|
_run_preprocessors.add(
|
2022-02-04 11:12:17 +08:00
|
|
|
|
Dependent[Any].parse(call=func, allow_types=RUN_PREPCS_PARAMS)
|
2021-12-12 18:19:08 +08:00
|
|
|
|
)
|
2020-11-07 17:35:44 +08:00
|
|
|
|
return func
|
|
|
|
|
|
|
|
|
|
|
2020-12-17 21:09:30 +08:00
|
|
|
|
def run_postprocessor(func: T_RunPostProcessor) -> T_RunPostProcessor:
|
2023-05-30 15:20:31 +08:00
|
|
|
|
"""运行后处理。
|
|
|
|
|
|
|
|
|
|
装饰一个函数,使它在每次事件响应器运行后执行。
|
|
|
|
|
"""
|
2021-12-12 18:19:08 +08:00
|
|
|
|
_run_postprocessors.add(
|
2022-02-04 11:12:17 +08:00
|
|
|
|
Dependent[Any].parse(call=func, allow_types=RUN_POSTPCS_PARAMS)
|
2021-12-12 18:19:08 +08:00
|
|
|
|
)
|
2020-11-07 17:35:44 +08:00
|
|
|
|
return func
|
|
|
|
|
|
|
|
|
|
|
2024-10-26 15:36:01 +08:00
|
|
|
|
def _handle_ignored_exception(msg: str) -> Callable[[BaseExceptionGroup], None]:
|
|
|
|
|
def _handle(exc_group: BaseExceptionGroup[IgnoredException]) -> None:
|
|
|
|
|
logger.opt(colors=True).info(msg)
|
|
|
|
|
|
|
|
|
|
return _handle
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def _handle_exception(msg: str) -> Callable[[BaseExceptionGroup], None]:
|
|
|
|
|
def _handle(exc_group: BaseExceptionGroup[Exception]) -> None:
|
|
|
|
|
for exc in flatten_exception_group(exc_group):
|
|
|
|
|
logger.opt(colors=True, exception=exc).error(msg)
|
|
|
|
|
|
|
|
|
|
return _handle
|
|
|
|
|
|
|
|
|
|
|
2023-05-30 15:20:31 +08:00
|
|
|
|
async def _apply_event_preprocessors(
|
|
|
|
|
bot: "Bot",
|
|
|
|
|
event: "Event",
|
|
|
|
|
state: T_State,
|
|
|
|
|
stack: Optional[AsyncExitStack] = None,
|
|
|
|
|
dependency_cache: Optional[T_DependencyCache] = None,
|
|
|
|
|
show_log: bool = True,
|
|
|
|
|
) -> bool:
|
|
|
|
|
"""运行事件预处理。
|
|
|
|
|
|
|
|
|
|
参数:
|
|
|
|
|
bot: Bot 对象
|
|
|
|
|
event: Event 对象
|
|
|
|
|
state: 会话状态
|
|
|
|
|
stack: 异步上下文栈
|
|
|
|
|
dependency_cache: 依赖缓存
|
|
|
|
|
show_log: 是否显示日志
|
|
|
|
|
|
|
|
|
|
返回:
|
|
|
|
|
是否继续处理事件
|
|
|
|
|
"""
|
|
|
|
|
if not _event_preprocessors:
|
|
|
|
|
return True
|
|
|
|
|
|
|
|
|
|
if show_log:
|
|
|
|
|
logger.debug("Running PreProcessors...")
|
|
|
|
|
|
2024-10-26 15:36:01 +08:00
|
|
|
|
with catch(
|
|
|
|
|
{
|
|
|
|
|
IgnoredException: _handle_ignored_exception(
|
|
|
|
|
f"Event {escape_tag(event.get_event_name())} is <b>ignored</b>"
|
|
|
|
|
),
|
|
|
|
|
Exception: _handle_exception(
|
|
|
|
|
"<r><bg #f8bbd0>Error when running EventPreProcessors. "
|
|
|
|
|
"Event ignored!</bg #f8bbd0></r>"
|
|
|
|
|
),
|
|
|
|
|
}
|
|
|
|
|
):
|
|
|
|
|
async with anyio.create_task_group() as tg:
|
|
|
|
|
for proc in _event_preprocessors:
|
|
|
|
|
tg.start_soon(
|
|
|
|
|
run_coro_with_catch,
|
2023-05-30 15:20:31 +08:00
|
|
|
|
proc(
|
|
|
|
|
bot=bot,
|
|
|
|
|
event=event,
|
|
|
|
|
state=state,
|
|
|
|
|
stack=stack,
|
|
|
|
|
dependency_cache=dependency_cache,
|
|
|
|
|
),
|
|
|
|
|
(SkippedException,),
|
|
|
|
|
)
|
|
|
|
|
|
2024-10-26 15:36:01 +08:00
|
|
|
|
return True
|
|
|
|
|
|
|
|
|
|
return False
|
2023-05-30 15:20:31 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
async def _apply_event_postprocessors(
|
|
|
|
|
bot: "Bot",
|
|
|
|
|
event: "Event",
|
|
|
|
|
state: T_State,
|
|
|
|
|
stack: Optional[AsyncExitStack] = None,
|
|
|
|
|
dependency_cache: Optional[T_DependencyCache] = None,
|
|
|
|
|
show_log: bool = True,
|
|
|
|
|
) -> None:
|
|
|
|
|
"""运行事件后处理。
|
|
|
|
|
|
|
|
|
|
参数:
|
|
|
|
|
bot: Bot 对象
|
|
|
|
|
event: Event 对象
|
|
|
|
|
state: 会话状态
|
|
|
|
|
stack: 异步上下文栈
|
|
|
|
|
dependency_cache: 依赖缓存
|
|
|
|
|
show_log: 是否显示日志
|
|
|
|
|
"""
|
|
|
|
|
if not _event_postprocessors:
|
|
|
|
|
return
|
|
|
|
|
|
|
|
|
|
if show_log:
|
|
|
|
|
logger.debug("Running PostProcessors...")
|
|
|
|
|
|
2024-10-26 15:36:01 +08:00
|
|
|
|
with catch(
|
|
|
|
|
{
|
|
|
|
|
Exception: _handle_exception(
|
|
|
|
|
"<r><bg #f8bbd0>Error when running EventPostProcessors</bg #f8bbd0></r>"
|
|
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
):
|
|
|
|
|
async with anyio.create_task_group() as tg:
|
|
|
|
|
for proc in _event_postprocessors:
|
|
|
|
|
tg.start_soon(
|
|
|
|
|
run_coro_with_catch,
|
2023-05-30 15:20:31 +08:00
|
|
|
|
proc(
|
|
|
|
|
bot=bot,
|
|
|
|
|
event=event,
|
|
|
|
|
state=state,
|
|
|
|
|
stack=stack,
|
|
|
|
|
dependency_cache=dependency_cache,
|
|
|
|
|
),
|
|
|
|
|
(SkippedException,),
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
async def _apply_run_preprocessors(
|
|
|
|
|
bot: "Bot",
|
|
|
|
|
event: "Event",
|
|
|
|
|
state: T_State,
|
|
|
|
|
matcher: Matcher,
|
|
|
|
|
stack: Optional[AsyncExitStack] = None,
|
|
|
|
|
dependency_cache: Optional[T_DependencyCache] = None,
|
|
|
|
|
) -> bool:
|
|
|
|
|
"""运行事件响应器运行前处理。
|
|
|
|
|
|
|
|
|
|
参数:
|
|
|
|
|
bot: Bot 对象
|
|
|
|
|
event: Event 对象
|
|
|
|
|
state: 会话状态
|
|
|
|
|
matcher: 事件响应器
|
|
|
|
|
stack: 异步上下文栈
|
|
|
|
|
dependency_cache: 依赖缓存
|
|
|
|
|
|
|
|
|
|
返回:
|
|
|
|
|
是否继续处理事件
|
|
|
|
|
"""
|
|
|
|
|
if not _run_preprocessors:
|
|
|
|
|
return True
|
|
|
|
|
|
|
|
|
|
# ensure matcher function can be correctly called
|
2024-10-26 15:36:01 +08:00
|
|
|
|
with (
|
|
|
|
|
matcher.ensure_context(bot, event),
|
|
|
|
|
catch(
|
|
|
|
|
{
|
|
|
|
|
IgnoredException: _handle_ignored_exception(
|
|
|
|
|
f"{matcher} running is <b>cancelled</b>"
|
|
|
|
|
),
|
|
|
|
|
Exception: _handle_exception(
|
|
|
|
|
"<r><bg #f8bbd0>Error when running RunPreProcessors. "
|
|
|
|
|
"Running cancelled!</bg #f8bbd0></r>"
|
|
|
|
|
),
|
|
|
|
|
}
|
|
|
|
|
),
|
|
|
|
|
):
|
|
|
|
|
async with anyio.create_task_group() as tg:
|
|
|
|
|
for proc in _run_preprocessors:
|
|
|
|
|
tg.start_soon(
|
|
|
|
|
run_coro_with_catch,
|
|
|
|
|
proc(
|
|
|
|
|
matcher=matcher,
|
|
|
|
|
bot=bot,
|
|
|
|
|
event=event,
|
|
|
|
|
state=state,
|
|
|
|
|
stack=stack,
|
|
|
|
|
dependency_cache=dependency_cache,
|
|
|
|
|
),
|
|
|
|
|
(SkippedException,),
|
2023-05-30 15:20:31 +08:00
|
|
|
|
)
|
|
|
|
|
|
2024-10-26 15:36:01 +08:00
|
|
|
|
return True
|
|
|
|
|
|
|
|
|
|
return False
|
2023-05-30 15:20:31 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
async def _apply_run_postprocessors(
|
|
|
|
|
bot: "Bot",
|
|
|
|
|
event: "Event",
|
|
|
|
|
matcher: Matcher,
|
|
|
|
|
exception: Optional[Exception] = None,
|
|
|
|
|
stack: Optional[AsyncExitStack] = None,
|
|
|
|
|
dependency_cache: Optional[T_DependencyCache] = None,
|
|
|
|
|
) -> None:
|
|
|
|
|
"""运行事件响应器运行后处理。
|
|
|
|
|
|
2023-06-24 14:47:35 +08:00
|
|
|
|
参数:
|
2023-05-30 15:20:31 +08:00
|
|
|
|
bot: Bot 对象
|
|
|
|
|
event: Event 对象
|
|
|
|
|
matcher: 事件响应器
|
|
|
|
|
exception: 事件响应器运行异常
|
|
|
|
|
stack: 异步上下文栈
|
|
|
|
|
dependency_cache: 依赖缓存
|
|
|
|
|
"""
|
|
|
|
|
if not _run_postprocessors:
|
|
|
|
|
return
|
|
|
|
|
|
2024-10-26 15:36:01 +08:00
|
|
|
|
with (
|
|
|
|
|
matcher.ensure_context(bot, event),
|
|
|
|
|
catch(
|
|
|
|
|
{
|
|
|
|
|
Exception: _handle_exception(
|
|
|
|
|
"<r><bg #f8bbd0>Error when running RunPostProcessors"
|
|
|
|
|
"</bg #f8bbd0></r>"
|
|
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
),
|
|
|
|
|
):
|
|
|
|
|
async with anyio.create_task_group() as tg:
|
|
|
|
|
for proc in _run_postprocessors:
|
|
|
|
|
tg.start_soon(
|
|
|
|
|
run_coro_with_catch,
|
|
|
|
|
proc(
|
|
|
|
|
matcher=matcher,
|
|
|
|
|
exception=exception,
|
|
|
|
|
bot=bot,
|
|
|
|
|
event=event,
|
|
|
|
|
state=matcher.state,
|
|
|
|
|
stack=stack,
|
|
|
|
|
dependency_cache=dependency_cache,
|
|
|
|
|
),
|
|
|
|
|
(SkippedException,),
|
2023-05-30 15:20:31 +08:00
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
2021-11-21 12:36:44 +08:00
|
|
|
|
async def _check_matcher(
|
2024-04-16 00:33:48 +08:00
|
|
|
|
Matcher: type[Matcher],
|
2021-11-22 23:21:26 +08:00
|
|
|
|
bot: "Bot",
|
|
|
|
|
event: "Event",
|
|
|
|
|
state: T_State,
|
|
|
|
|
stack: Optional[AsyncExitStack] = None,
|
2021-12-16 23:22:25 +08:00
|
|
|
|
dependency_cache: Optional[T_DependencyCache] = None,
|
2023-05-30 15:20:31 +08:00
|
|
|
|
) -> bool:
|
|
|
|
|
"""检查事件响应器是否符合运行条件。
|
|
|
|
|
|
|
|
|
|
请注意,过时的事件响应器将被**销毁**。对于未过时的事件响应器,将会一次检查其响应类型、权限和规则。
|
|
|
|
|
|
|
|
|
|
参数:
|
|
|
|
|
Matcher: 要检查的事件响应器
|
|
|
|
|
bot: Bot 对象
|
|
|
|
|
event: Event 对象
|
|
|
|
|
state: 会话状态
|
|
|
|
|
stack: 异步上下文栈
|
|
|
|
|
dependency_cache: 依赖缓存
|
|
|
|
|
|
|
|
|
|
返回:
|
|
|
|
|
bool: 是否符合运行条件
|
|
|
|
|
"""
|
2021-02-01 11:42:05 +08:00
|
|
|
|
if Matcher.expire_time and datetime.now() > Matcher.expire_time:
|
2022-08-14 19:41:00 +08:00
|
|
|
|
with contextlib.suppress(Exception):
|
2022-12-06 14:19:48 +08:00
|
|
|
|
Matcher.destroy()
|
2023-05-30 15:20:31 +08:00
|
|
|
|
return False
|
2021-02-01 11:42:05 +08:00
|
|
|
|
|
|
|
|
|
try:
|
2023-09-12 15:13:35 +08:00
|
|
|
|
if not await Matcher.check_perm(bot, event, stack, dependency_cache):
|
|
|
|
|
logger.trace(f"Permission conditions not met for {Matcher}")
|
|
|
|
|
return False
|
|
|
|
|
except Exception as e:
|
|
|
|
|
logger.opt(colors=True, exception=e).error(
|
|
|
|
|
f"<r><bg #f8bbd0>Permission check failed for {Matcher}.</bg #f8bbd0></r>"
|
|
|
|
|
)
|
|
|
|
|
return False
|
|
|
|
|
|
|
|
|
|
try:
|
|
|
|
|
if not await Matcher.check_rule(bot, event, state, stack, dependency_cache):
|
|
|
|
|
logger.trace(f"Rule conditions not met for {Matcher}")
|
2023-05-30 15:20:31 +08:00
|
|
|
|
return False
|
2021-02-01 11:42:05 +08:00
|
|
|
|
except Exception as e:
|
|
|
|
|
logger.opt(colors=True, exception=e).error(
|
2021-11-22 23:21:26 +08:00
|
|
|
|
f"<r><bg #f8bbd0>Rule check failed for {Matcher}.</bg #f8bbd0></r>"
|
|
|
|
|
)
|
2023-05-30 15:20:31 +08:00
|
|
|
|
return False
|
2021-02-01 11:42:05 +08:00
|
|
|
|
|
2023-05-30 15:20:31 +08:00
|
|
|
|
return True
|
2020-08-21 14:24:32 +08:00
|
|
|
|
|
2020-11-16 11:25:42 +08:00
|
|
|
|
|
2021-11-21 12:36:44 +08:00
|
|
|
|
async def _run_matcher(
|
2024-04-16 00:33:48 +08:00
|
|
|
|
Matcher: type[Matcher],
|
2021-11-22 23:21:26 +08:00
|
|
|
|
bot: "Bot",
|
|
|
|
|
event: "Event",
|
|
|
|
|
state: T_State,
|
|
|
|
|
stack: Optional[AsyncExitStack] = None,
|
2021-12-16 23:22:25 +08:00
|
|
|
|
dependency_cache: Optional[T_DependencyCache] = None,
|
2021-11-22 23:21:26 +08:00
|
|
|
|
) -> None:
|
2023-05-30 15:20:31 +08:00
|
|
|
|
"""运行事件响应器。
|
|
|
|
|
|
|
|
|
|
临时事件响应器将在运行前被**销毁**。
|
|
|
|
|
|
|
|
|
|
参数:
|
|
|
|
|
Matcher: 事件响应器
|
|
|
|
|
bot: Bot 对象
|
|
|
|
|
event: Event 对象
|
|
|
|
|
state: 会话状态
|
|
|
|
|
stack: 异步上下文栈
|
|
|
|
|
dependency_cache: 依赖缓存
|
|
|
|
|
|
|
|
|
|
异常:
|
|
|
|
|
StopPropagation: 阻止事件继续传播
|
|
|
|
|
"""
|
2020-08-26 17:47:36 +08:00
|
|
|
|
logger.info(f"Event will be handled by {Matcher}")
|
|
|
|
|
|
2023-05-30 15:20:31 +08:00
|
|
|
|
if Matcher.temp:
|
|
|
|
|
with contextlib.suppress(Exception):
|
|
|
|
|
Matcher.destroy()
|
|
|
|
|
|
2020-08-21 14:24:32 +08:00
|
|
|
|
matcher = Matcher()
|
2022-11-15 10:50:52 +08:00
|
|
|
|
|
2023-05-30 15:20:31 +08:00
|
|
|
|
if not await _apply_run_preprocessors(
|
|
|
|
|
bot=bot,
|
|
|
|
|
event=event,
|
|
|
|
|
state=state,
|
|
|
|
|
matcher=matcher,
|
|
|
|
|
stack=stack,
|
|
|
|
|
dependency_cache=dependency_cache,
|
|
|
|
|
):
|
|
|
|
|
return
|
2020-11-07 17:35:44 +08:00
|
|
|
|
|
2020-11-16 11:25:42 +08:00
|
|
|
|
exception = None
|
2020-11-07 17:35:44 +08:00
|
|
|
|
|
2024-10-26 15:36:01 +08:00
|
|
|
|
logger.debug(f"Running {matcher}")
|
|
|
|
|
|
2020-08-21 14:24:32 +08:00
|
|
|
|
try:
|
2021-11-21 15:46:48 +08:00
|
|
|
|
await matcher.run(bot, event, state, stack, dependency_cache)
|
2020-08-21 14:24:32 +08:00
|
|
|
|
except Exception as e:
|
2020-08-27 16:43:58 +08:00
|
|
|
|
logger.opt(colors=True, exception=e).error(
|
2022-09-09 11:52:57 +08:00
|
|
|
|
f"<r><bg #f8bbd0>Running {matcher} failed.</bg #f8bbd0></r>"
|
2020-08-27 16:43:58 +08:00
|
|
|
|
)
|
2020-11-16 11:25:42 +08:00
|
|
|
|
exception = e
|
2020-11-07 17:35:44 +08:00
|
|
|
|
|
2023-05-30 15:20:31 +08:00
|
|
|
|
await _apply_run_postprocessors(
|
|
|
|
|
bot=bot,
|
|
|
|
|
event=event,
|
|
|
|
|
matcher=matcher,
|
|
|
|
|
exception=exception,
|
|
|
|
|
stack=stack,
|
|
|
|
|
dependency_cache=dependency_cache,
|
|
|
|
|
)
|
2020-11-07 17:35:44 +08:00
|
|
|
|
|
2020-12-24 22:19:08 +08:00
|
|
|
|
if matcher.block:
|
2020-11-16 11:25:42 +08:00
|
|
|
|
raise StopPropagation
|
2023-05-30 15:20:31 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
async def check_and_run_matcher(
|
2024-04-16 00:33:48 +08:00
|
|
|
|
Matcher: type[Matcher],
|
2023-05-30 15:20:31 +08:00
|
|
|
|
bot: "Bot",
|
|
|
|
|
event: "Event",
|
|
|
|
|
state: T_State,
|
|
|
|
|
stack: Optional[AsyncExitStack] = None,
|
|
|
|
|
dependency_cache: Optional[T_DependencyCache] = None,
|
|
|
|
|
) -> None:
|
|
|
|
|
"""检查并运行事件响应器。
|
|
|
|
|
|
|
|
|
|
参数:
|
|
|
|
|
Matcher: 事件响应器
|
|
|
|
|
bot: Bot 对象
|
|
|
|
|
event: Event 对象
|
|
|
|
|
state: 会话状态
|
|
|
|
|
stack: 异步上下文栈
|
|
|
|
|
dependency_cache: 依赖缓存
|
|
|
|
|
"""
|
|
|
|
|
if not await _check_matcher(
|
|
|
|
|
Matcher=Matcher,
|
|
|
|
|
bot=bot,
|
|
|
|
|
event=event,
|
|
|
|
|
state=state,
|
|
|
|
|
stack=stack,
|
|
|
|
|
dependency_cache=dependency_cache,
|
|
|
|
|
):
|
|
|
|
|
return
|
|
|
|
|
|
|
|
|
|
await _run_matcher(
|
|
|
|
|
Matcher=Matcher,
|
|
|
|
|
bot=bot,
|
|
|
|
|
event=event,
|
|
|
|
|
state=state,
|
|
|
|
|
stack=stack,
|
|
|
|
|
dependency_cache=dependency_cache,
|
|
|
|
|
)
|
2020-08-21 14:24:32 +08:00
|
|
|
|
|
|
|
|
|
|
2021-09-27 12:52:21 +08:00
|
|
|
|
async def handle_event(bot: "Bot", event: "Event") -> None:
|
2022-01-18 23:46:10 +08:00
|
|
|
|
"""处理一个事件。调用该函数以实现分发事件。
|
2020-11-30 11:08:00 +08:00
|
|
|
|
|
2022-01-12 18:31:12 +08:00
|
|
|
|
参数:
|
2022-01-12 19:10:29 +08:00
|
|
|
|
bot: Bot 对象
|
|
|
|
|
event: Event 对象
|
2020-11-30 11:08:00 +08:00
|
|
|
|
|
2022-01-12 18:53:30 +08:00
|
|
|
|
用法:
|
|
|
|
|
```python
|
2024-10-26 15:36:01 +08:00
|
|
|
|
driver.task_group.start_soon(handle_event, bot, event)
|
2022-01-12 18:53:30 +08:00
|
|
|
|
```
|
2020-11-16 15:06:37 +08:00
|
|
|
|
"""
|
2020-10-29 17:06:07 +08:00
|
|
|
|
show_log = True
|
2022-09-09 11:52:57 +08:00
|
|
|
|
log_msg = f"<m>{escape_tag(bot.type)} {escape_tag(bot.self_id)}</m> | "
|
2020-12-09 17:51:24 +08:00
|
|
|
|
try:
|
2020-12-19 14:16:47 +08:00
|
|
|
|
log_msg += event.get_log_string()
|
2020-12-09 17:51:24 +08:00
|
|
|
|
except NoLogException:
|
2020-10-29 17:06:07 +08:00
|
|
|
|
show_log = False
|
|
|
|
|
if show_log:
|
2021-04-19 21:15:10 +08:00
|
|
|
|
logger.opt(colors=True).success(log_msg)
|
2020-08-25 18:02:18 +08:00
|
|
|
|
|
2024-04-16 00:33:48 +08:00
|
|
|
|
state: dict[Any, Any] = {}
|
2021-12-16 23:22:25 +08:00
|
|
|
|
dependency_cache: T_DependencyCache = {}
|
2020-07-04 22:51:10 +08:00
|
|
|
|
|
2023-05-30 15:20:31 +08:00
|
|
|
|
# create event scope context
|
2021-11-15 21:44:24 +08:00
|
|
|
|
async with AsyncExitStack() as stack:
|
2023-05-30 15:20:31 +08:00
|
|
|
|
if not await _apply_event_preprocessors(
|
|
|
|
|
bot=bot,
|
|
|
|
|
event=event,
|
|
|
|
|
state=state,
|
|
|
|
|
stack=stack,
|
|
|
|
|
dependency_cache=dependency_cache,
|
|
|
|
|
):
|
|
|
|
|
return
|
2021-11-15 21:44:24 +08:00
|
|
|
|
|
|
|
|
|
# Trie Match
|
2021-12-27 02:26:02 +08:00
|
|
|
|
try:
|
|
|
|
|
TrieRule.get_value(bot, event, state)
|
|
|
|
|
except Exception as e:
|
|
|
|
|
logger.opt(colors=True, exception=e).warning(
|
|
|
|
|
"Error while parsing command for event"
|
|
|
|
|
)
|
2021-11-15 21:44:24 +08:00
|
|
|
|
|
|
|
|
|
break_flag = False
|
2024-10-26 15:36:01 +08:00
|
|
|
|
|
|
|
|
|
def _handle_stop_propagation(exc_group: BaseExceptionGroup) -> None:
|
|
|
|
|
nonlocal break_flag
|
|
|
|
|
|
|
|
|
|
break_flag = True
|
|
|
|
|
logger.debug("Stop event propagation")
|
|
|
|
|
|
2023-05-30 15:20:31 +08:00
|
|
|
|
# iterate through all priority until stop propagation
|
2021-11-15 21:44:24 +08:00
|
|
|
|
for priority in sorted(matchers.keys()):
|
|
|
|
|
if break_flag:
|
|
|
|
|
break
|
2020-11-07 17:35:44 +08:00
|
|
|
|
|
2021-03-27 14:42:43 +08:00
|
|
|
|
if show_log:
|
2021-11-15 21:44:24 +08:00
|
|
|
|
logger.debug(f"Checking for matchers in priority {priority}...")
|
|
|
|
|
|
2024-10-26 15:36:01 +08:00
|
|
|
|
if not (priority_matchers := matchers[priority]):
|
|
|
|
|
continue
|
|
|
|
|
|
|
|
|
|
with catch(
|
|
|
|
|
{
|
|
|
|
|
StopPropagation: _handle_stop_propagation,
|
|
|
|
|
Exception: _handle_exception(
|
2021-11-15 21:44:24 +08:00
|
|
|
|
"<r><bg #f8bbd0>Error when checking Matcher.</bg #f8bbd0></r>"
|
2024-10-26 15:36:01 +08:00
|
|
|
|
),
|
|
|
|
|
}
|
|
|
|
|
):
|
|
|
|
|
async with anyio.create_task_group() as tg:
|
|
|
|
|
for matcher in priority_matchers:
|
|
|
|
|
tg.start_soon(
|
|
|
|
|
run_coro_with_shield,
|
|
|
|
|
check_and_run_matcher(
|
|
|
|
|
matcher,
|
|
|
|
|
bot,
|
|
|
|
|
event,
|
|
|
|
|
state.copy(),
|
|
|
|
|
stack,
|
|
|
|
|
dependency_cache,
|
|
|
|
|
),
|
|
|
|
|
)
|
2021-11-15 21:44:24 +08:00
|
|
|
|
|
2023-01-11 16:51:20 +08:00
|
|
|
|
if show_log:
|
|
|
|
|
logger.debug("Checking for matchers completed")
|
|
|
|
|
|
2023-05-30 15:20:31 +08:00
|
|
|
|
await _apply_event_postprocessors(bot, event, state, stack, dependency_cache)
|