Skip to content

Commit

Permalink
✨ version 0.31.7
Browse files Browse the repository at this point in the history
optional argument
  • Loading branch information
RF-Tar-Railt committed Nov 5, 2023
1 parent 0e2e550 commit 65b8340
Show file tree
Hide file tree
Showing 6 changed files with 58 additions and 31 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 @@ -96,7 +96,7 @@
from .consts import ALCONNA_EXEC_RESULT as ALCONNA_EXEC_RESULT
from .extension import add_global_extension as add_global_extension

__version__ = "0.31.6"
__version__ = "0.31.7"

__plugin_meta__ = PluginMetadata(
name="Alconna 插件",
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 @@ -32,7 +32,7 @@
from .params import UniversalMessage as UniversalMessage
from .params import UniversalSegment as UniversalSegment

__version__ = "0.31.6"
__version__ = "0.31.7"

__plugin_meta__ = PluginMetadata(
name="Universal Segment 插件",
Expand Down
1 change: 1 addition & 0 deletions src/nonebot_plugin_alconna/uniseg/i18n/en-US.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
"failed_segment": "Cannot serialize {seg!r} to {target} segment of adatper {adapter}",
"failed": "Cannot serialize {target!r} to message of adapter {adapter}",
"bot_missing": "Can not export message without bot instance",
"event_missing": "Can not export message without event instance",
"unsupported": "Message Exporter not found under adapter {adapter}"
}
}
1 change: 1 addition & 0 deletions src/nonebot_plugin_alconna/uniseg/i18n/zh-CN.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
"failed_segment": "无法将 {seg!r} 序列化为适配器 {adapter} 下的 {target} 消息段",
"failed": "无法将 {target!r} 序列化为适配器 {adapter} 下的消息",
"bot_missing": "在没有机器人实例的情况下无法将通用消息转为对应的适配器消息",
"event_missing": "在没有事件实例的情况下无法将通用消息转为对应的适配器消息",
"unsupported": "未找到适配器 {adapter} 下的消息转换器"
}
}
79 changes: 52 additions & 27 deletions src/nonebot_plugin_alconna/uniseg/message.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
from typing import TYPE_CHECKING, List, Type, Tuple, Union, Literal, TypeVar, Iterable, Optional, overload

from tarina import lang
from nonebot.internal.matcher import current_bot
from nonebot.internal.adapter import Bot, Event, Message
from nonebot.internal.matcher import current_bot, current_event

from .receipt import Receipt
from .adapters import MAPPING
Expand Down Expand Up @@ -740,18 +740,22 @@ def extract_plain_text(self) -> str:
async def generate(
*, message: Optional[Message] = None, event: Optional[Event] = None, bot: Optional[Bot] = None
):
if message is None and not event:
raise ValueError("No message or event given")
try:
msg = event.get_message() if message is None else message # type: ignore
except Exception:
return UniMessage()
if not message:
if not event:
try:
event = current_event.get()
except LookupError as e:
raise SerializeFailed(lang.require("nbp-uniseg", "event_missing")) from e
try:
message = event.get_message()
except Exception:
return UniMessage()
result = UniMessage()
msg_copy = msg.copy()
msg_copy = message.copy()
if (event and bot) and (_reply := await reply_handle(event, bot)):
result.append(_reply)
elif (res := reply.validate(msg[0])).success:
res.value.origin = msg[0]
elif (res := reply.validate(message[0])).success:
res.value.origin = message[0]
result.append(res.value)
msg_copy.pop(0)
for seg in msg_copy:
Expand All @@ -765,30 +769,46 @@ async def generate(
return result

@staticmethod
def get_message_id(event: Event, bot: Optional[Bot] = None) -> str:
if not bot:
def get_message_id(
event: Optional[Event] = None, bot: Optional[Bot] = None, adapter: Optional[str] = None
) -> str:
if not event:
try:
bot = current_bot.get()
event = current_event.get()
except LookupError as e:
raise SerializeFailed(lang.require("nbp-uniseg", "bot_missing")) from e
adapter = bot.adapter
adapter_name = adapter.get_name()
if fn := MAPPING.get(adapter_name):
raise SerializeFailed(lang.require("nbp-uniseg", "event_missing")) from e
if not adapter:
if not bot:
try:
bot = current_bot.get()
except LookupError as e:
raise SerializeFailed(lang.require("nbp-uniseg", "bot_missing")) from e
_adapter = bot.adapter
adapter = _adapter.get_name()
if fn := MAPPING.get(adapter):
return fn.get_message_id(event)
raise SerializeFailed(lang.require("nbp-uniseg", "unsupported").format(adapter=adapter_name))
raise SerializeFailed(lang.require("nbp-uniseg", "unsupported").format(adapter=adapter))

@staticmethod
def get_target(event: Event, bot: Optional[Bot] = None) -> Target:
if not bot:
def get_target(
event: Optional[Event] = None, bot: Optional[Bot] = None, adapter: Optional[str] = None
) -> Target:
if not event:
try:
bot = current_bot.get()
event = current_event.get()
except LookupError as e:
raise SerializeFailed(lang.require("nbp-uniseg", "bot_missing")) from e
adapter = bot.adapter
adapter_name = adapter.get_name()
if fn := MAPPING.get(adapter_name):
raise SerializeFailed(lang.require("nbp-uniseg", "event_missing")) from e
if not adapter:
if not bot:
try:
bot = current_bot.get()
except LookupError as e:
raise SerializeFailed(lang.require("nbp-uniseg", "bot_missing")) from e
_adapter = bot.adapter
adapter = _adapter.get_name()
if fn := MAPPING.get(adapter):
return fn.get_target(event)
raise SerializeFailed(lang.require("nbp-uniseg", "unsupported").format(adapter=adapter_name))
raise SerializeFailed(lang.require("nbp-uniseg", "unsupported").format(adapter=adapter))

async def export(self, bot: Optional[Bot] = None, fallback: bool = True) -> Message:
if not bot:
Expand All @@ -809,7 +829,7 @@ async def export(self, bot: Optional[Bot] = None, fallback: bool = True) -> Mess

async def send(
self,
target: Union[Event, Target],
target: Union[Event, Target, None] = None,
bot: Optional[Bot] = None,
fallback: bool = True,
at_sender: Union[str, bool] = False,
Expand All @@ -820,6 +840,11 @@ async def send(
bot = current_bot.get()
except LookupError as e:
raise SerializeFailed(lang.require("nbp-uniseg", "bot_missing")) from e
if not target:
try:
target = current_event.get()
except LookupError as e:
raise SerializeFailed(lang.require("nbp-uniseg", "event_missing")) from e
if at_sender:
if isinstance(at_sender, str):
self.insert(0, At("user", at_sender)) # type: ignore
Expand Down
4 changes: 2 additions & 2 deletions tests/test_uniseg.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,8 @@ async def test_unimsg_send(app: App):
matcher = on_alconna(Alconna("test_unimsg_send"))

@matcher.handle()
async def handle(_bot: Bot, _event: Event, msg: MsgId):
receipt = await UniMessage("hello!").send(_event, _bot, at_sender=True, reply_to=msg)
async def handle(msg: MsgId):
receipt = await UniMessage("hello!").send(at_sender=True, reply_to=msg)
receipt.msg_ids[0] = {"message_id": 2}
await receipt.recall(1)

Expand Down

0 comments on commit 65b8340

Please sign in to comment.