Skip to content

Commit

Permalink
✨ 适配alconna的uniseg
Browse files Browse the repository at this point in the history
  • Loading branch information
AzideCupric committed Oct 29, 2023
1 parent 3dc3214 commit a17d69a
Show file tree
Hide file tree
Showing 4 changed files with 219 additions and 85 deletions.
57 changes: 57 additions & 0 deletions nonebot_plugin_saa/ext/uniseg.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
try:
from nonebot_plugin_alconna.uniseg import Other, Segment
from nonebot_plugin_alconna.uniseg import UniMessage as UniMsg
from nonebot_plugin_alconna.uniseg.adapters import MAPPING as alc_exportor_map
except ImportError as e:
raise ImportError(

Check warning on line 6 in nonebot_plugin_saa/ext/uniseg.py

View check run for this annotation

Codecov / codecov/patch

nonebot_plugin_saa/ext/uniseg.py#L5-L6

Added lines #L5 - L6 were not covered by tests
"请使用 `pip install nonebot-plugin-send-anything-anywhere[alc]` 安装所需依赖"
) from e

from typing_extensions import override
from typing import Union, TypeVar, Iterable

from nonebot.adapters import Bot, Message, MessageSegment

from ..utils import extract_adapter_type
from ..abstract_factories import MessageFactory, MessageSegmentFactory

UMS = TypeVar("UMS", bound=Segment)


class UniSegmentUnsupport(Exception):
...


class UniMessageSegmentFactory(MessageSegmentFactory):
data: Segment

def __init__(self, uniseg: Segment) -> None:
self.data = uniseg

async def build(self, bot: Bot) -> MessageSegment:
adapter_name = extract_adapter_type(bot)
seg_type = self.data.__class__
export_res = None
if exportor := alc_exportor_map.get(adapter_name):
exportor.segment_class = exportor.get_message_type().get_segment_class()
export_res = await exportor._mapping[seg_type](self.data, bot)

if not export_res and isinstance(self.data, Other):
export_res = self.data.origin

Check warning on line 40 in nonebot_plugin_saa/ext/uniseg.py

View check run for this annotation

Codecov / codecov/patch

nonebot_plugin_saa/ext/uniseg.py#L40

Added line #L40 was not covered by tests

if not export_res:
raise UniSegmentUnsupport(self.data)

Check warning on line 43 in nonebot_plugin_saa/ext/uniseg.py

View check run for this annotation

Codecov / codecov/patch

nonebot_plugin_saa/ext/uniseg.py#L43

Added line #L43 was not covered by tests

return export_res


class UniMessageFactory(MessageFactory):
def __init__(
self, message: Union[Iterable[Union[str, UMS]], str, UMS, None] = None
):
self.unimsg = UniMsg(message)
self.extend(map(UniMessageSegmentFactory, self.unimsg))

@override
async def build(self, bot: Bot) -> Message:
return await self.unimsg.export(bot)

Check warning on line 57 in nonebot_plugin_saa/ext/uniseg.py

View check run for this annotation

Codecov / codecov/patch

nonebot_plugin_saa/ext/uniseg.py#L57

Added line #L57 was not covered by tests
Loading

0 comments on commit a17d69a

Please sign in to comment.