From 12039fe7d09f5ffdde91c0974dd21bbfa7844e40 Mon Sep 17 00:00:00 2001 From: Soulter <905617992@qq.com> Date: Fri, 3 Apr 2026 13:41:56 +0800 Subject: [PATCH] fix: resolve Discord/Misskey hot reload issue by fixing client_self_id misuse fixes: #7187 closes: #7188 --- .../discord/discord_platform_adapter.py | 14 ++++---- .../sources/misskey/misskey_adapter.py | 34 +++++++++---------- .../platform/sources/misskey/misskey_utils.py | 16 ++++----- .../platform/sources/telegram/tg_adapter.py | 3 +- .../platform/sources/wecom/wecom_adapter.py | 1 - .../weixin_offacc_adapter.py | 2 -- 6 files changed, 34 insertions(+), 36 deletions(-) diff --git a/astrbot/core/platform/sources/discord/discord_platform_adapter.py b/astrbot/core/platform/sources/discord/discord_platform_adapter.py index 7657962a1..dbc392c89 100644 --- a/astrbot/core/platform/sources/discord/discord_platform_adapter.py +++ b/astrbot/core/platform/sources/discord/discord_platform_adapter.py @@ -46,7 +46,7 @@ class DiscordPlatformAdapter(Platform): ) -> None: super().__init__(platform_config, event_queue) self.settings = platform_settings - self.client_self_id: str | None = None + self.bot_self_id: str | None = None self.registered_handlers = [] # 指令注册相关 self.enable_command_register = self.config.get("discord_command_register", True) @@ -92,10 +92,10 @@ class DiscordPlatformAdapter(Platform): message_obj.message_str = message_chain.get_plain_text() message_obj.sender = MessageMember( - user_id=str(self.client_self_id), + user_id=str(self.bot_self_id), nickname=self.client.user.display_name, ) - message_obj.self_id = cast(str, self.client_self_id) + message_obj.self_id = cast(str, self.bot_self_id) message_obj.session_id = session.session_id message_obj.message = message_chain.chain @@ -128,8 +128,8 @@ class DiscordPlatformAdapter(Platform): # 初始化回调函数 async def on_received(message_data) -> None: logger.debug(f"[Discord] 收到消息: {message_data}") - if self.client_self_id is None: - self.client_self_id = message_data.get("bot_id") + if self.bot_self_id is None: + self.bot_self_id = message_data.get("bot_id") abm = await self.convert_message(data=message_data) await self.handle_msg(abm) @@ -241,7 +241,7 @@ class DiscordPlatformAdapter(Platform): ) abm.message = message_chain abm.raw_message = message - abm.self_id = cast(str, self.client_self_id) + abm.self_id = cast(str, self.bot_self_id) abm.session_id = str(message.channel.id) abm.message_id = str(message.id) return abm @@ -465,7 +465,7 @@ class DiscordPlatformAdapter(Platform): ) abm.message = [Plain(text=message_str_for_filter)] abm.raw_message = ctx.interaction - abm.self_id = cast(str, self.client_self_id) + abm.self_id = cast(str, self.bot_self_id) abm.session_id = str(ctx.channel_id) abm.message_id = str(ctx.interaction.id) diff --git a/astrbot/core/platform/sources/misskey/misskey_adapter.py b/astrbot/core/platform/sources/misskey/misskey_adapter.py index fd61c3e50..1692c251c 100644 --- a/astrbot/core/platform/sources/misskey/misskey_adapter.py +++ b/astrbot/core/platform/sources/misskey/misskey_adapter.py @@ -93,7 +93,7 @@ class MisskeyPlatformAdapter(Platform): self.api: MisskeyAPI | None = None self._running = False - self.client_self_id = "" + self.bot_self_id = "" self._bot_username = "" self._user_cache = {} @@ -138,10 +138,10 @@ class MisskeyPlatformAdapter(Platform): try: user_info = await self.api.get_current_user() - self.client_self_id = str(user_info.get("id", "")) + self.bot_self_id = str(user_info.get("id", "")) self._bot_username = user_info.get("username", "") logger.info( - f"[Misskey] 已连接用户: {self._bot_username} (ID: {self.client_self_id})", + f"[Misskey] 已连接用户: {self._bot_username} (ID: {self.bot_self_id})", ) except Exception as e: logger.error(f"[Misskey] 获取用户信息失败: {e}") @@ -312,9 +312,9 @@ class MisskeyPlatformAdapter(Platform): ) room_id = data.get("toRoomId") logger.debug( - f"[Misskey] 收到聊天事件: sender_id={sender_id}, room_id={room_id}, is_self={sender_id == self.client_self_id}", + f"[Misskey] 收到聊天事件: sender_id={sender_id}, room_id={room_id}, is_self={sender_id == self.bot_self_id}", ) - if sender_id == self.client_self_id: + if sender_id == self.bot_self_id: return if room_id: @@ -354,13 +354,13 @@ class MisskeyPlatformAdapter(Platform): mentions = note.get("mentions", []) if self._bot_username and f"@{self._bot_username}" in text: return True - if self.client_self_id in [str(uid) for uid in mentions]: + if self.bot_self_id in [str(uid) for uid in mentions]: return True reply = note.get("reply") if reply and isinstance(reply, dict): reply_user_id = str(reply.get("user", {}).get("id", "")) - if reply_user_id == self.client_self_id: + if reply_user_id == self.bot_self_id: return bool(self._bot_username and f"@{self._bot_username}" in text) return False @@ -598,7 +598,7 @@ class MisskeyPlatformAdapter(Platform): visibility, visible_user_ids = resolve_message_visibility( user_id=user_id_for_cache, user_cache=self._user_cache, - self_id=self.client_self_id, + self_id=self.bot_self_id, default_visibility=self.default_visibility, ) logger.debug( @@ -637,14 +637,14 @@ class MisskeyPlatformAdapter(Platform): message = create_base_message( raw_data, sender_info, - self.client_self_id, + self.bot_self_id, is_chat=False, ) cache_user_info( self._user_cache, sender_info, raw_data, - self.client_self_id, + self.bot_self_id, is_chat=False, ) @@ -656,7 +656,7 @@ class MisskeyPlatformAdapter(Platform): message, raw_text, self._bot_username, - self.client_self_id, + self.bot_self_id, ) message_parts.extend(text_parts) @@ -685,14 +685,14 @@ class MisskeyPlatformAdapter(Platform): message = create_base_message( raw_data, sender_info, - self.client_self_id, + self.bot_self_id, is_chat=True, ) cache_user_info( self._user_cache, sender_info, raw_data, - self.client_self_id, + self.bot_self_id, is_chat=True, ) @@ -713,7 +713,7 @@ class MisskeyPlatformAdapter(Platform): message = create_base_message( raw_data, sender_info, - self.client_self_id, + self.bot_self_id, is_chat=False, room_id=room_id, ) @@ -722,10 +722,10 @@ class MisskeyPlatformAdapter(Platform): self._user_cache, sender_info, raw_data, - self.client_self_id, + self.bot_self_id, is_chat=False, ) - cache_room_info(self._user_cache, raw_data, self.client_self_id) + cache_room_info(self._user_cache, raw_data, self.bot_self_id) raw_text = raw_data.get("text", "") message_parts = [] @@ -736,7 +736,7 @@ class MisskeyPlatformAdapter(Platform): message, raw_text, self._bot_username, - self.client_self_id, + self.bot_self_id, ) message_parts.extend(text_parts) else: diff --git a/astrbot/core/platform/sources/misskey/misskey_utils.py b/astrbot/core/platform/sources/misskey/misskey_utils.py index dd02c13c0..86b76c21f 100644 --- a/astrbot/core/platform/sources/misskey/misskey_utils.py +++ b/astrbot/core/platform/sources/misskey/misskey_utils.py @@ -335,7 +335,7 @@ def extract_sender_info( def create_base_message( raw_data: dict[str, Any], sender_info: dict[str, Any], - client_self_id: str, + bot_self_id: str, is_chat: bool = False, room_id: str | None = None, ) -> AstrBotMessage: @@ -367,7 +367,7 @@ def create_base_message( session_id if sender_info["sender_id"] else f"{session_prefix}%unknown" ) message.message_id = str(raw_data.get("id", "")) - message.self_id = client_self_id + message.self_id = bot_self_id return message @@ -376,7 +376,7 @@ def process_at_mention( message: AstrBotMessage, raw_text: str, bot_username: str, - client_self_id: str, + bot_self_id: str, ) -> tuple[list[str], str]: """处理@提及逻辑,返回消息部分列表和处理后的文本""" message_parts = [] @@ -386,7 +386,7 @@ def process_at_mention( if bot_username and raw_text.startswith(f"@{bot_username}"): at_mention = f"@{bot_username}" - message.message.append(Comp.At(qq=client_self_id)) + message.message.append(Comp.At(qq=bot_self_id)) remaining_text = raw_text[len(at_mention) :].strip() if remaining_text: message.message.append(Comp.Plain(remaining_text)) @@ -401,7 +401,7 @@ def cache_user_info( user_cache: dict[str, Any], sender_info: dict[str, Any], raw_data: dict[str, Any], - client_self_id: str, + bot_self_id: str, is_chat: bool = False, ) -> None: """缓存用户信息""" @@ -410,7 +410,7 @@ def cache_user_info( "username": sender_info["username"], "nickname": sender_info["nickname"], "visibility": "specified", - "visible_user_ids": [client_self_id, sender_info["sender_id"]], + "visible_user_ids": [bot_self_id, sender_info["sender_id"]], } else: user_cache_data = { @@ -428,7 +428,7 @@ def cache_user_info( def cache_room_info( user_cache: dict[str, Any], raw_data: dict[str, Any], - client_self_id: str, + bot_self_id: str, ) -> None: """缓存房间信息""" room_data = raw_data.get("toRoom") @@ -442,7 +442,7 @@ def cache_room_info( "room_description": room_data.get("description", ""), "owner_id": room_data.get("ownerId", ""), "visibility": "specified", - "visible_user_ids": [client_self_id], + "visible_user_ids": [bot_self_id], } diff --git a/astrbot/core/platform/sources/telegram/tg_adapter.py b/astrbot/core/platform/sources/telegram/tg_adapter.py index 337d6f56d..e7d5a7ad4 100644 --- a/astrbot/core/platform/sources/telegram/tg_adapter.py +++ b/astrbot/core/platform/sources/telegram/tg_adapter.py @@ -50,7 +50,6 @@ class TelegramPlatformAdapter(Platform): ) -> None: super().__init__(platform_config, event_queue) self.settings = platform_settings - self.client_self_id = uuid.uuid4().hex[:8] base_url = self.config.get( "telegram_api_base_url", @@ -336,6 +335,8 @@ class TelegramPlatformAdapter(Platform): return None def _apply_caption() -> None: + if not update.message: + return if update.message.caption: message.message_str = update.message.caption message.message.append(Comp.Plain(message.message_str)) diff --git a/astrbot/core/platform/sources/wecom/wecom_adapter.py b/astrbot/core/platform/sources/wecom/wecom_adapter.py index 410b30eea..a30afcd8f 100644 --- a/astrbot/core/platform/sources/wecom/wecom_adapter.py +++ b/astrbot/core/platform/sources/wecom/wecom_adapter.py @@ -148,7 +148,6 @@ class WecomPlatformAdapter(Platform): ) -> None: super().__init__(platform_config, event_queue) self.settingss = platform_settings - self.client_self_id = uuid.uuid4().hex[:8] self.api_base_url = platform_config.get( "api_base_url", "https://qyapi.weixin.qq.com/cgi-bin/", diff --git a/astrbot/core/platform/sources/weixin_official_account/weixin_offacc_adapter.py b/astrbot/core/platform/sources/weixin_official_account/weixin_offacc_adapter.py index bb7061ca1..8b646e43f 100644 --- a/astrbot/core/platform/sources/weixin_official_account/weixin_offacc_adapter.py +++ b/astrbot/core/platform/sources/weixin_official_account/weixin_offacc_adapter.py @@ -2,7 +2,6 @@ import asyncio import os import sys import time -import uuid from collections.abc import Callable, Coroutine from typing import Any, cast @@ -324,7 +323,6 @@ class WeixinOfficialAccountPlatformAdapter(Platform): ) -> None: super().__init__(platform_config, event_queue) self.settingss = platform_settings - self.client_self_id = uuid.uuid4().hex[:8] self.api_base_url = platform_config.get( "api_base_url", "https://api.weixin.qq.com/cgi-bin/",