Skip to content

Commit 12039fe

Browse files
committed
fix: resolve Discord/Misskey hot reload issue by fixing client_self_id misuse
fixes: #7187 closes: #7188
1 parent 5886c43 commit 12039fe

6 files changed

Lines changed: 34 additions & 36 deletions

File tree

astrbot/core/platform/sources/discord/discord_platform_adapter.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ def __init__(
4646
) -> None:
4747
super().__init__(platform_config, event_queue)
4848
self.settings = platform_settings
49-
self.client_self_id: str | None = None
49+
self.bot_self_id: str | None = None
5050
self.registered_handlers = []
5151
# 指令注册相关
5252
self.enable_command_register = self.config.get("discord_command_register", True)
@@ -92,10 +92,10 @@ async def send_by_session(
9292

9393
message_obj.message_str = message_chain.get_plain_text()
9494
message_obj.sender = MessageMember(
95-
user_id=str(self.client_self_id),
95+
user_id=str(self.bot_self_id),
9696
nickname=self.client.user.display_name,
9797
)
98-
message_obj.self_id = cast(str, self.client_self_id)
98+
message_obj.self_id = cast(str, self.bot_self_id)
9999
message_obj.session_id = session.session_id
100100
message_obj.message = message_chain.chain
101101

@@ -128,8 +128,8 @@ async def run(self) -> None:
128128
# 初始化回调函数
129129
async def on_received(message_data) -> None:
130130
logger.debug(f"[Discord] 收到消息: {message_data}")
131-
if self.client_self_id is None:
132-
self.client_self_id = message_data.get("bot_id")
131+
if self.bot_self_id is None:
132+
self.bot_self_id = message_data.get("bot_id")
133133
abm = await self.convert_message(data=message_data)
134134
await self.handle_msg(abm)
135135

@@ -241,7 +241,7 @@ def _convert_message_to_abm(self, data: dict) -> AstrBotMessage:
241241
)
242242
abm.message = message_chain
243243
abm.raw_message = message
244-
abm.self_id = cast(str, self.client_self_id)
244+
abm.self_id = cast(str, self.bot_self_id)
245245
abm.session_id = str(message.channel.id)
246246
abm.message_id = str(message.id)
247247
return abm
@@ -465,7 +465,7 @@ async def dynamic_callback(
465465
)
466466
abm.message = [Plain(text=message_str_for_filter)]
467467
abm.raw_message = ctx.interaction
468-
abm.self_id = cast(str, self.client_self_id)
468+
abm.self_id = cast(str, self.bot_self_id)
469469
abm.session_id = str(ctx.channel_id)
470470
abm.message_id = str(ctx.interaction.id)
471471

astrbot/core/platform/sources/misskey/misskey_adapter.py

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ def __init__(
9393

9494
self.api: MisskeyAPI | None = None
9595
self._running = False
96-
self.client_self_id = ""
96+
self.bot_self_id = ""
9797
self._bot_username = ""
9898
self._user_cache = {}
9999

@@ -138,10 +138,10 @@ async def run(self) -> None:
138138

139139
try:
140140
user_info = await self.api.get_current_user()
141-
self.client_self_id = str(user_info.get("id", ""))
141+
self.bot_self_id = str(user_info.get("id", ""))
142142
self._bot_username = user_info.get("username", "")
143143
logger.info(
144-
f"[Misskey] 已连接用户: {self._bot_username} (ID: {self.client_self_id})",
144+
f"[Misskey] 已连接用户: {self._bot_username} (ID: {self.bot_self_id})",
145145
)
146146
except Exception as e:
147147
logger.error(f"[Misskey] 获取用户信息失败: {e}")
@@ -312,9 +312,9 @@ async def _handle_chat_message(self, data: dict[str, Any]) -> None:
312312
)
313313
room_id = data.get("toRoomId")
314314
logger.debug(
315-
f"[Misskey] 收到聊天事件: sender_id={sender_id}, room_id={room_id}, is_self={sender_id == self.client_self_id}",
315+
f"[Misskey] 收到聊天事件: sender_id={sender_id}, room_id={room_id}, is_self={sender_id == self.bot_self_id}",
316316
)
317-
if sender_id == self.client_self_id:
317+
if sender_id == self.bot_self_id:
318318
return
319319

320320
if room_id:
@@ -354,13 +354,13 @@ def _is_bot_mentioned(self, note: dict[str, Any]) -> bool:
354354
mentions = note.get("mentions", [])
355355
if self._bot_username and f"@{self._bot_username}" in text:
356356
return True
357-
if self.client_self_id in [str(uid) for uid in mentions]:
357+
if self.bot_self_id in [str(uid) for uid in mentions]:
358358
return True
359359

360360
reply = note.get("reply")
361361
if reply and isinstance(reply, dict):
362362
reply_user_id = str(reply.get("user", {}).get("id", ""))
363-
if reply_user_id == self.client_self_id:
363+
if reply_user_id == self.bot_self_id:
364364
return bool(self._bot_username and f"@{self._bot_username}" in text)
365365

366366
return False
@@ -598,7 +598,7 @@ async def _upload_comp(comp) -> object | None:
598598
visibility, visible_user_ids = resolve_message_visibility(
599599
user_id=user_id_for_cache,
600600
user_cache=self._user_cache,
601-
self_id=self.client_self_id,
601+
self_id=self.bot_self_id,
602602
default_visibility=self.default_visibility,
603603
)
604604
logger.debug(
@@ -637,14 +637,14 @@ async def convert_message(self, raw_data: dict[str, Any]) -> AstrBotMessage:
637637
message = create_base_message(
638638
raw_data,
639639
sender_info,
640-
self.client_self_id,
640+
self.bot_self_id,
641641
is_chat=False,
642642
)
643643
cache_user_info(
644644
self._user_cache,
645645
sender_info,
646646
raw_data,
647-
self.client_self_id,
647+
self.bot_self_id,
648648
is_chat=False,
649649
)
650650

@@ -656,7 +656,7 @@ async def convert_message(self, raw_data: dict[str, Any]) -> AstrBotMessage:
656656
message,
657657
raw_text,
658658
self._bot_username,
659-
self.client_self_id,
659+
self.bot_self_id,
660660
)
661661
message_parts.extend(text_parts)
662662

@@ -685,14 +685,14 @@ async def convert_chat_message(self, raw_data: dict[str, Any]) -> AstrBotMessage
685685
message = create_base_message(
686686
raw_data,
687687
sender_info,
688-
self.client_self_id,
688+
self.bot_self_id,
689689
is_chat=True,
690690
)
691691
cache_user_info(
692692
self._user_cache,
693693
sender_info,
694694
raw_data,
695-
self.client_self_id,
695+
self.bot_self_id,
696696
is_chat=True,
697697
)
698698

@@ -713,7 +713,7 @@ async def convert_room_message(self, raw_data: dict[str, Any]) -> AstrBotMessage
713713
message = create_base_message(
714714
raw_data,
715715
sender_info,
716-
self.client_self_id,
716+
self.bot_self_id,
717717
is_chat=False,
718718
room_id=room_id,
719719
)
@@ -722,10 +722,10 @@ async def convert_room_message(self, raw_data: dict[str, Any]) -> AstrBotMessage
722722
self._user_cache,
723723
sender_info,
724724
raw_data,
725-
self.client_self_id,
725+
self.bot_self_id,
726726
is_chat=False,
727727
)
728-
cache_room_info(self._user_cache, raw_data, self.client_self_id)
728+
cache_room_info(self._user_cache, raw_data, self.bot_self_id)
729729

730730
raw_text = raw_data.get("text", "")
731731
message_parts = []
@@ -736,7 +736,7 @@ async def convert_room_message(self, raw_data: dict[str, Any]) -> AstrBotMessage
736736
message,
737737
raw_text,
738738
self._bot_username,
739-
self.client_self_id,
739+
self.bot_self_id,
740740
)
741741
message_parts.extend(text_parts)
742742
else:

astrbot/core/platform/sources/misskey/misskey_utils.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -335,7 +335,7 @@ def extract_sender_info(
335335
def create_base_message(
336336
raw_data: dict[str, Any],
337337
sender_info: dict[str, Any],
338-
client_self_id: str,
338+
bot_self_id: str,
339339
is_chat: bool = False,
340340
room_id: str | None = None,
341341
) -> AstrBotMessage:
@@ -367,7 +367,7 @@ def create_base_message(
367367
session_id if sender_info["sender_id"] else f"{session_prefix}%unknown"
368368
)
369369
message.message_id = str(raw_data.get("id", ""))
370-
message.self_id = client_self_id
370+
message.self_id = bot_self_id
371371

372372
return message
373373

@@ -376,7 +376,7 @@ def process_at_mention(
376376
message: AstrBotMessage,
377377
raw_text: str,
378378
bot_username: str,
379-
client_self_id: str,
379+
bot_self_id: str,
380380
) -> tuple[list[str], str]:
381381
"""处理@提及逻辑,返回消息部分列表和处理后的文本"""
382382
message_parts = []
@@ -386,7 +386,7 @@ def process_at_mention(
386386

387387
if bot_username and raw_text.startswith(f"@{bot_username}"):
388388
at_mention = f"@{bot_username}"
389-
message.message.append(Comp.At(qq=client_self_id))
389+
message.message.append(Comp.At(qq=bot_self_id))
390390
remaining_text = raw_text[len(at_mention) :].strip()
391391
if remaining_text:
392392
message.message.append(Comp.Plain(remaining_text))
@@ -401,7 +401,7 @@ def cache_user_info(
401401
user_cache: dict[str, Any],
402402
sender_info: dict[str, Any],
403403
raw_data: dict[str, Any],
404-
client_self_id: str,
404+
bot_self_id: str,
405405
is_chat: bool = False,
406406
) -> None:
407407
"""缓存用户信息"""
@@ -410,7 +410,7 @@ def cache_user_info(
410410
"username": sender_info["username"],
411411
"nickname": sender_info["nickname"],
412412
"visibility": "specified",
413-
"visible_user_ids": [client_self_id, sender_info["sender_id"]],
413+
"visible_user_ids": [bot_self_id, sender_info["sender_id"]],
414414
}
415415
else:
416416
user_cache_data = {
@@ -428,7 +428,7 @@ def cache_user_info(
428428
def cache_room_info(
429429
user_cache: dict[str, Any],
430430
raw_data: dict[str, Any],
431-
client_self_id: str,
431+
bot_self_id: str,
432432
) -> None:
433433
"""缓存房间信息"""
434434
room_data = raw_data.get("toRoom")
@@ -442,7 +442,7 @@ def cache_room_info(
442442
"room_description": room_data.get("description", ""),
443443
"owner_id": room_data.get("ownerId", ""),
444444
"visibility": "specified",
445-
"visible_user_ids": [client_self_id],
445+
"visible_user_ids": [bot_self_id],
446446
}
447447

448448

astrbot/core/platform/sources/telegram/tg_adapter.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,6 @@ def __init__(
5050
) -> None:
5151
super().__init__(platform_config, event_queue)
5252
self.settings = platform_settings
53-
self.client_self_id = uuid.uuid4().hex[:8]
5453

5554
base_url = self.config.get(
5655
"telegram_api_base_url",
@@ -336,6 +335,8 @@ async def convert_message(
336335
return None
337336

338337
def _apply_caption() -> None:
338+
if not update.message:
339+
return
339340
if update.message.caption:
340341
message.message_str = update.message.caption
341342
message.message.append(Comp.Plain(message.message_str))

astrbot/core/platform/sources/wecom/wecom_adapter.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,6 @@ def __init__(
148148
) -> None:
149149
super().__init__(platform_config, event_queue)
150150
self.settingss = platform_settings
151-
self.client_self_id = uuid.uuid4().hex[:8]
152151
self.api_base_url = platform_config.get(
153152
"api_base_url",
154153
"https://qyapi.weixin.qq.com/cgi-bin/",

astrbot/core/platform/sources/weixin_official_account/weixin_offacc_adapter.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
import os
33
import sys
44
import time
5-
import uuid
65
from collections.abc import Callable, Coroutine
76
from typing import Any, cast
87

@@ -324,7 +323,6 @@ def __init__(
324323
) -> None:
325324
super().__init__(platform_config, event_queue)
326325
self.settingss = platform_settings
327-
self.client_self_id = uuid.uuid4().hex[:8]
328326
self.api_base_url = platform_config.get(
329327
"api_base_url",
330328
"https://api.weixin.qq.com/cgi-bin/",

0 commit comments

Comments
 (0)