From a4459a5c9212181c6a1e3226edccfab0f1683653 Mon Sep 17 00:00:00 2001 From: shuiping233 <1944680304@qq.com> Date: Tue, 3 Mar 2026 15:55:00 +0800 Subject: [PATCH 1/2] =?UTF-8?q?refactor:=20=E4=BD=BF=E7=94=A8kook=E6=8E=A5?= =?UTF-8?q?=E5=8F=A3=E4=B8=AD=E8=8E=B7=E5=8F=96=E7=9A=84bot=5Fid=E5=88=A4?= =?UTF-8?q?=E6=96=AD=E6=98=AF=E5=90=A6=E8=BF=87=E6=BB=A4=E6=9D=A5=E8=87=AA?= =?UTF-8?q?bot=E8=87=AA=E8=BA=AB=E7=9A=84=E4=BF=A1=E6=81=AF,=E8=80=8C?= =?UTF-8?q?=E4=B8=8D=E6=98=AF=E7=94=A8=E6=88=B7=E6=89=8B=E5=8A=A8=E8=BE=93?= =?UTF-8?q?=E5=85=A5=E7=9A=84bot=5Fnickname=E6=9D=A5=E5=88=A4=E6=96=AD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- astrbot/core/config/default.py | 6 ------ .../platform/sources/kook/kook_adapter.py | 20 ++++++------------- .../core/platform/sources/kook/kook_config.py | 2 -- .../en-US/features/config-metadata.json | 5 ----- .../zh-CN/features/config-metadata.json | 5 ----- 5 files changed, 6 insertions(+), 32 deletions(-) diff --git a/astrbot/core/config/default.py b/astrbot/core/config/default.py index cbadb5c18f..ec6bc423b4 100644 --- a/astrbot/core/config/default.py +++ b/astrbot/core/config/default.py @@ -454,7 +454,6 @@ class ChatProviderTemplate(TypedDict): "type": "kook", "enable": False, "kook_bot_token": "", - "kook_bot_nickname": "", "kook_reconnect_delay": 1, "kook_max_reconnect_delay": 60, "kook_max_retry_delay": 60, @@ -809,11 +808,6 @@ class ChatProviderTemplate(TypedDict): "type": "string", "hint": "必填项。从 KOOK 开发者平台获取的机器人 Token。", }, - "kook_bot_nickname": { - "description": "Bot Nickname", - "type": "string", - "hint": "可选项。若发送者昵称与此值一致,将忽略该消息以避免广播风暴。", - }, "kook_reconnect_delay": { "description": "重连延迟", "type": "int", diff --git a/astrbot/core/platform/sources/kook/kook_adapter.py b/astrbot/core/platform/sources/kook/kook_adapter.py index 1124c6841d..872ee6000b 100644 --- a/astrbot/core/platform/sources/kook/kook_adapter.py +++ b/astrbot/core/platform/sources/kook/kook_adapter.py @@ -57,20 +57,12 @@ def meta(self) -> PlatformMetadata: name="kook", description="KOOK 适配器", id=self.kook_config.id ) - def _should_ignore_event_by_bot_nickname(self, payload: dict) -> bool: - bot_nickname = self.kook_config.bot_nickname.strip() - if not bot_nickname: + def _should_ignore_event_by_bot_self(self, payload: dict) -> bool: + bot_id = self.client.bot_id + author_id: str = payload.get("author_id", "") + if not isinstance(author_id, str): return False - - author = payload.get("extra", {}).get("author", {}) - if not isinstance(author, dict): - return False - - author_nickname = author.get("nickname") or author.get("username") or "" - if not isinstance(author_nickname, str): - author_nickname = str(author_nickname) - - return author_nickname.strip().casefold() == bot_nickname.casefold() + return bot_id == author_id async def _on_received(self, data: dict): logger.debug(f"KOOK 收到数据: {data}") @@ -79,7 +71,7 @@ async def _on_received(self, data: dict): event_type = payload.get("type") # 支持type=9(文本)和type=10(卡片) if event_type in (9, 10): - if self._should_ignore_event_by_bot_nickname(payload): + if self._should_ignore_event_by_bot_self(payload): return try: abm = await self.convert_message(payload) diff --git a/astrbot/core/platform/sources/kook/kook_config.py b/astrbot/core/platform/sources/kook/kook_config.py index 21f2547b03..0b9d180a29 100644 --- a/astrbot/core/platform/sources/kook/kook_config.py +++ b/astrbot/core/platform/sources/kook/kook_config.py @@ -9,7 +9,6 @@ class KookConfig: # 基础配置 token: str - bot_nickname: str = "" enable: bool = False id: str = "kook" @@ -41,7 +40,6 @@ def from_dict(cls, config_dict: dict) -> "KookConfig": # id=config_dict.get("id", "kook"), enable=config_dict.get("enable", False), token=config_dict.get("kook_bot_token", ""), - bot_nickname=config_dict.get("kook_bot_nickname", ""), reconnect_delay=config_dict.get( "kook_reconnect_delay", KookConfig.reconnect_delay, diff --git a/dashboard/src/i18n/locales/en-US/features/config-metadata.json b/dashboard/src/i18n/locales/en-US/features/config-metadata.json index a143678c23..b69225b078 100644 --- a/dashboard/src/i18n/locales/en-US/features/config-metadata.json +++ b/dashboard/src/i18n/locales/en-US/features/config-metadata.json @@ -590,11 +590,6 @@ "type": "string", "hint": "Required. The Bot Token obtained from the KOOK Developer Platform." }, - "kook_bot_nickname": { - "description": "Bot Nickname", - "type": "string", - "hint": "Optional. If the sender nickname matches this value, the message will be ignored to prevent broadcast storms." - }, "kook_reconnect_delay": { "description": "Reconnect Delay", "type": "int", diff --git a/dashboard/src/i18n/locales/zh-CN/features/config-metadata.json b/dashboard/src/i18n/locales/zh-CN/features/config-metadata.json index 015ce3082c..e5ffa96df7 100644 --- a/dashboard/src/i18n/locales/zh-CN/features/config-metadata.json +++ b/dashboard/src/i18n/locales/zh-CN/features/config-metadata.json @@ -593,11 +593,6 @@ "type": "string", "hint": "必填项。从 KOOK 开发者平台获取的机器人 Token" }, - "kook_bot_nickname": { - "description": "Bot Nickname", - "type": "string", - "hint": "可选项。若发送者昵称与此值一致,将忽略该消息。" - }, "kook_reconnect_delay": { "description": "重连延迟", "type": "int", From 931e01c5b2aece36d8719ed75099ec0da938d2d6 Mon Sep 17 00:00:00 2001 From: shuiping233 <1944680304@qq.com> Date: Tue, 3 Mar 2026 16:27:57 +0800 Subject: [PATCH 2/2] =?UTF-8?q?refactor:=20=E6=B7=BB=E5=8A=A0bot=5Fid?= =?UTF-8?q?=E7=A9=BA=E5=80=BC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- astrbot/core/platform/sources/kook/kook_adapter.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/astrbot/core/platform/sources/kook/kook_adapter.py b/astrbot/core/platform/sources/kook/kook_adapter.py index 872ee6000b..48e0bf5958 100644 --- a/astrbot/core/platform/sources/kook/kook_adapter.py +++ b/astrbot/core/platform/sources/kook/kook_adapter.py @@ -59,8 +59,10 @@ def meta(self) -> PlatformMetadata: def _should_ignore_event_by_bot_self(self, payload: dict) -> bool: bot_id = self.client.bot_id - author_id: str = payload.get("author_id", "") - if not isinstance(author_id, str): + if not bot_id: + logger.warning("[kook] 发现 bot_id 为空, 将无法过滤来自机器人自身的消息") + author_id: str | None = payload.get("author_id") + if not author_id: return False return bot_id == author_id