2021-01-25 18:15:25 +08:00
|
|
|
from typing import Dict, Optional
|
|
|
|
|
|
|
|
from nonebot.typing import T_State
|
2021-11-16 18:30:16 +08:00
|
|
|
from nonebot.matcher import Matcher
|
2021-01-25 18:15:25 +08:00
|
|
|
from nonebot.adapters import Bot, Event
|
2021-09-27 00:19:30 +08:00
|
|
|
from nonebot.message import (IgnoredException, run_preprocessor,
|
|
|
|
run_postprocessor)
|
2021-01-25 18:15:25 +08:00
|
|
|
|
2021-01-26 15:03:37 +08:00
|
|
|
_running_matcher: Dict[str, int] = {}
|
2021-01-25 18:15:25 +08:00
|
|
|
|
|
|
|
|
|
|
|
@run_preprocessor
|
2021-11-16 18:30:16 +08:00
|
|
|
async def preprocess(event: Event):
|
2021-02-24 22:21:47 +08:00
|
|
|
try:
|
|
|
|
session_id = event.get_session_id()
|
2021-02-24 23:56:52 +08:00
|
|
|
except Exception:
|
2021-02-24 22:21:47 +08:00
|
|
|
return
|
|
|
|
current_event_id = id(event)
|
|
|
|
event_id = _running_matcher.get(session_id, None)
|
|
|
|
if event_id and event_id != current_event_id:
|
2021-03-11 10:55:18 +08:00
|
|
|
raise IgnoredException("Another matcher running")
|
2021-01-26 15:03:37 +08:00
|
|
|
|
2021-02-24 22:21:47 +08:00
|
|
|
_running_matcher[session_id] = current_event_id
|
2021-01-25 18:15:25 +08:00
|
|
|
|
|
|
|
|
|
|
|
@run_postprocessor
|
2021-11-16 18:30:16 +08:00
|
|
|
async def postprocess(event: Event):
|
2021-02-24 22:21:47 +08:00
|
|
|
try:
|
|
|
|
session_id = event.get_session_id()
|
2021-02-24 23:56:52 +08:00
|
|
|
except Exception:
|
2021-02-24 22:21:47 +08:00
|
|
|
return
|
2021-01-26 15:03:37 +08:00
|
|
|
if session_id in _running_matcher:
|
|
|
|
del _running_matcher[session_id]
|