From 2e75671d5617297a951b19748893d78afefa9e27 Mon Sep 17 00:00:00 2001 From: yanyongyu Date: Tue, 26 Jan 2021 15:03:37 +0800 Subject: [PATCH] :alembic: add processor to ensure single matcher --- nonebot/plugins/single_session.py | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/nonebot/plugins/single_session.py b/nonebot/plugins/single_session.py index 20eca034..8516b0c1 100644 --- a/nonebot/plugins/single_session.py +++ b/nonebot/plugins/single_session.py @@ -5,16 +5,22 @@ from nonebot.matcher import Matcher from nonebot.adapters import Bot, Event from nonebot.message import run_preprocessor, run_postprocessor, IgnoredException -_running_matcher: Dict[str, bool] = {} +_running_matcher: Dict[str, int] = {} @run_preprocessor async def _(matcher: Matcher, bot: Bot, event: Event, state: T_State): - # TODO - pass + session_id = event.get_session_id() + event_id = id(event) + + if _running_matcher.get(session_id, None) != event_id: + raise IgnoredException("Annother matcher running") + + _running_matcher[session_id] = event_id @run_postprocessor async def _(matcher: Matcher, exception: Optional[Exception], bot: Bot, event: Event, state: T_State): - # TODO - pass + session_id = event.get_session_id() + if session_id in _running_matcher: + del _running_matcher[session_id]