-
-
Notifications
You must be signed in to change notification settings - Fork 931
feat(satori): 添加对合并转发消息功能的支持 #3050
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 3 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
a4d7696
Update satori_event.py
shangxueink a8b2791
Update satori_event.py
shangxueink d765b1a
Update satori_event.py
shangxueink bc7ae07
Update satori_adapter.py
shangxueink da285f7
style: format code for better readability in satori_adapter.py and sa…
Soulter File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -2,7 +2,7 @@ | |||||||||||||||||||||||||||||||||||||||
from astrbot.api import logger | ||||||||||||||||||||||||||||||||||||||||
from astrbot.api.event import AstrMessageEvent, MessageChain | ||||||||||||||||||||||||||||||||||||||||
from astrbot.api.platform import AstrBotMessage, PlatformMetadata | ||||||||||||||||||||||||||||||||||||||||
from astrbot.api.message_components import Plain, Image, At, File, Record, Video, Reply | ||||||||||||||||||||||||||||||||||||||||
from astrbot.api.message_components import Plain, Image, At, File, Record, Video, Reply, Forward, Node, Nodes | ||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||
if TYPE_CHECKING: | ||||||||||||||||||||||||||||||||||||||||
from .satori_adapter import SatoriPlatformAdapter | ||||||||||||||||||||||||||||||||||||||||
|
@@ -48,55 +48,22 @@ async def send_with_adapter( | |||||||||||||||||||||||||||||||||||||||
content_parts = [] | ||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||
for component in message.chain: | ||||||||||||||||||||||||||||||||||||||||
if isinstance(component, Plain): | ||||||||||||||||||||||||||||||||||||||||
text = ( | ||||||||||||||||||||||||||||||||||||||||
component.text.replace("&", "&") | ||||||||||||||||||||||||||||||||||||||||
.replace("<", "<") | ||||||||||||||||||||||||||||||||||||||||
.replace(">", ">") | ||||||||||||||||||||||||||||||||||||||||
) | ||||||||||||||||||||||||||||||||||||||||
content_parts.append(text) | ||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||
elif isinstance(component, At): | ||||||||||||||||||||||||||||||||||||||||
if component.qq: | ||||||||||||||||||||||||||||||||||||||||
content_parts.append(f'<at id="{component.qq}"/>') | ||||||||||||||||||||||||||||||||||||||||
elif component.name: | ||||||||||||||||||||||||||||||||||||||||
content_parts.append(f'<at name="{component.name}"/>') | ||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||
elif isinstance(component, Image): | ||||||||||||||||||||||||||||||||||||||||
try: | ||||||||||||||||||||||||||||||||||||||||
image_base64 = await component.convert_to_base64() | ||||||||||||||||||||||||||||||||||||||||
if image_base64: | ||||||||||||||||||||||||||||||||||||||||
content_parts.append( | ||||||||||||||||||||||||||||||||||||||||
f'<img src="data:image/jpeg;base64,{image_base64}"/>' | ||||||||||||||||||||||||||||||||||||||||
) | ||||||||||||||||||||||||||||||||||||||||
except Exception as e: | ||||||||||||||||||||||||||||||||||||||||
logger.error(f"图片转换为base64失败: {e}") | ||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||
elif isinstance(component, File): | ||||||||||||||||||||||||||||||||||||||||
content_parts.append( | ||||||||||||||||||||||||||||||||||||||||
f'<file src="{component.file}" name="{component.name or "文件"}"/>' | ||||||||||||||||||||||||||||||||||||||||
) | ||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||
elif isinstance(component, Record): | ||||||||||||||||||||||||||||||||||||||||
try: | ||||||||||||||||||||||||||||||||||||||||
record_base64 = await component.convert_to_base64() | ||||||||||||||||||||||||||||||||||||||||
if record_base64: | ||||||||||||||||||||||||||||||||||||||||
content_parts.append( | ||||||||||||||||||||||||||||||||||||||||
f'<audio src="data:audio/wav;base64,{record_base64}"/>' | ||||||||||||||||||||||||||||||||||||||||
) | ||||||||||||||||||||||||||||||||||||||||
except Exception as e: | ||||||||||||||||||||||||||||||||||||||||
logger.error(f"语音转换为base64失败: {e}") | ||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||
elif isinstance(component, Reply): | ||||||||||||||||||||||||||||||||||||||||
content_parts.append(f'<reply id="{component.id}"/>') | ||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||
elif isinstance(component, Video): | ||||||||||||||||||||||||||||||||||||||||
try: | ||||||||||||||||||||||||||||||||||||||||
video_path_url = await component.convert_to_file_path() | ||||||||||||||||||||||||||||||||||||||||
if video_path_url: | ||||||||||||||||||||||||||||||||||||||||
content_parts.append(f'<video src="{video_path_url}"/>') | ||||||||||||||||||||||||||||||||||||||||
except Exception as e: | ||||||||||||||||||||||||||||||||||||||||
logger.error(f"视频文件转换失败: {e}") | ||||||||||||||||||||||||||||||||||||||||
component_content = await cls._convert_component_to_satori_static(component) | ||||||||||||||||||||||||||||||||||||||||
if component_content: | ||||||||||||||||||||||||||||||||||||||||
content_parts.append(component_content) | ||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||
# 特殊处理 Node 和 Nodes 组件 | ||||||||||||||||||||||||||||||||||||||||
if isinstance(component, Node): | ||||||||||||||||||||||||||||||||||||||||
# 单个转发节点 | ||||||||||||||||||||||||||||||||||||||||
node_content = await cls._convert_node_to_satori_static(component) | ||||||||||||||||||||||||||||||||||||||||
if node_content: | ||||||||||||||||||||||||||||||||||||||||
content_parts.append(node_content) | ||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||
elif isinstance(component, Nodes): | ||||||||||||||||||||||||||||||||||||||||
# 合并转发消息 | ||||||||||||||||||||||||||||||||||||||||
node_content = await cls._convert_nodes_to_satori_static(component) | ||||||||||||||||||||||||||||||||||||||||
if node_content: | ||||||||||||||||||||||||||||||||||||||||
content_parts.append(node_content) | ||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||
content = "".join(content_parts) | ||||||||||||||||||||||||||||||||||||||||
channel_id = session_id | ||||||||||||||||||||||||||||||||||||||||
|
@@ -138,55 +105,22 @@ async def send(self, message: MessageChain): | |||||||||||||||||||||||||||||||||||||||
content_parts = [] | ||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||
for component in message.chain: | ||||||||||||||||||||||||||||||||||||||||
if isinstance(component, Plain): | ||||||||||||||||||||||||||||||||||||||||
text = ( | ||||||||||||||||||||||||||||||||||||||||
component.text.replace("&", "&") | ||||||||||||||||||||||||||||||||||||||||
.replace("<", "<") | ||||||||||||||||||||||||||||||||||||||||
.replace(">", ">") | ||||||||||||||||||||||||||||||||||||||||
) | ||||||||||||||||||||||||||||||||||||||||
content_parts.append(text) | ||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||
elif isinstance(component, At): | ||||||||||||||||||||||||||||||||||||||||
if component.qq: | ||||||||||||||||||||||||||||||||||||||||
content_parts.append(f'<at id="{component.qq}"/>') | ||||||||||||||||||||||||||||||||||||||||
elif component.name: | ||||||||||||||||||||||||||||||||||||||||
content_parts.append(f'<at name="{component.name}"/>') | ||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||
elif isinstance(component, Image): | ||||||||||||||||||||||||||||||||||||||||
try: | ||||||||||||||||||||||||||||||||||||||||
image_base64 = await component.convert_to_base64() | ||||||||||||||||||||||||||||||||||||||||
if image_base64: | ||||||||||||||||||||||||||||||||||||||||
content_parts.append( | ||||||||||||||||||||||||||||||||||||||||
f'<img src="data:image/jpeg;base64,{image_base64}"/>' | ||||||||||||||||||||||||||||||||||||||||
) | ||||||||||||||||||||||||||||||||||||||||
except Exception as e: | ||||||||||||||||||||||||||||||||||||||||
logger.error(f"图片转换为base64失败: {e}") | ||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||
elif isinstance(component, File): | ||||||||||||||||||||||||||||||||||||||||
content_parts.append( | ||||||||||||||||||||||||||||||||||||||||
f'<file src="{component.file}" name="{component.name or "文件"}"/>' | ||||||||||||||||||||||||||||||||||||||||
) | ||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||
elif isinstance(component, Record): | ||||||||||||||||||||||||||||||||||||||||
try: | ||||||||||||||||||||||||||||||||||||||||
record_base64 = await component.convert_to_base64() | ||||||||||||||||||||||||||||||||||||||||
if record_base64: | ||||||||||||||||||||||||||||||||||||||||
content_parts.append( | ||||||||||||||||||||||||||||||||||||||||
f'<audio src="data:audio/wav;base64,{record_base64}"/>' | ||||||||||||||||||||||||||||||||||||||||
) | ||||||||||||||||||||||||||||||||||||||||
except Exception as e: | ||||||||||||||||||||||||||||||||||||||||
logger.error(f"语音转换为base64失败: {e}") | ||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||
elif isinstance(component, Reply): | ||||||||||||||||||||||||||||||||||||||||
content_parts.append(f'<reply id="{component.id}"/>') | ||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||
elif isinstance(component, Video): | ||||||||||||||||||||||||||||||||||||||||
try: | ||||||||||||||||||||||||||||||||||||||||
video_path_url = await component.convert_to_file_path() | ||||||||||||||||||||||||||||||||||||||||
if video_path_url: | ||||||||||||||||||||||||||||||||||||||||
content_parts.append(f'<video src="{video_path_url}"/>') | ||||||||||||||||||||||||||||||||||||||||
except Exception as e: | ||||||||||||||||||||||||||||||||||||||||
logger.error(f"视频文件转换失败: {e}") | ||||||||||||||||||||||||||||||||||||||||
component_content = await self._convert_component_to_satori(component) | ||||||||||||||||||||||||||||||||||||||||
if component_content: | ||||||||||||||||||||||||||||||||||||||||
content_parts.append(component_content) | ||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||
# 特殊处理 Node 和 Nodes 组件 | ||||||||||||||||||||||||||||||||||||||||
if isinstance(component, Node): | ||||||||||||||||||||||||||||||||||||||||
# 单个转发节点 | ||||||||||||||||||||||||||||||||||||||||
node_content = await self._convert_node_to_satori(component) | ||||||||||||||||||||||||||||||||||||||||
if node_content: | ||||||||||||||||||||||||||||||||||||||||
content_parts.append(node_content) | ||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||
elif isinstance(component, Nodes): | ||||||||||||||||||||||||||||||||||||||||
# 合并转发消息 | ||||||||||||||||||||||||||||||||||||||||
node_content = await self._convert_nodes_to_satori(component) | ||||||||||||||||||||||||||||||||||||||||
if node_content: | ||||||||||||||||||||||||||||||||||||||||
content_parts.append(node_content) | ||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||
content = "".join(content_parts) | ||||||||||||||||||||||||||||||||||||||||
channel_id = self.session_id | ||||||||||||||||||||||||||||||||||||||||
|
@@ -250,3 +184,219 @@ async def send_streaming(self, generator, use_fallback: bool = False): | |||||||||||||||||||||||||||||||||||||||
logger.error(f"Satori 流式消息发送异常: {e}") | ||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||
return await super().send_streaming(generator, use_fallback) | ||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||
async def _convert_component_to_satori(self, component) -> str: | ||||||||||||||||||||||||||||||||||||||||
"""将单个消息组件转换为 Satori 格式""" | ||||||||||||||||||||||||||||||||||||||||
try: | ||||||||||||||||||||||||||||||||||||||||
if isinstance(component, Plain): | ||||||||||||||||||||||||||||||||||||||||
text = ( | ||||||||||||||||||||||||||||||||||||||||
component.text.replace("&", "&") | ||||||||||||||||||||||||||||||||||||||||
.replace("<", "<") | ||||||||||||||||||||||||||||||||||||||||
.replace(">", ">") | ||||||||||||||||||||||||||||||||||||||||
) | ||||||||||||||||||||||||||||||||||||||||
return text | ||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||
elif isinstance(component, At): | ||||||||||||||||||||||||||||||||||||||||
if component.qq: | ||||||||||||||||||||||||||||||||||||||||
return f'<at id="{component.qq}"/>' | ||||||||||||||||||||||||||||||||||||||||
elif component.name: | ||||||||||||||||||||||||||||||||||||||||
return f'<at name="{component.name}"/>' | ||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||
elif isinstance(component, Image): | ||||||||||||||||||||||||||||||||||||||||
try: | ||||||||||||||||||||||||||||||||||||||||
image_base64 = await component.convert_to_base64() | ||||||||||||||||||||||||||||||||||||||||
if image_base64: | ||||||||||||||||||||||||||||||||||||||||
return f'<img src="data:image/jpeg;base64,{image_base64}"/>' | ||||||||||||||||||||||||||||||||||||||||
except Exception as e: | ||||||||||||||||||||||||||||||||||||||||
logger.error(f"图片转换为base64失败: {e}") | ||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||
elif isinstance(component, File): | ||||||||||||||||||||||||||||||||||||||||
return f'<file src="{component.file}" name="{component.name or "文件"}"/>' | ||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||
elif isinstance(component, Record): | ||||||||||||||||||||||||||||||||||||||||
try: | ||||||||||||||||||||||||||||||||||||||||
record_base64 = await component.convert_to_base64() | ||||||||||||||||||||||||||||||||||||||||
if record_base64: | ||||||||||||||||||||||||||||||||||||||||
return f'<audio src="data:audio/wav;base64,{record_base64}"/>' | ||||||||||||||||||||||||||||||||||||||||
except Exception as e: | ||||||||||||||||||||||||||||||||||||||||
logger.error(f"语音转换为base64失败: {e}") | ||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||
elif isinstance(component, Reply): | ||||||||||||||||||||||||||||||||||||||||
return f'<reply id="{component.id}"/>' | ||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||
elif isinstance(component, Video): | ||||||||||||||||||||||||||||||||||||||||
try: | ||||||||||||||||||||||||||||||||||||||||
video_path_url = await component.convert_to_file_path() | ||||||||||||||||||||||||||||||||||||||||
if video_path_url: | ||||||||||||||||||||||||||||||||||||||||
return f'<video src="{video_path_url}"/>' | ||||||||||||||||||||||||||||||||||||||||
except Exception as e: | ||||||||||||||||||||||||||||||||||||||||
logger.error(f"视频文件转换失败: {e}") | ||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||
elif isinstance(component, Forward): | ||||||||||||||||||||||||||||||||||||||||
return f'<message id="{component.id}" forward/>' | ||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||
# 对于其他未处理的组件类型,返回空字符串 | ||||||||||||||||||||||||||||||||||||||||
return "" | ||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||
except Exception as e: | ||||||||||||||||||||||||||||||||||||||||
logger.error(f"转换消息组件失败: {e}") | ||||||||||||||||||||||||||||||||||||||||
return "" | ||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||
async def _convert_node_to_satori(self, node: Node) -> str: | ||||||||||||||||||||||||||||||||||||||||
"""将单个转发节点转换为 Satori 格式""" | ||||||||||||||||||||||||||||||||||||||||
try: | ||||||||||||||||||||||||||||||||||||||||
content_parts = [] | ||||||||||||||||||||||||||||||||||||||||
if node.content: | ||||||||||||||||||||||||||||||||||||||||
for content_component in node.content: | ||||||||||||||||||||||||||||||||||||||||
component_content = await self._convert_component_to_satori(content_component) | ||||||||||||||||||||||||||||||||||||||||
if component_content: | ||||||||||||||||||||||||||||||||||||||||
content_parts.append(component_content) | ||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||
content = "".join(content_parts) | ||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||
# 如果内容为空,添加默认内容 | ||||||||||||||||||||||||||||||||||||||||
if not content.strip(): | ||||||||||||||||||||||||||||||||||||||||
content = "[转发消息]" | ||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||
# 构建 Satori 格式的转发节点 | ||||||||||||||||||||||||||||||||||||||||
author_attrs = [] | ||||||||||||||||||||||||||||||||||||||||
if node.uin: | ||||||||||||||||||||||||||||||||||||||||
author_attrs.append(f'id="{node.uin}"') | ||||||||||||||||||||||||||||||||||||||||
if node.name: | ||||||||||||||||||||||||||||||||||||||||
author_attrs.append(f'name="{node.name}"') | ||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||
author_attr_str = " ".join(author_attrs) | ||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||
return f'<message><author {author_attr_str}/>{content}</message>' | ||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||
except Exception as e: | ||||||||||||||||||||||||||||||||||||||||
logger.error(f"转换转发节点失败: {e}") | ||||||||||||||||||||||||||||||||||||||||
return "" | ||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||
@classmethod | ||||||||||||||||||||||||||||||||||||||||
async def _convert_component_to_satori_static(cls, component) -> str: | ||||||||||||||||||||||||||||||||||||||||
"""将单个消息组件转换为 Satori 格式""" | ||||||||||||||||||||||||||||||||||||||||
try: | ||||||||||||||||||||||||||||||||||||||||
if isinstance(component, Plain): | ||||||||||||||||||||||||||||||||||||||||
text = ( | ||||||||||||||||||||||||||||||||||||||||
component.text.replace("&", "&") | ||||||||||||||||||||||||||||||||||||||||
.replace("<", "<") | ||||||||||||||||||||||||||||||||||||||||
.replace(">", ">") | ||||||||||||||||||||||||||||||||||||||||
) | ||||||||||||||||||||||||||||||||||||||||
return text | ||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||
elif isinstance(component, At): | ||||||||||||||||||||||||||||||||||||||||
if component.qq: | ||||||||||||||||||||||||||||||||||||||||
return f'<at id="{component.qq}"/>' | ||||||||||||||||||||||||||||||||||||||||
elif component.name: | ||||||||||||||||||||||||||||||||||||||||
return f'<at name="{component.name}"/>' | ||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||
elif isinstance(component, Image): | ||||||||||||||||||||||||||||||||||||||||
try: | ||||||||||||||||||||||||||||||||||||||||
image_base64 = await component.convert_to_base64() | ||||||||||||||||||||||||||||||||||||||||
if image_base64: | ||||||||||||||||||||||||||||||||||||||||
return f'<img src="data:image/jpeg;base64,{image_base64}"/>' | ||||||||||||||||||||||||||||||||||||||||
except Exception as e: | ||||||||||||||||||||||||||||||||||||||||
logger.error(f"图片转换为base64失败: {e}") | ||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||
elif isinstance(component, File): | ||||||||||||||||||||||||||||||||||||||||
return f'<file src="{component.file}" name="{component.name or "文件"}"/>' | ||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||
elif isinstance(component, Record): | ||||||||||||||||||||||||||||||||||||||||
try: | ||||||||||||||||||||||||||||||||||||||||
record_base64 = await component.convert_to_base64() | ||||||||||||||||||||||||||||||||||||||||
if record_base64: | ||||||||||||||||||||||||||||||||||||||||
return f'<audio src="data:audio/wav;base64,{record_base64}"/>' | ||||||||||||||||||||||||||||||||||||||||
except Exception as e: | ||||||||||||||||||||||||||||||||||||||||
logger.error(f"语音转换为base64失败: {e}") | ||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||
elif isinstance(component, Reply): | ||||||||||||||||||||||||||||||||||||||||
return f'<reply id="{component.id}"/>' | ||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||
elif isinstance(component, Video): | ||||||||||||||||||||||||||||||||||||||||
try: | ||||||||||||||||||||||||||||||||||||||||
video_path_url = await component.convert_to_file_path() | ||||||||||||||||||||||||||||||||||||||||
if video_path_url: | ||||||||||||||||||||||||||||||||||||||||
return f'<video src="{video_path_url}"/>' | ||||||||||||||||||||||||||||||||||||||||
except Exception as e: | ||||||||||||||||||||||||||||||||||||||||
logger.error(f"视频文件转换失败: {e}") | ||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||
elif isinstance(component, Forward): | ||||||||||||||||||||||||||||||||||||||||
return f'<message id="{component.id}" forward/>' | ||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||
# 对于其他未处理的组件类型,返回空字符串 | ||||||||||||||||||||||||||||||||||||||||
return "" | ||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||
except Exception as e: | ||||||||||||||||||||||||||||||||||||||||
logger.error(f"转换消息组件失败: {e}") | ||||||||||||||||||||||||||||||||||||||||
return "" | ||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||
@classmethod | ||||||||||||||||||||||||||||||||||||||||
async def _convert_node_to_satori_static(cls, node: Node) -> str: | ||||||||||||||||||||||||||||||||||||||||
"""将单个转发节点转换为 Satori 格式""" | ||||||||||||||||||||||||||||||||||||||||
try: | ||||||||||||||||||||||||||||||||||||||||
content_parts = [] | ||||||||||||||||||||||||||||||||||||||||
if node.content: | ||||||||||||||||||||||||||||||||||||||||
for content_component in node.content: | ||||||||||||||||||||||||||||||||||||||||
component_content = await cls._convert_component_to_satori_static(content_component) | ||||||||||||||||||||||||||||||||||||||||
if component_content: | ||||||||||||||||||||||||||||||||||||||||
content_parts.append(component_content) | ||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||
content = "".join(content_parts) | ||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||
# 如果内容为空,添加默认内容 | ||||||||||||||||||||||||||||||||||||||||
if not content.strip(): | ||||||||||||||||||||||||||||||||||||||||
content = "[转发消息]" | ||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||
author_attrs = [] | ||||||||||||||||||||||||||||||||||||||||
if node.uin: | ||||||||||||||||||||||||||||||||||||||||
author_attrs.append(f'id="{node.uin}"') | ||||||||||||||||||||||||||||||||||||||||
if node.name: | ||||||||||||||||||||||||||||||||||||||||
author_attrs.append(f'name="{node.name}"') | ||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||
author_attr_str = " ".join(author_attrs) | ||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||
return f'<message><author {author_attr_str}/>{content}</message>' | ||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||
except Exception as e: | ||||||||||||||||||||||||||||||||||||||||
logger.error(f"转换转发节点失败: {e}") | ||||||||||||||||||||||||||||||||||||||||
return "" | ||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||
async def _convert_nodes_to_satori(self, nodes: Nodes) -> str: | ||||||||||||||||||||||||||||||||||||||||
"""将多个转发节点转换为 Satori 格式的合并转发""" | ||||||||||||||||||||||||||||||||||||||||
try: | ||||||||||||||||||||||||||||||||||||||||
node_parts = [] | ||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||
for node in nodes.nodes: | ||||||||||||||||||||||||||||||||||||||||
node_content = await self._convert_node_to_satori(node) | ||||||||||||||||||||||||||||||||||||||||
if node_content: | ||||||||||||||||||||||||||||||||||||||||
Comment on lines
386
to
393
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. suggestion: No handling for empty nodes list in _convert_nodes_to_satori. Returning an empty string when nodes.nodes is empty could lead to confusion. Please add explicit handling for this case or log a warning to improve clarity.
Suggested change
|
||||||||||||||||||||||||||||||||||||||||
node_parts.append(node_content) | ||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||
if node_parts: | ||||||||||||||||||||||||||||||||||||||||
return f'<message forward>{"".join(node_parts)}</message>' | ||||||||||||||||||||||||||||||||||||||||
else: | ||||||||||||||||||||||||||||||||||||||||
return "" | ||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||
except Exception as e: | ||||||||||||||||||||||||||||||||||||||||
logger.error(f"转换合并转发消息失败: {e}") | ||||||||||||||||||||||||||||||||||||||||
return "" | ||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||
@classmethod | ||||||||||||||||||||||||||||||||||||||||
async def _convert_nodes_to_satori_static(cls, nodes: Nodes) -> str: | ||||||||||||||||||||||||||||||||||||||||
"""将多个转发节点转换为 Satori 格式的合并转发""" | ||||||||||||||||||||||||||||||||||||||||
try: | ||||||||||||||||||||||||||||||||||||||||
node_parts = [] | ||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||
for node in nodes.nodes: | ||||||||||||||||||||||||||||||||||||||||
node_content = await cls._convert_node_to_satori_static(node) | ||||||||||||||||||||||||||||||||||||||||
if node_content: | ||||||||||||||||||||||||||||||||||||||||
node_parts.append(node_content) | ||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||
if node_parts: | ||||||||||||||||||||||||||||||||||||||||
return f'<message forward>{"".join(node_parts)}</message>' | ||||||||||||||||||||||||||||||||||||||||
else: | ||||||||||||||||||||||||||||||||||||||||
return "" | ||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||
except Exception as e: | ||||||||||||||||||||||||||||||||||||||||
logger.error(f"转换合并转发消息失败: {e}") | ||||||||||||||||||||||||||||||||||||||||
return "" |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
suggestion: The method returns an empty string for unhandled component types, which may silently drop unknown components.
Consider adding a warning or error log when an unrecognized component type is encountered to improve maintainability and debugging.
Suggested implementation:
If
logger
is not already imported in this file, you should add:or the appropriate import statement for your project's logger at the top of the file.