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

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

feat(cross-platform): 为跨平台消息处理添加异常捕获和日志
This commit is contained in:
2026-03-24 14:00:29 +08:00
committed by 镀铬酸钾
parent f4df1989de
commit 95694071d1
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,16 +389,20 @@ 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):
self.logger.warning("检测到 WebSocket 连接已关闭,触发重连...")
await self.ws.close(4000)
break
# 检查 WebSocket 连接状态
if self.ws is not None:
# 正确检查 WebSocket 状态
if not getattr(self.ws, 'open', False):
self.logger.warning("检测到 WebSocket 连接已关闭,触发重连...")
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}")