Skip to content

Commit

Permalink
🐛 version 0.30.7
Browse files Browse the repository at this point in the history
fix skip_for_unmatch
  • Loading branch information
RF-Tar-Railt committed Oct 27, 2023
1 parent 402214a commit ba06145
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 2 deletions.
2 changes: 1 addition & 1 deletion src/nonebot_plugin_alconna/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 插件",
Expand Down
5 changes: 5 additions & 0 deletions src/nonebot_plugin_alconna/extension.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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(
Expand Down
4 changes: 4 additions & 0 deletions src/nonebot_plugin_alconna/rule.py
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion src/nonebot_plugin_alconna/uniseg/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 插件",
Expand Down
39 changes: 39 additions & 0 deletions tests/test_unmatch.py
Original file line number Diff line number Diff line change
@@ -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)

0 comments on commit ba06145

Please sign in to comment.