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

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

feat(cross-platform): 为跨平台消息处理添加异常捕获和日志
This commit is contained in:
2026-03-24 14:00:29 +08:00
parent ce650d2b1e
commit 5fb791b9fe
3 changed files with 423 additions and 402 deletions

View File

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

View File

@@ -84,6 +84,7 @@ class DiscordBotWrapper:
content = ""
files = []
try:
for node in nodes:
if node.get("type") == "node":
node_data = node.get("data", {})
@@ -194,7 +195,6 @@ class DiscordBotWrapper:
content += f"[表情:{face_id}]"
content += "\n"
try:
if content or files:
# target is usually event, we can use event.bot.send
if isinstance(target, GroupMessageEvent):
@@ -209,6 +209,8 @@ class DiscordBotWrapper:
await user.dm_channel.send(content=content, files=files if files else None)
except Exception as e:
logger.error(f"发送 Discord 合并转发消息失败: {e}")
import traceback
logger.error(f"异常堆栈: {traceback.format_exc()}")
class DiscordToOneBotConverter:
"""
@@ -416,6 +418,7 @@ class DiscordToOneBotConverter:
content = ""
files = []
try:
# 统一转换为列表处理
if not isinstance(message, list):
message = [message]
@@ -548,7 +551,6 @@ class DiscordToOneBotConverter:
pass
# 发送消息到 Discord
try:
# 如果内容为空但有文件Discord 允许发送
if content or files:
await channel.send(content=content, files=files if files else None)
@@ -556,3 +558,5 @@ class DiscordToOneBotConverter:
logger.warning("尝试发送空消息到 Discord已拦截")
except Exception as 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()
async def handle_qq_group_message(event: GroupMessageEvent):
"""处理 QQ 群消息,转发到 Discord"""
try:
if not config.ENABLE_CROSS_PLATFORM:
return
@@ -150,10 +151,15 @@ async def handle_qq_group_message(event: GroupMessageEvent):
content=content,
attachments=attachments
)
except Exception as e:
logger.error(f"[CrossPlatform] 处理 QQ 群消息失败: {e}")
import traceback
logger.error(f"[CrossPlatform] 异常堆栈: {traceback.format_exc()}")
@matcher.on_message()
async def handle_discord_message_event(event: Any):
"""处理 Discord 消息事件(通过适配器注入)"""
try:
if not config.ENABLE_CROSS_PLATFORM:
return
@@ -240,6 +246,10 @@ async def handle_discord_message_event(event: Any):
attachments=attachments,
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)
async def cross_config_command(event: MessageEvent):