fix(discord): 修复WebSocket连接检查并添加错误日志

refactor(config): 更新配置文件的网络和认证信息

feat(cross-platform): 为跨平台消息处理添加异常捕获和日志
This commit is contained in:
2026-03-24 14:00:29 +08:00
committed by 镀铬酸钾
parent 88f4836d22
commit fcc8438d0c
3 changed files with 423 additions and 402 deletions

View File

@@ -84,6 +84,7 @@ class DiscordBotWrapper:
content = "" content = ""
files = [] files = []
try:
for node in nodes: for node in nodes:
if node.get("type") == "node": if node.get("type") == "node":
node_data = node.get("data", {}) node_data = node.get("data", {})
@@ -194,7 +195,6 @@ class DiscordBotWrapper:
content += f"[表情:{face_id}]" content += f"[表情:{face_id}]"
content += "\n" content += "\n"
try:
if content or files: if content or files:
# target is usually event, we can use event.bot.send # target is usually event, we can use event.bot.send
if isinstance(target, GroupMessageEvent): if isinstance(target, GroupMessageEvent):
@@ -209,6 +209,8 @@ class DiscordBotWrapper:
await user.dm_channel.send(content=content, files=files if files else None) await user.dm_channel.send(content=content, files=files if files else None)
except Exception as e: except Exception as e:
logger.error(f"发送 Discord 合并转发消息失败: {e}") logger.error(f"发送 Discord 合并转发消息失败: {e}")
import traceback
logger.error(f"异常堆栈: {traceback.format_exc()}")
class DiscordToOneBotConverter: class DiscordToOneBotConverter:
""" """
@@ -416,6 +418,7 @@ class DiscordToOneBotConverter:
content = "" content = ""
files = [] files = []
try:
# 统一转换为列表处理 # 统一转换为列表处理
if not isinstance(message, list): if not isinstance(message, list):
message = [message] message = [message]
@@ -548,7 +551,6 @@ class DiscordToOneBotConverter:
pass pass
# 发送消息到 Discord # 发送消息到 Discord
try:
# 如果内容为空但有文件Discord 允许发送 # 如果内容为空但有文件Discord 允许发送
if content or files: if content or files:
await channel.send(content=content, files=files if files else None) await channel.send(content=content, files=files if files else None)
@@ -556,3 +558,5 @@ class DiscordToOneBotConverter:
logger.warning("尝试发送空消息到 Discord已拦截") logger.warning("尝试发送空消息到 Discord已拦截")
except Exception as e: except Exception as e:
logger.error(f"发送 Discord 消息失败: {e}") logger.error(f"发送 Discord 消息失败: {e}")
import traceback
logger.error(f"异常堆栈: {traceback.format_exc()}")

View File

@@ -51,6 +51,7 @@ async def handle_qq_message(
@matcher.on_message() @matcher.on_message()
async def handle_qq_group_message(event: GroupMessageEvent): async def handle_qq_group_message(event: GroupMessageEvent):
"""处理 QQ 群消息,转发到 Discord""" """处理 QQ 群消息,转发到 Discord"""
try:
if not config.ENABLE_CROSS_PLATFORM: if not config.ENABLE_CROSS_PLATFORM:
return return
@@ -150,10 +151,15 @@ async def handle_qq_group_message(event: GroupMessageEvent):
content=content, content=content,
attachments=attachments attachments=attachments
) )
except Exception as e:
logger.error(f"[CrossPlatform] 处理 QQ 群消息失败: {e}")
import traceback
logger.error(f"[CrossPlatform] 异常堆栈: {traceback.format_exc()}")
@matcher.on_message() @matcher.on_message()
async def handle_discord_message_event(event: Any): async def handle_discord_message_event(event: Any):
"""处理 Discord 消息事件(通过适配器注入)""" """处理 Discord 消息事件(通过适配器注入)"""
try:
if not config.ENABLE_CROSS_PLATFORM: if not config.ENABLE_CROSS_PLATFORM:
return return
@@ -240,6 +246,10 @@ async def handle_discord_message_event(event: Any):
attachments=attachments, attachments=attachments,
embed=None embed=None
) )
except Exception as e:
logger.error(f"[CrossPlatform] 处理 Discord 消息事件失败: {e}")
import traceback
logger.error(f"[CrossPlatform] 异常堆栈: {traceback.format_exc()}")
@matcher.command("cross_config", "跨平台配置", permission=Permission.ADMIN) @matcher.command("cross_config", "跨平台配置", permission=Permission.ADMIN)
async def cross_config_command(event: MessageEvent): async def cross_config_command(event: MessageEvent):

View File

@@ -88,6 +88,9 @@ class DiscordAdapter(discord.Client if DISCORD_AVAILABLE else object):
await matcher.handle_event(mock_event.bot, mock_event) await matcher.handle_event(mock_event.bot, mock_event)
except Exception as e: except Exception as e:
self.logger.error(f"处理 Discord 消息时发生异常: {e}") self.logger.error(f"处理 Discord 消息时发生异常: {e}")
# 记录详细的异常信息
import traceback
self.logger.error(f"异常堆栈: {traceback.format_exc()}")
async def start_redis_subscription(self): async def start_redis_subscription(self):
"""启动 Redis 订阅以处理跨平台消息发送""" """启动 Redis 订阅以处理跨平台消息发送"""
@@ -96,7 +99,7 @@ class DiscordAdapter(discord.Client if DISCORD_AVAILABLE else object):
return return
try: try:
channel_name = "neobot_cross_platform" channel_name = "neobot_discord_send"
pubsub = redis_manager.redis.pubsub() pubsub = redis_manager.redis.pubsub()
await pubsub.subscribe(channel_name) await pubsub.subscribe(channel_name)
@@ -386,15 +389,19 @@ class DiscordAdapter(discord.Client if DISCORD_AVAILABLE else object):
""" """
self.logger.info(f"心跳机制已启动,间隔: {interval}") self.logger.info(f"心跳机制已启动,间隔: {interval}")
while self.is_closed() is False: while not self.is_closed():
try: try:
await asyncio.sleep(interval) await asyncio.sleep(interval)
# discord.py 的 ws 对象是 DiscordWebSocket它没有 closed 属性 # 检查 WebSocket 连接状态
# 我们可以通过检查 self.is_closed() 或者 ws.open 来判断 if self.ws is not None:
if self.ws is not None and not getattr(self.ws, 'open', True): # 正确检查 WebSocket 状态
if not getattr(self.ws, 'open', False):
self.logger.warning("检测到 WebSocket 连接已关闭,触发重连...") self.logger.warning("检测到 WebSocket 连接已关闭,触发重连...")
await self.ws.close(4000) try:
await self.ws.close(code=4000)
except Exception as close_error:
self.logger.error(f"关闭 WebSocket 连接时出错: {close_error}")
break break
self.logger.debug(f"心跳正常: {self.user}") self.logger.debug(f"心跳正常: {self.user}")