From 06c33ad6eaa942a7c2b7408bf58f6f8037944128 Mon Sep 17 00:00:00 2001 From: Akirami <66513481+A-kirami@users.noreply.github.com> Date: Tue, 6 Dec 2022 14:19:48 +0800 Subject: [PATCH] =?UTF-8?q?:sparkles:=20Feature:=20=E6=94=AF=E6=8C=81?= =?UTF-8?q?=E4=B8=BB=E5=8A=A8=E9=94=80=E6=AF=81=E4=BA=8B=E4=BB=B6=E5=93=8D?= =?UTF-8?q?=E5=BA=94=E5=99=A8=20(#1444)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> --- nonebot/internal/matcher/matcher.py | 5 +++++ nonebot/message.py | 7 +++---- tests/test_matcher/test_matcher.py | 12 +++--------- 3 files changed, 11 insertions(+), 13 deletions(-) diff --git a/nonebot/internal/matcher/matcher.py b/nonebot/internal/matcher/matcher.py index c6cb0824..3c92ac07 100644 --- a/nonebot/internal/matcher/matcher.py +++ b/nonebot/internal/matcher/matcher.py @@ -247,6 +247,11 @@ class Matcher(metaclass=MatcherMeta): return NewMatcher + @classmethod + def destroy(cls) -> None: + """销毁当前的事件响应器""" + matchers[cls.priority].remove(cls) + @classmethod async def check_perm( cls, diff --git a/nonebot/message.py b/nonebot/message.py index 9203d43b..a7189058 100644 --- a/nonebot/message.py +++ b/nonebot/message.py @@ -112,7 +112,6 @@ def run_postprocessor(func: T_RunPostProcessor) -> T_RunPostProcessor: async def _check_matcher( - priority: int, Matcher: Type[Matcher], bot: "Bot", event: "Event", @@ -122,7 +121,7 @@ async def _check_matcher( ) -> None: if Matcher.expire_time and datetime.now() > Matcher.expire_time: with contextlib.suppress(Exception): - matchers[priority].remove(Matcher) + Matcher.destroy() return try: @@ -138,7 +137,7 @@ async def _check_matcher( if Matcher.temp: with contextlib.suppress(Exception): - matchers[priority].remove(Matcher) + Matcher.destroy() await _run_matcher(Matcher, bot, event, state, stack, dependency_cache) @@ -294,7 +293,7 @@ async def handle_event(bot: "Bot", event: "Event") -> None: pending_tasks = [ _check_matcher( - priority, matcher, bot, event, state.copy(), stack, dependency_cache + matcher, bot, event, state.copy(), stack, dependency_cache ) for matcher in matchers[priority] ] diff --git a/tests/test_matcher/test_matcher.py b/tests/test_matcher/test_matcher.py index 0e88179f..1a942937 100644 --- a/tests/test_matcher/test_matcher.py +++ b/tests/test_matcher/test_matcher.py @@ -187,25 +187,19 @@ async def test_expire(app: App, load_plugin): async with app.test_api() as ctx: bot = ctx.create_bot() assert test_temp_matcher in matchers[test_temp_matcher.priority] - await _check_matcher( - test_temp_matcher.priority, test_temp_matcher, bot, event, {} - ) + await _check_matcher(test_temp_matcher, bot, event, {}) assert test_temp_matcher not in matchers[test_temp_matcher.priority] event = make_fake_event()() async with app.test_api() as ctx: bot = ctx.create_bot() assert test_datetime_matcher in matchers[test_datetime_matcher.priority] - await _check_matcher( - test_datetime_matcher.priority, test_datetime_matcher, bot, event, {} - ) + await _check_matcher(test_datetime_matcher, bot, event, {}) assert test_datetime_matcher not in matchers[test_datetime_matcher.priority] event = make_fake_event()() async with app.test_api() as ctx: bot = ctx.create_bot() assert test_timedelta_matcher in matchers[test_timedelta_matcher.priority] - await _check_matcher( - test_timedelta_matcher.priority, test_timedelta_matcher, bot, event, {} - ) + await _check_matcher(test_timedelta_matcher, bot, event, {}) assert test_timedelta_matcher not in matchers[test_timedelta_matcher.priority]