mirror of
https://github.com/nonebot/nonebot2.git
synced 2024-11-24 17:15:05 +08:00
commit
bc8833ce8e
@ -83,23 +83,6 @@ sidebarDepth: 0
|
|||||||
|
|
||||||
|
|
||||||
|
|
||||||
## _exception_ `ExpiredException`
|
|
||||||
|
|
||||||
基类:`Exception`
|
|
||||||
|
|
||||||
|
|
||||||
* **说明**
|
|
||||||
|
|
||||||
指示 NoneBot 当前 `Matcher` 已失效。
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
* **用法**
|
|
||||||
|
|
||||||
当 `Matcher` 运行前检查时抛出。
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
## _exception_ `StopPropagation`
|
## _exception_ `StopPropagation`
|
||||||
|
|
||||||
基类:`Exception`
|
基类:`Exception`
|
||||||
|
@ -6,3 +6,138 @@ sidebarDepth: 0
|
|||||||
# NoneBot.message 模块
|
# NoneBot.message 模块
|
||||||
|
|
||||||
## 事件处理
|
## 事件处理
|
||||||
|
|
||||||
|
NoneBot 内部处理并按优先级分发事件给所有事件响应器,提供了多个插槽以进行事件的预处理等。
|
||||||
|
|
||||||
|
|
||||||
|
## `event_preprocessor(func)`
|
||||||
|
|
||||||
|
|
||||||
|
* **说明**
|
||||||
|
|
||||||
|
事件预处理。装饰一个函数,使它在每次接收到事件并分发给各响应器之前执行。
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
* **参数**
|
||||||
|
|
||||||
|
事件预处理函数接收三个参数。
|
||||||
|
|
||||||
|
|
||||||
|
* `bot: Bot`: Bot 对象
|
||||||
|
|
||||||
|
|
||||||
|
* `event: Event`: Event 对象
|
||||||
|
|
||||||
|
|
||||||
|
* `state: dict`: 当前 State
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
## `event_postprocessor(func)`
|
||||||
|
|
||||||
|
|
||||||
|
* **说明**
|
||||||
|
|
||||||
|
事件后处理。装饰一个函数,使它在每次接收到事件并分发给各响应器之后执行。
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
* **参数**
|
||||||
|
|
||||||
|
事件后处理函数接收三个参数。
|
||||||
|
|
||||||
|
|
||||||
|
* `bot: Bot`: Bot 对象
|
||||||
|
|
||||||
|
|
||||||
|
* `event: Event`: Event 对象
|
||||||
|
|
||||||
|
|
||||||
|
* `state: dict`: 当前事件运行前 State
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
## `run_preprocessor(func)`
|
||||||
|
|
||||||
|
|
||||||
|
* **说明**
|
||||||
|
|
||||||
|
运行预处理。装饰一个函数,使它在每次事件响应器运行前执行。
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
* **参数**
|
||||||
|
|
||||||
|
运行预处理函数接收四个参数。
|
||||||
|
|
||||||
|
|
||||||
|
* `matcher: Matcher`: 当前要运行的事件响应器
|
||||||
|
|
||||||
|
|
||||||
|
* `bot: Bot`: Bot 对象
|
||||||
|
|
||||||
|
|
||||||
|
* `event: Event`: Event 对象
|
||||||
|
|
||||||
|
|
||||||
|
* `state: dict`: 当前 State
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
## `run_postprocessor(func)`
|
||||||
|
|
||||||
|
|
||||||
|
* **说明**
|
||||||
|
|
||||||
|
运行后处理。装饰一个函数,使它在每次事件响应器运行后执行。
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
* **参数**
|
||||||
|
|
||||||
|
运行后处理函数接收五个参数。
|
||||||
|
|
||||||
|
|
||||||
|
* `matcher: Matcher`: 运行完毕的事件响应器
|
||||||
|
|
||||||
|
|
||||||
|
* `exception: Optional[Exception]`: 事件响应器运行错误(如果存在)
|
||||||
|
|
||||||
|
|
||||||
|
* `bot: Bot`: Bot 对象
|
||||||
|
|
||||||
|
|
||||||
|
* `event: Event`: Event 对象
|
||||||
|
|
||||||
|
|
||||||
|
* `state: dict`: 当前 State
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
## _async_ `handle_event(bot, event)`
|
||||||
|
|
||||||
|
|
||||||
|
* **说明**
|
||||||
|
|
||||||
|
处理一个事件。调用该函数以实现分发事件。
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
* **参数**
|
||||||
|
|
||||||
|
|
||||||
|
* `bot: Bot`: Bot 对象
|
||||||
|
|
||||||
|
|
||||||
|
* `event: Event`: Event 对象
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
* **示例**
|
||||||
|
|
||||||
|
|
||||||
|
```python
|
||||||
|
import asyncio
|
||||||
|
asyncio.create_task(handle_event(bot, event))
|
||||||
|
```
|
||||||
|
@ -163,13 +163,13 @@ sidebarDepth: 0
|
|||||||
|
|
||||||
* **类型**
|
* **类型**
|
||||||
|
|
||||||
`Callable[[Matcher, List[Any], Bot, Event, dict], Union[Awaitable[None], Awaitable[NoReturn]]]`
|
`Callable[[Matcher, Optional[Exception], Bot, Event, dict], Union[Awaitable[None], Awaitable[NoReturn]]]`
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
* **说明**
|
* **说明**
|
||||||
|
|
||||||
事件响应器运行前预处理函数 RunPostProcessor 类型,第二个参数包含运行时产生的错误以及 `ExpiredException`, `StopPropagation` (如果存在)
|
事件响应器运行前预处理函数 RunPostProcessor 类型,第二个参数为运行时产生的错误(如果存在)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
@ -9,12 +9,6 @@
|
|||||||
from nonebot.typing import List, Type, Optional
|
from nonebot.typing import List, Type, Optional
|
||||||
|
|
||||||
|
|
||||||
class _ExceptionContainer(Exception):
|
|
||||||
|
|
||||||
def __init__(self, exceptions: List[Type[Exception]]) -> None:
|
|
||||||
self.exceptions = exceptions
|
|
||||||
|
|
||||||
|
|
||||||
class IgnoredException(Exception):
|
class IgnoredException(Exception):
|
||||||
"""
|
"""
|
||||||
:说明:
|
:说明:
|
||||||
@ -79,19 +73,6 @@ class FinishedException(Exception):
|
|||||||
pass
|
pass
|
||||||
|
|
||||||
|
|
||||||
class ExpiredException(Exception):
|
|
||||||
"""
|
|
||||||
:说明:
|
|
||||||
|
|
||||||
指示 NoneBot 当前 ``Matcher`` 已失效。
|
|
||||||
|
|
||||||
:用法:
|
|
||||||
|
|
||||||
当 ``Matcher`` 运行前检查时抛出。
|
|
||||||
"""
|
|
||||||
pass
|
|
||||||
|
|
||||||
|
|
||||||
class StopPropagation(Exception):
|
class StopPropagation(Exception):
|
||||||
"""
|
"""
|
||||||
:说明:
|
:说明:
|
||||||
|
@ -1,6 +1,8 @@
|
|||||||
"""
|
"""
|
||||||
事件处理
|
事件处理
|
||||||
========
|
========
|
||||||
|
|
||||||
|
NoneBot 内部处理并按优先级分发事件给所有事件响应器,提供了多个插槽以进行事件的预处理等。
|
||||||
"""
|
"""
|
||||||
|
|
||||||
import asyncio
|
import asyncio
|
||||||
@ -9,10 +11,9 @@ from datetime import datetime
|
|||||||
from nonebot.log import logger
|
from nonebot.log import logger
|
||||||
from nonebot.rule import TrieRule
|
from nonebot.rule import TrieRule
|
||||||
from nonebot.utils import escape_tag
|
from nonebot.utils import escape_tag
|
||||||
from nonebot.matcher import matchers
|
from nonebot.matcher import matchers, Matcher
|
||||||
from nonebot.exception import IgnoredException, ExpiredException
|
from nonebot.typing import Set, Type, Union, Optional, Iterable, NoReturn, Bot, Event
|
||||||
from nonebot.exception import StopPropagation, _ExceptionContainer
|
from nonebot.exception import IgnoredException, StopPropagation
|
||||||
from nonebot.typing import Set, Type, Union, NoReturn, Bot, Event, Matcher
|
|
||||||
from nonebot.typing import EventPreProcessor, RunPreProcessor, EventPostProcessor, RunPostProcessor
|
from nonebot.typing import EventPreProcessor, RunPreProcessor, EventPostProcessor, RunPostProcessor
|
||||||
|
|
||||||
_event_preprocessors: Set[EventPreProcessor] = set()
|
_event_preprocessors: Set[EventPreProcessor] = set()
|
||||||
@ -22,39 +23,108 @@ _run_postprocessors: Set[RunPostProcessor] = set()
|
|||||||
|
|
||||||
|
|
||||||
def event_preprocessor(func: EventPreProcessor) -> EventPreProcessor:
|
def event_preprocessor(func: EventPreProcessor) -> EventPreProcessor:
|
||||||
|
"""
|
||||||
|
:说明:
|
||||||
|
事件预处理。装饰一个函数,使它在每次接收到事件并分发给各响应器之前执行。
|
||||||
|
:参数:
|
||||||
|
事件预处理函数接收三个参数。
|
||||||
|
|
||||||
|
* ``bot: Bot``: Bot 对象
|
||||||
|
* ``event: Event``: Event 对象
|
||||||
|
* ``state: dict``: 当前 State
|
||||||
|
"""
|
||||||
_event_preprocessors.add(func)
|
_event_preprocessors.add(func)
|
||||||
return func
|
return func
|
||||||
|
|
||||||
|
|
||||||
def event_postprocessor(func: EventPostProcessor) -> EventPostProcessor:
|
def event_postprocessor(func: EventPostProcessor) -> EventPostProcessor:
|
||||||
|
"""
|
||||||
|
:说明:
|
||||||
|
事件后处理。装饰一个函数,使它在每次接收到事件并分发给各响应器之后执行。
|
||||||
|
:参数:
|
||||||
|
事件后处理函数接收三个参数。
|
||||||
|
|
||||||
|
* ``bot: Bot``: Bot 对象
|
||||||
|
* ``event: Event``: Event 对象
|
||||||
|
* ``state: dict``: 当前事件运行前 State
|
||||||
|
"""
|
||||||
_event_postprocessors.add(func)
|
_event_postprocessors.add(func)
|
||||||
return func
|
return func
|
||||||
|
|
||||||
|
|
||||||
def run_preprocessor(func: RunPreProcessor) -> RunPreProcessor:
|
def run_preprocessor(func: RunPreProcessor) -> RunPreProcessor:
|
||||||
|
"""
|
||||||
|
:说明:
|
||||||
|
运行预处理。装饰一个函数,使它在每次事件响应器运行前执行。
|
||||||
|
:参数:
|
||||||
|
运行预处理函数接收四个参数。
|
||||||
|
|
||||||
|
* ``matcher: Matcher``: 当前要运行的事件响应器
|
||||||
|
* ``bot: Bot``: Bot 对象
|
||||||
|
* ``event: Event``: Event 对象
|
||||||
|
* ``state: dict``: 当前 State
|
||||||
|
"""
|
||||||
_run_preprocessors.add(func)
|
_run_preprocessors.add(func)
|
||||||
return func
|
return func
|
||||||
|
|
||||||
|
|
||||||
def run_postprocessor(func: RunPostProcessor) -> RunPostProcessor:
|
def run_postprocessor(func: RunPostProcessor) -> RunPostProcessor:
|
||||||
|
"""
|
||||||
|
:说明:
|
||||||
|
运行后处理。装饰一个函数,使它在每次事件响应器运行后执行。
|
||||||
|
:参数:
|
||||||
|
运行后处理函数接收五个参数。
|
||||||
|
|
||||||
|
* ``matcher: Matcher``: 运行完毕的事件响应器
|
||||||
|
* ``exception: Optional[Exception]``: 事件响应器运行错误(如果存在)
|
||||||
|
* ``bot: Bot``: Bot 对象
|
||||||
|
* ``event: Event``: Event 对象
|
||||||
|
* ``state: dict``: 当前 State
|
||||||
|
"""
|
||||||
_run_postprocessors.add(func)
|
_run_postprocessors.add(func)
|
||||||
return func
|
return func
|
||||||
|
|
||||||
|
|
||||||
|
async def _check_matcher(priority: int, bot: Bot, event: Event,
|
||||||
|
state: dict) -> Iterable[Type[Matcher]]:
|
||||||
|
current_matchers = matchers[priority].copy()
|
||||||
|
|
||||||
|
async def _check(Matcher: Type[Matcher], bot: Bot, event: Event,
|
||||||
|
state: dict) -> Optional[Type[Matcher]]:
|
||||||
|
try:
|
||||||
|
if await Matcher.check_perm(
|
||||||
|
bot, event) and await Matcher.check_rule(bot, event, state):
|
||||||
|
return Matcher
|
||||||
|
except Exception as e:
|
||||||
|
logger.opt(colors=True, exception=e).error(
|
||||||
|
f"<r><bg #f8bbd0>Rule check failed for {Matcher}.</bg #f8bbd0></r>"
|
||||||
|
)
|
||||||
|
return None
|
||||||
|
|
||||||
|
async def _check_expire(Matcher: Type[Matcher]) -> Optional[Type[Matcher]]:
|
||||||
|
if Matcher.temp or (Matcher.expire_time and
|
||||||
|
datetime.now() > Matcher.expire_time):
|
||||||
|
return Matcher
|
||||||
|
return None
|
||||||
|
|
||||||
|
checking_tasks = [
|
||||||
|
_check(Matcher, bot, event, state) for Matcher in current_matchers
|
||||||
|
]
|
||||||
|
checking_expire_tasks = [
|
||||||
|
_check_expire(Matcher) for Matcher in current_matchers
|
||||||
|
]
|
||||||
|
results = await asyncio.gather(*checking_tasks, return_exceptions=True)
|
||||||
|
expired = await asyncio.gather(*checking_expire_tasks)
|
||||||
|
for expired_matcher in filter(lambda x: x and x in results, expired):
|
||||||
|
try:
|
||||||
|
matchers[priority].remove(expired_matcher)
|
||||||
|
except Exception:
|
||||||
|
pass
|
||||||
|
return filter(lambda x: x, results)
|
||||||
|
|
||||||
|
|
||||||
async def _run_matcher(Matcher: Type[Matcher], bot: Bot, event: Event,
|
async def _run_matcher(Matcher: Type[Matcher], bot: Bot, event: Event,
|
||||||
state: dict) -> Union[None, NoReturn]:
|
state: dict) -> Union[None, NoReturn]:
|
||||||
if Matcher.expire_time and datetime.now() > Matcher.expire_time:
|
|
||||||
raise _ExceptionContainer([ExpiredException])
|
|
||||||
|
|
||||||
try:
|
|
||||||
if not await Matcher.check_perm(
|
|
||||||
bot, event) or not await Matcher.check_rule(bot, event, state):
|
|
||||||
return
|
|
||||||
except Exception as e:
|
|
||||||
logger.opt(colors=True, exception=e).error(
|
|
||||||
f"<r><bg #f8bbd0>Rule check failed for {Matcher}.</bg #f8bbd0></r>")
|
|
||||||
return
|
|
||||||
|
|
||||||
logger.info(f"Event will be handled by {Matcher}")
|
logger.info(f"Event will be handled by {Matcher}")
|
||||||
|
|
||||||
matcher = Matcher()
|
matcher = Matcher()
|
||||||
@ -74,7 +144,7 @@ async def _run_matcher(Matcher: Type[Matcher], bot: Bot, event: Event,
|
|||||||
"Running cancelled!</bg #f8bbd0></r>")
|
"Running cancelled!</bg #f8bbd0></r>")
|
||||||
return
|
return
|
||||||
|
|
||||||
exceptions = []
|
exception = None
|
||||||
|
|
||||||
try:
|
try:
|
||||||
logger.debug(f"Running matcher {matcher}")
|
logger.debug(f"Running matcher {matcher}")
|
||||||
@ -83,15 +153,10 @@ async def _run_matcher(Matcher: Type[Matcher], bot: Bot, event: Event,
|
|||||||
logger.opt(colors=True, exception=e).error(
|
logger.opt(colors=True, exception=e).error(
|
||||||
f"<r><bg #f8bbd0>Running matcher {matcher} failed.</bg #f8bbd0></r>"
|
f"<r><bg #f8bbd0>Running matcher {matcher} failed.</bg #f8bbd0></r>"
|
||||||
)
|
)
|
||||||
exceptions.append(e)
|
exception = e
|
||||||
|
|
||||||
if Matcher.temp:
|
|
||||||
exceptions.append(ExpiredException)
|
|
||||||
if Matcher.block:
|
|
||||||
exceptions.append(StopPropagation)
|
|
||||||
|
|
||||||
coros = list(
|
coros = list(
|
||||||
map(lambda x: x(matcher, exceptions, bot, event, state),
|
map(lambda x: x(matcher, exception, bot, event, state),
|
||||||
_run_postprocessors))
|
_run_postprocessors))
|
||||||
if coros:
|
if coros:
|
||||||
try:
|
try:
|
||||||
@ -101,11 +166,24 @@ async def _run_matcher(Matcher: Type[Matcher], bot: Bot, event: Event,
|
|||||||
"<r><bg #f8bbd0>Error when running RunPostProcessors</bg #f8bbd0></r>"
|
"<r><bg #f8bbd0>Error when running RunPostProcessors</bg #f8bbd0></r>"
|
||||||
)
|
)
|
||||||
|
|
||||||
if exceptions:
|
if matcher.block:
|
||||||
raise _ExceptionContainer(exceptions)
|
raise StopPropagation
|
||||||
|
|
||||||
|
|
||||||
async def handle_event(bot: Bot, event: Event):
|
async def handle_event(bot: Bot, event: Event):
|
||||||
|
"""
|
||||||
|
:说明:
|
||||||
|
处理一个事件。调用该函数以实现分发事件。
|
||||||
|
:参数:
|
||||||
|
* ``bot: Bot``: Bot 对象
|
||||||
|
* ``event: Event``: Event 对象
|
||||||
|
:示例:
|
||||||
|
|
||||||
|
.. code-block:: python
|
||||||
|
|
||||||
|
import asyncio
|
||||||
|
asyncio.create_task(handle_event(bot, event))
|
||||||
|
"""
|
||||||
show_log = True
|
show_log = True
|
||||||
log_msg = f"<m>{bot.type.upper()} </m>| {event.self_id} [{event.name}]: "
|
log_msg = f"<m>{bot.type.upper()} </m>| {event.self_id} [{event.name}]: "
|
||||||
if event.type == "message":
|
if event.type == "message":
|
||||||
@ -153,29 +231,23 @@ async def handle_event(bot: Bot, event: Event):
|
|||||||
if break_flag:
|
if break_flag:
|
||||||
break
|
break
|
||||||
|
|
||||||
pending_tasks = [
|
|
||||||
_run_matcher(matcher, bot, event, state.copy())
|
|
||||||
for matcher in matchers[priority]
|
|
||||||
]
|
|
||||||
|
|
||||||
if show_log:
|
if show_log:
|
||||||
logger.debug(f"Checking for matchers in priority {priority}...")
|
logger.debug(f"Checking for matchers in priority {priority}...")
|
||||||
|
|
||||||
|
run_matchers = await _check_matcher(priority, bot, event, state)
|
||||||
|
|
||||||
|
pending_tasks = [
|
||||||
|
_run_matcher(matcher, bot, event, state.copy())
|
||||||
|
for matcher in run_matchers
|
||||||
|
]
|
||||||
|
|
||||||
results = await asyncio.gather(*pending_tasks, return_exceptions=True)
|
results = await asyncio.gather(*pending_tasks, return_exceptions=True)
|
||||||
|
|
||||||
i = 0
|
for result in results:
|
||||||
for index, result in enumerate(results):
|
if result is StopPropagation:
|
||||||
if isinstance(result, _ExceptionContainer):
|
if not break_flag:
|
||||||
e_list = result.exceptions
|
break_flag = True
|
||||||
if StopPropagation in e_list:
|
logger.debug("Stop event propagation")
|
||||||
if not break_flag:
|
|
||||||
break_flag = True
|
|
||||||
logger.debug("Stop event propagation")
|
|
||||||
if ExpiredException in e_list:
|
|
||||||
logger.debug(
|
|
||||||
f"Matcher {matchers[priority][index - i]} will be removed."
|
|
||||||
)
|
|
||||||
del matchers[priority][index - i]
|
|
||||||
i += 1
|
|
||||||
|
|
||||||
coros = list(map(lambda x: x(bot, event, state), _event_postprocessors))
|
coros = list(map(lambda x: x(bot, event, state), _event_postprocessors))
|
||||||
if coros:
|
if coros:
|
||||||
|
@ -119,14 +119,14 @@ RunPreProcessor = Callable[["Matcher", Bot, Event, dict],
|
|||||||
|
|
||||||
事件响应器运行前预处理函数 RunPreProcessor 类型
|
事件响应器运行前预处理函数 RunPreProcessor 类型
|
||||||
"""
|
"""
|
||||||
RunPostProcessor = Callable[["Matcher", List[Any], Bot, Event, dict],
|
RunPostProcessor = Callable[["Matcher", Optional[Exception], Bot, Event, dict],
|
||||||
Union[Awaitable[None], Awaitable[NoReturn]]]
|
Union[Awaitable[None], Awaitable[NoReturn]]]
|
||||||
"""
|
"""
|
||||||
:类型: ``Callable[[Matcher, List[Any], Bot, Event, dict], Union[Awaitable[None], Awaitable[NoReturn]]]``
|
:类型: ``Callable[[Matcher, Optional[Exception], Bot, Event, dict], Union[Awaitable[None], Awaitable[NoReturn]]]``
|
||||||
|
|
||||||
:说明:
|
:说明:
|
||||||
|
|
||||||
事件响应器运行前预处理函数 RunPostProcessor 类型,第二个参数包含运行时产生的错误以及 ``ExpiredException``, ``StopPropagation`` (如果存在)
|
事件响应器运行前预处理函数 RunPostProcessor 类型,第二个参数为运行时产生的错误(如果存在)
|
||||||
"""
|
"""
|
||||||
|
|
||||||
Matcher = TypeVar("Matcher", bound="MatcherClass")
|
Matcher = TypeVar("Matcher", bound="MatcherClass")
|
||||||
|
Loading…
Reference in New Issue
Block a user