fix: resolve Discord/Misskey hot reload issue by fixing client_self_id misuse

fixes: #7187
closes: #7188
This commit is contained in:
Soulter
2026-04-03 13:41:56 +08:00
parent 5886c43752
commit 12039fe7d0
6 changed files with 34 additions and 36 deletions

View File

@@ -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)

View File

@@ -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:

View File

@@ -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],
}

View File

@@ -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))

View File

@@ -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/",

View File

@@ -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/",