From b3e6d1c8030ac6f8216b2eaf7286d5f92b4ef24e Mon Sep 17 00:00:00 2001 From: yanyongyu Date: Mon, 16 Nov 2020 15:06:37 +0800 Subject: [PATCH] :bulb: add message docstring --- nonebot/message.py | 58 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 58 insertions(+) diff --git a/nonebot/message.py b/nonebot/message.py index 942ea3e8..c000d39a 100644 --- a/nonebot/message.py +++ b/nonebot/message.py @@ -1,6 +1,8 @@ """ 事件处理 ======== + +NoneBot 内部处理并按优先级分发事件给所有事件响应器,提供了多个插槽以进行事件的预处理等。 """ import asyncio @@ -21,21 +23,64 @@ _run_postprocessors: Set[RunPostProcessor] = set() def event_preprocessor(func: EventPreProcessor) -> EventPreProcessor: + """ + :说明: + 事件预处理。装饰一个函数,使它在每次接收到事件并分发给各响应器之前执行。 + :参数: + 事件预处理函数接收三个参数。 + + * ``bot: Bot``: Bot 对象 + * ``event: Event``: Event 对象 + * ``state: dict``: 当前 State + """ _event_preprocessors.add(func) return func def event_postprocessor(func: EventPostProcessor) -> EventPostProcessor: + """ + :说明: + 事件后处理。装饰一个函数,使它在每次接收到事件并分发给各响应器之后执行。 + :参数: + 事件后处理函数接收三个参数。 + + * ``bot: Bot``: Bot 对象 + * ``event: Event``: Event 对象 + * ``state: dict``: 当前事件运行前 State + """ _event_postprocessors.add(func) return func def run_preprocessor(func: RunPreProcessor) -> RunPreProcessor: + """ + :说明: + 运行预处理。装饰一个函数,使它在每次事件响应器运行前执行。 + :参数: + 运行预处理函数接收四个参数。 + + * ``matcher: Matcher``: 当前要运行的事件响应器 + * ``bot: Bot``: Bot 对象 + * ``event: Event``: Event 对象 + * ``state: dict``: 当前 State + """ _run_preprocessors.add(func) return func def run_postprocessor(func: RunPostProcessor) -> RunPostProcessor: + """ + :说明: + 运行后处理。装饰一个函数,使它在每次事件响应器运行后执行。 + :参数: + 运行后处理函数接收五个参数。 + + * ``matcher: Matcher``: 运行完毕的事件响应器 + * ``exception: Optional[Exception]``: 事件响应器运行错误(如果存在) + * ``bot: Bot``: Bot 对象 + * ``event: Event``: Event 对象 + * ``state: dict``: 当前 State + """ _run_postprocessors.add(func) return func @@ -126,6 +171,19 @@ async def _run_matcher(Matcher: Type[Matcher], 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 log_msg = f"{bot.type.upper()} | {event.self_id} [{event.name}]: " if event.type == "message":