Skip to content

Commit

Permalink
🐛 fix twice send wrapper for matcher.send
Browse files Browse the repository at this point in the history
  • Loading branch information
RF-Tar-Railt committed Jul 18, 2024
1 parent 7843c55 commit 148254e
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 4 deletions.
2 changes: 1 addition & 1 deletion src/nonebot_plugin_alconna/matcher.py
Original file line number Diff line number Diff line change
Expand Up @@ -639,7 +639,7 @@ async def send(
event = current_event.get()
_message = await cls.executor.send_wrapper(bot, event, cls.convert(message))
if isinstance(_message, UniMessage):
return await _message.send(target=event, bot=bot, fallback=fallback, **kwargs)
return await _message.send(target=event, bot=bot, fallback=fallback, no_wrapper=True, **kwargs)
else:
res = _message
return await bot.send(event=event, message=res, **kwargs)
Expand Down
3 changes: 2 additions & 1 deletion src/nonebot_plugin_alconna/uniseg/message.py
Original file line number Diff line number Diff line change
Expand Up @@ -1297,6 +1297,7 @@ async def send(
fallback: Union[bool, FallbackStrategy] = FallbackStrategy.rollback,
at_sender: Union[str, bool] = False,
reply_to: Union[str, bool, Reply, None] = False,
no_wrapper: bool = False,
**kwargs,
) -> "Receipt":
if not target:
Expand Down Expand Up @@ -1331,7 +1332,7 @@ async def send(
else:
raise TypeError("reply_to must be str when target is not Event")
self.insert(0, Reply(reply_to)) # type: ignore
if (wrapper := current_send_wrapper.get(None)) and isinstance(target, Event):
if not no_wrapper and isinstance(target, Event) and (wrapper := current_send_wrapper.get(None)):
self[:] = await wrapper(bot, target, self)
msg = await self.export(bot, fallback)
adapter = bot.adapter
Expand Down
12 changes: 10 additions & 2 deletions tests/test_extension.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from nonebug import App
from nonebot import get_adapter
from arclet.alconna import Args, Alconna
from nonebot.internal.adapter import Event
from nonebot.adapters.onebot.v11 import Bot, Adapter, Message

from tests.fake import fake_group_message_event_v11
Expand All @@ -13,7 +14,7 @@
async def test_extension(app: App):
from nonebot.adapters.onebot.v11 import MessageEvent

from nonebot_plugin_alconna import Extension, Interface, on_alconna
from nonebot_plugin_alconna import Extension, Interface, UniMessage, on_alconna

class DemoExtension(Extension):
@property
Expand All @@ -35,6 +36,11 @@ def before_catch(self, name: str, annotation: Any, default: Any) -> bool:
return True
return False

async def send_wrapper(self, bot: Bot, event: Event, send):
if isinstance(send, UniMessage):
return send + "456"
return send + "123"

async def catch(self, interface: Interface[MessageEvent]):
if interface.annotation is str:
return {
Expand All @@ -48,13 +54,15 @@ async def catch(self, interface: Interface[MessageEvent]):
async def h(a: float, b: float, hello: str, world: str, test: str):
"""加法测试"""
await add.send(f"{a} + {b} = {a + b}\n{hello} {world} {test}!")
await UniMessage(f"{a} + {b} = {a + b}\n{hello} {world} {test}!").send()

async with app.test_matcher(add) as ctx: # type: ignore
adapter = get_adapter(Adapter)
bot = ctx.create_bot(base=Bot, adapter=adapter)
event = fake_group_message_event_v11(message=Message("add 1.3 2.4"), user_id=123)
ctx.receive_event(bot, event)
ctx.should_call_send(event, "1.3 + 2.4 = 3.7\nHello! World! test!")
ctx.should_call_send(event, "1.3 + 2.4 = 3.7\nHello! World! test!123")
ctx.should_call_send(event, Message("1.3 + 2.4 = 3.7\nHello! World! test!456"))

event = fake_group_message_event_v11(message=Message("add 1.3 2.4"), user_id=456)
ctx.receive_event(bot, event)
Expand Down

0 comments on commit 148254e

Please sign in to comment.