fix(discord-cross): 修复跨平台消息处理和附件下载问题
修复QQ群消息处理中的非群消息过滤问题 优化Discord附件下载逻辑,使用aiohttp替代requests 修复Redis订阅任务重复创建问题 调整消息格式化的embed字段处理逻辑
This commit is contained in:
@@ -41,6 +41,7 @@ class DiscordAdapter(discord.Client if DISCORD_AVAILABLE else object):
|
||||
|
||||
self.proxy = None
|
||||
self.proxy_type = "http"
|
||||
self._redis_sub_task = None
|
||||
if global_config.discord.proxy:
|
||||
self.proxy = global_config.discord.proxy
|
||||
self.proxy_type = global_config.discord.proxy_type or "http"
|
||||
@@ -66,7 +67,8 @@ class DiscordAdapter(discord.Client if DISCORD_AVAILABLE else object):
|
||||
self.start_heartbeat_task(interval=30)
|
||||
|
||||
# 启动 Redis 订阅以处理跨平台消息
|
||||
asyncio.create_task(self.start_redis_subscription())
|
||||
if self._redis_sub_task is None or self._redis_sub_task.done():
|
||||
self._redis_sub_task = asyncio.create_task(self.start_redis_subscription())
|
||||
|
||||
async def on_message(self, message: 'discord.Message'):
|
||||
"""当收到 Discord 消息时触发"""
|
||||
@@ -94,7 +96,7 @@ class DiscordAdapter(discord.Client if DISCORD_AVAILABLE else object):
|
||||
|
||||
async def start_redis_subscription(self):
|
||||
"""启动 Redis 订阅以处理跨平台消息发送"""
|
||||
if redis_manager.redis is None:
|
||||
if redis_manager._redis is None:
|
||||
self.logger.warning("[DiscordAdapter] Redis 未初始化,跳过订阅")
|
||||
return
|
||||
|
||||
@@ -226,7 +228,12 @@ class DiscordAdapter(discord.Client if DISCORD_AVAILABLE else object):
|
||||
|
||||
if attachment_url.startswith('http'):
|
||||
try:
|
||||
response = requests.get(attachment_url, proxies=proxies, timeout=30)
|
||||
import aiohttp
|
||||
proxy_url = self.proxy if self.proxy else None
|
||||
async with aiohttp.ClientSession() as session:
|
||||
async with session.get(attachment_url, proxy=proxy_url, timeout=30) as response:
|
||||
content_bytes = await response.read()
|
||||
|
||||
if not filename:
|
||||
filename = os.path.basename(attachment_url.split('?')[0]) or "attachment"
|
||||
|
||||
@@ -235,7 +242,7 @@ class DiscordAdapter(discord.Client if DISCORD_AVAILABLE else object):
|
||||
|
||||
if is_voice:
|
||||
# 尝试转换为 OGG Opus
|
||||
ogg_bytes = await self.convert_to_ogg_opus(response.content)
|
||||
ogg_bytes = await self.convert_to_ogg_opus(content_bytes)
|
||||
if ogg_bytes:
|
||||
# 转换成功,作为语音消息发送
|
||||
# discord.py 官方 API 目前不支持直接发送语音消息
|
||||
@@ -277,9 +284,9 @@ class DiscordAdapter(discord.Client if DISCORD_AVAILABLE else object):
|
||||
files.append(discord.File(fp=io.BytesIO(ogg_bytes), filename="voice.ogg"))
|
||||
else:
|
||||
# 转换失败,作为普通文件发送
|
||||
files.append(discord.File(fp=io.BytesIO(response.content), filename=filename))
|
||||
files.append(discord.File(fp=io.BytesIO(content_bytes), filename=filename))
|
||||
else:
|
||||
files.append(discord.File(fp=io.BytesIO(response.content), filename=filename))
|
||||
files.append(discord.File(fp=io.BytesIO(content_bytes), filename=filename))
|
||||
except Exception as e:
|
||||
self.logger.error(f"[DiscordAdapter] 下载附件失败: {attachment_url}, 错误: {e}")
|
||||
|
||||
|
||||
@@ -55,6 +55,10 @@ async def handle_qq_group_message(event: GroupMessageEvent):
|
||||
if not config.ENABLE_CROSS_PLATFORM:
|
||||
return
|
||||
|
||||
# 忽略非群消息和 Discord 注入的消息
|
||||
if not hasattr(event, 'group_id') or hasattr(event, '_is_discord_message'):
|
||||
return
|
||||
|
||||
group_id = event.group_id
|
||||
mapped_channel = None
|
||||
for discord_channel_id, info in config.CROSS_PLATFORM_MAP.items():
|
||||
|
||||
@@ -327,13 +327,14 @@ async def format_qq_to_discord_content(
|
||||
"name": f"{qq_nickname}",
|
||||
"icon_url": f"https://q1.qlogo.cn/g?b=qq&nk={qq_user_id}&s=640"
|
||||
},
|
||||
"description": content if content else "",
|
||||
"timestamp": None,
|
||||
"footer": {
|
||||
"text": f"来自 QQ"
|
||||
}
|
||||
}
|
||||
|
||||
if content:
|
||||
embed["description"] = content
|
||||
|
||||
if attachments:
|
||||
image_urls = []
|
||||
voice_urls = []
|
||||
@@ -364,7 +365,10 @@ async def format_qq_to_discord_content(
|
||||
filtered_attachments.append(att)
|
||||
|
||||
attachments = filtered_attachments
|
||||
embed["description"] = content if content else ""
|
||||
if content:
|
||||
embed["description"] = content
|
||||
elif "description" not in embed:
|
||||
embed["description"] = ""
|
||||
|
||||
if image_urls:
|
||||
embed["image"] = {"url": image_urls[0]}
|
||||
|
||||
Reference in New Issue
Block a user