From ba06145ca27a469eda74b673ab2d4be3c6a696d3 Mon Sep 17 00:00:00 2001 From: RF-Tar-Railt <3165388245@qq.com> Date: Fri, 27 Oct 2023 20:37:39 +0800 Subject: [PATCH] :bug: version 0.30.7 fix skip_for_unmatch --- src/nonebot_plugin_alconna/__init__.py | 2 +- src/nonebot_plugin_alconna/extension.py | 5 +++ src/nonebot_plugin_alconna/rule.py | 4 ++ src/nonebot_plugin_alconna/uniseg/__init__.py | 2 +- tests/test_unmatch.py | 39 +++++++++++++++++++ 5 files changed, 50 insertions(+), 2 deletions(-) create mode 100644 tests/test_unmatch.py diff --git a/src/nonebot_plugin_alconna/__init__.py b/src/nonebot_plugin_alconna/__init__.py index 710bdc3..ecc856c 100644 --- a/src/nonebot_plugin_alconna/__init__.py +++ b/src/nonebot_plugin_alconna/__init__.py @@ -89,7 +89,7 @@ from .consts import ALCONNA_EXEC_RESULT as ALCONNA_EXEC_RESULT from .extension import add_global_extension as add_global_extension -__version__ = "0.30.6" +__version__ = "0.30.7" __plugin_meta__ = PluginMetadata( name="Alconna 插件", diff --git a/src/nonebot_plugin_alconna/extension.py b/src/nonebot_plugin_alconna/extension.py index 4531c6c..8b8bdd8 100644 --- a/src/nonebot_plugin_alconna/extension.py +++ b/src/nonebot_plugin_alconna/extension.py @@ -40,6 +40,7 @@ class Extension(metaclass=ABCMeta): def __init_subclass__(cls, **kwargs): cls._overrides = { + "output_converter": cls.output_converter != Extension.output_converter, "send_wrapper": cls.send_wrapper != Extension.send_wrapper, "receive_wrapper": cls.receive_wrapper != Extension.receive_wrapper, "permission_check": cls.permission_check != Extension.permission_check, @@ -184,10 +185,14 @@ def select(self, bot: Bot, event: Event) -> Self: async def output_converter(self, output_type: OutputType, content: str) -> Message | UniMessage: exc = None for ext in self.context: + if not ext._overrides["output_converter"]: + continue try: return await ext.output_converter(output_type, content) except Exception as e: exc = e + if not exc: + return FallbackMessage() raise exc # type: ignore async def message_provider( diff --git a/src/nonebot_plugin_alconna/rule.py b/src/nonebot_plugin_alconna/rule.py index c1ec8d8..dd1f139 100644 --- a/src/nonebot_plugin_alconna/rule.py +++ b/src/nonebot_plugin_alconna/rule.py @@ -249,6 +249,8 @@ async def __call__(self, event: Event, state: T_State, bot: Bot) -> bool: arp = Arparma(self.command.path, msg, False, error_info=e) may_help_text: Optional[str] = cap.get("output", None) self._session = None + if not arp.head_matched: + return False if not arp.matched and not may_help_text and self.skip: log( "TRACE", @@ -288,6 +290,8 @@ async def send(self, text: str, bot: Bot, event: Event, arp: Arparma) -> Message _t = str(arp.error_info) if isinstance(arp.error_info, SpecialOptionTriggered) else "help" try: msg = await self.executor.output_converter(_t, text) # type: ignore + if not msg: + return await bot.send(event, text) if isinstance(msg, UniMessage): msg = await msg.export(bot, fallback=True) return await bot.send(event, msg) # type: ignore diff --git a/src/nonebot_plugin_alconna/uniseg/__init__.py b/src/nonebot_plugin_alconna/uniseg/__init__.py index 0ecbb39..2a2e00f 100644 --- a/src/nonebot_plugin_alconna/uniseg/__init__.py +++ b/src/nonebot_plugin_alconna/uniseg/__init__.py @@ -24,7 +24,7 @@ from .params import UniversalMessage as UniversalMessage from .params import UniversalSegment as UniversalSegment -__version__ = "0.30.6" +__version__ = "0.30.7" __plugin_meta__ = PluginMetadata( name="Universal Segment 插件", diff --git a/tests/test_unmatch.py b/tests/test_unmatch.py new file mode 100644 index 0000000..a75223f --- /dev/null +++ b/tests/test_unmatch.py @@ -0,0 +1,39 @@ +from typing import Union + +import pytest +from nonebug import App +from nonebot import get_adapter +from arclet.alconna import Args, Alconna +from nonebot.adapters.onebot.v11 import Bot, Adapter, Message + +from tests.fake import fake_group_message_event_v11 + + +@pytest.mark.asyncio() +async def test_unmatch(app: App): + from nonebot_plugin_alconna import At, Match, UniMessage, on_alconna + + test_cmd = on_alconna( + Alconna("test", Args["target", Union[int, At]]), skip_for_unmatch=False, auto_send_output=True + ) + + @test_cmd.handle() + async def tt_h(target: Match[Union[int, At]]): + await test_cmd.send(UniMessage(["ok\n", str(target.result)])) + + async with app.test_matcher(test_cmd) as ctx: + adapter = get_adapter(Adapter) + bot = ctx.create_bot(base=Bot, adapter=adapter) + + event = fake_group_message_event_v11(message=Message("tes 1234"), user_id=123) + ctx.receive_event(bot, event) + ctx.should_not_pass_rule() + + event = fake_group_message_event_v11(message=Message("test 1234"), user_id=123) + ctx.receive_event(bot, event) + ctx.should_call_send(event, Message("ok\n1234")) + + event = fake_group_message_event_v11(message=Message("test abcd"), user_id=123) + ctx.receive_event(bot, event) + ctx.should_not_pass_rule() + ctx.should_call_send(event, "ParamsUnmatched('参数 abcd 不正确')", bot=bot)