Skip to content

Commit

Permalink
✨ middlewares: get_supported_message_segments
Browse files Browse the repository at this point in the history
  • Loading branch information
j1g5awi committed Aug 19, 2024
1 parent c2c7d30 commit dd00f06
Show file tree
Hide file tree
Showing 5 changed files with 82 additions and 6 deletions.
12 changes: 12 additions & 0 deletions nonebot_plugin_all4one/middlewares/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,10 @@ def get_bot_self(self) -> BotSelf:
return BotSelf(
platform=self.get_platform(),
user_id=self.self_id,
**{
"supported_actions": self.get_supported_actions(),
"supported_message_segments": self.get_supported_message_segments(),
},
)

@classmethod
Expand All @@ -74,6 +78,14 @@ def get_platform(self) -> str:
async def to_onebot_event(self, event: Event) -> list[OneBotEvent]:
raise NotImplementedError

@supported_action
async def get_supported_message_segments(self, **kwargs: Any) -> list[str]:
"""获取支持的消息段列表
参数:
kwargs: 扩展字段
"""
return []

async def send_message(
self,
*,
Expand Down
10 changes: 10 additions & 0 deletions nonebot_plugin_all4one/middlewares/discord.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,16 @@ async def to_onebot_event(self, event: Event) -> list[OneBotEvent]:
return [event_out]
return []

async def get_supported_message_segments(self, **kwargs: Any) -> list[str]:
return [
"text",
"mention",
"mention_all",
"image",
"file",
"reply",
]

async def to_onebot_message(self, event: MessageEvent) -> OneBotMessage:
message_list = []
for segment in event.original_message:
Expand Down
46 changes: 46 additions & 0 deletions nonebot_plugin_all4one/middlewares/onebot_v11.py
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,17 @@ async def to_onebot_event(self, event: Event) -> list[OneBotEvent]:
return [event_out]
return []

async def get_supported_message_segments(self, **kwargs: Any) -> list[str]:
return [
"text",
"mention",
"mention_all",
"image",
"video",
"reply",
"message_nodes",
]

async def to_onebot_message(self, message: Message) -> OneBotMessage:
message_list = []
for segment in message:
Expand All @@ -152,6 +163,24 @@ async def to_onebot_message(self, message: Message) -> OneBotMessage:
url=segment.data["url"],
)
message_list.append(OneBotMessageSegment.image(file_id))
elif segment.type == "forward":
resp = await self.bot.get_forward_msg(id=segment.data["id"])
nodes = []
for node in resp["message"]:
if node.type == "forward":
continue
nodes.append(
{
"user_id": node["data"]["user_id"],
"user_name": node["data"]["nickname"],
"message": await self.to_onebot_message(
node["data"]["content"]
),
}
)
message_list.append(
OneBotMessageSegment("message_nodes", {"nodes": nodes})
)
return OneBotMessage(message_list)

async def from_onebot_message(self, message: OneBotMessage) -> Message:
Expand Down Expand Up @@ -187,6 +216,23 @@ async def from_onebot_message(self, message: OneBotMessage) -> Message:
message_list.append(MessageSegment.record(data))
elif segment.type == "reply":
message_list.append(MessageSegment.reply(segment.data["message_id"]))
elif segment.type == "message_nodes":
nodes = []
for node in segment.data["nodes"]:
nodes.append(
MessageSegment(
"node",
{
"name": node["user_name"],
"uin": node["user_id"],
"content": await self.from_onebot_message(
node["message"]
),
},
)
)
resp = await self.bot.send_forward_msg(meesages=nodes)
message_list.append(MessageSegment.forward(resp["resid"]))
return Message(message_list)

@supported_action
Expand Down
13 changes: 13 additions & 0 deletions nonebot_plugin_all4one/middlewares/telegram.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,19 @@ async def to_onebot_event(self, event: Event) -> list[OneBotEvent]:
return [event_out]
return []

async def get_supported_message_segments(self, **kwargs: Any) -> list[str]:
return [
"text",
"mention",
"mention_all",
"image",
"voice",
"audio",
"video",
"file",
"reply",
]

async def to_onebot_message(self, message: Message) -> OneBotMessage:
message_list = []
for segment in message:
Expand Down
7 changes: 1 addition & 6 deletions nonebot_plugin_all4one/onebotimpl/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,15 +102,14 @@ async def _call_api(self, data: dict[str, Any]) -> Any:
try:
if (api := data["action"]) in (
"get_latest_events",
"get_supported_actions",
"get_status",
"get_version",
):
resp = await getattr(self, api)(**data)
else:
if bot_self := data.pop("self", None):
if middleware := self.middlewares.get(
bot_self.get("user_id", "").replace("a4o@", ""), None
bot_self.get("user_id", ""), None
):
resp = await middleware._call_api(api, **data["params"])
else:
Expand Down Expand Up @@ -542,12 +541,8 @@ async def _():

@self.driver.on_bot_connect
async def _(bot: Bot):
if bot.self_id.startswith("a4o@"):
return
await self.bot_connect(bot)

@self.driver.on_bot_disconnect
async def _(bot: Bot):
if bot.self_id.startswith("a4o@"):
return
await self.bot_disconnect(bot)

0 comments on commit dd00f06

Please sign in to comment.