mirror of
https://github.com/nonebot/nonebot2.git
synced 2025-01-19 17:58:26 +08:00
Add message preprocessor
This commit is contained in:
parent
8c8d939727
commit
eedef32f7d
@ -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 (
|
||||
|
@ -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
|
||||
|
Loading…
Reference in New Issue
Block a user