Add message preprocessor

This commit is contained in:
Richard Chien 2018-07-30 23:41:19 +08:00
parent 8c8d939727
commit eedef32f7d
2 changed files with 16 additions and 1 deletions

View File

@ -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 (

View File

@ -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