From eedef32f7d47e161b9ce5dc261b0925a30ac4605 Mon Sep 17 00:00:00 2001 From: Richard Chien Date: Mon, 30 Jul 2018 23:41:19 +0800 Subject: [PATCH] Add message preprocessor --- none/__init__.py | 1 + none/message.py | 16 +++++++++++++++- 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/none/__init__.py b/none/__init__.py index 279743c4..24a331d6 100644 --- a/none/__init__.py +++ b/none/__init__.py @@ -127,6 +127,7 @@ def load_builtin_plugins() -> None: load_plugins(plugin_dir, 'none.plugins') +from .message import message_preprocessor from .command import on_command, CommandSession, CommandGroup from .natural_language import on_natural_language, NLPSession, NLPResult from .notice_request import ( diff --git a/none/message.py b/none/message.py index 525e350a..00dd5be3 100644 --- a/none/message.py +++ b/none/message.py @@ -1,4 +1,5 @@ -from typing import Dict, Any +import asyncio +from typing import Dict, Any, Callable from aiocqhttp.message import MessageSegment @@ -7,10 +8,23 @@ from .command import handle_command, SwitchException from .log import logger from .natural_language import handle_natural_language +_message_preprocessors = set() + + +def message_preprocessor(func: Callable) -> Callable: + _message_preprocessors.add(func) + return func + async def handle_message(bot: NoneBot, ctx: Dict[str, Any]) -> None: _log_message(ctx) + coros = [] + for processor in _message_preprocessors: + coros.append(processor(ctx)) + if coros: + await asyncio.wait(coros) + if ctx['message_type'] != 'private': # group or discuss ctx['to_me'] = False