From b8ccfe3f64a174d9536c4717d2dd1136ab044e63 Mon Sep 17 00:00:00 2001 From: MinaraAgent Date: Sat, 11 Apr 2026 01:27:46 +0800 Subject: [PATCH] feat(discord): add configurable bot message filtering, allow bot to receive other bots' messages (#6505) * feat(discord): add configurable bot message filtering Add `discord_allow_bot_messages` config option to allow receiving messages from other Discord bots. This is useful for bot-to-bot communication scenarios like message forwarding between channels. By default, bot messages are still ignored (backward compatible). Usage: Set `discord_allow_bot_messages: true` in your Discord platform configuration. Co-Authored-By: Claude Opus 4.6 * feat(discord): add WebUI config for discord_allow_bot_messages Add configuration option to the dashboard for the new discord_allow_bot_messages feature. Users can now enable/disable this option through the WebUI in all supported languages (zh-CN, en-US, ru-RU). Co-Authored-By: Claude Opus 4.6 * refactor(discord): use typed constructor argument for allow_bot_messages Address code review feedback: - Add `allow_bot_messages` as a typed constructor argument in DiscordBotClient - Simplify the on_message check by using the instance attribute directly - Pass the parameter in constructor instead of using setattr Co-Authored-By: Claude Opus 4.6 --------- Co-authored-by: MinaraAgent Co-authored-by: Claude Opus 4.6 --- astrbot/core/config/default.py | 6 ++++++ astrbot/core/platform/sources/discord/client.py | 7 +++++-- .../platform/sources/discord/discord_platform_adapter.py | 3 ++- .../src/i18n/locales/en-US/features/config-metadata.json | 4 ++++ .../src/i18n/locales/ru-RU/features/config-metadata.json | 4 ++++ .../src/i18n/locales/zh-CN/features/config-metadata.json | 4 ++++ 6 files changed, 25 insertions(+), 3 deletions(-) diff --git a/astrbot/core/config/default.py b/astrbot/core/config/default.py index c0fcf8df6..445df7a62 100644 --- a/astrbot/core/config/default.py +++ b/astrbot/core/config/default.py @@ -454,6 +454,7 @@ CONFIG_METADATA_2 = { "discord_proxy": "", "discord_command_register": True, "discord_activity_name": "", + "discord_allow_bot_messages": False, }, "Misskey": { "id": "misskey", @@ -919,6 +920,11 @@ CONFIG_METADATA_2 = { "type": "string", "hint": "可选的 Discord 活动名称。留空则不设置活动。", }, + "discord_allow_bot_messages": { + "description": "允许接收机器人消息", + "type": "bool", + "hint": "启用后,AstrBot 将接收来自其他 Discord 机器人的消息。适用于机器人间通信场景(如消息转发)。默认关闭。", + }, "port": { "description": "回调服务器端口", "type": "int", diff --git a/astrbot/core/platform/sources/discord/client.py b/astrbot/core/platform/sources/discord/client.py index 7bff9e39e..fb21ac3d8 100644 --- a/astrbot/core/platform/sources/discord/client.py +++ b/astrbot/core/platform/sources/discord/client.py @@ -15,9 +15,12 @@ else: class DiscordBotClient(discord.Bot): """Discord客户端封装""" - def __init__(self, token: str, proxy: str | None = None) -> None: + def __init__( + self, token: str, proxy: str | None = None, allow_bot_messages: bool = False + ) -> None: self.token = token self.proxy = proxy + self.allow_bot_messages = allow_bot_messages # 设置Intent权限,遵循权限最小化原则 intents = discord.Intents.default() @@ -95,7 +98,7 @@ class DiscordBotClient(discord.Bot): async def on_message(self, message: discord.Message) -> None: """当接收到消息时触发""" - if message.author.bot: + if message.author.bot and not self.allow_bot_messages: return logger.debug( diff --git a/astrbot/core/platform/sources/discord/discord_platform_adapter.py b/astrbot/core/platform/sources/discord/discord_platform_adapter.py index 19b9c81b4..c436117c3 100644 --- a/astrbot/core/platform/sources/discord/discord_platform_adapter.py +++ b/astrbot/core/platform/sources/discord/discord_platform_adapter.py @@ -142,7 +142,8 @@ class DiscordPlatformAdapter(Platform): return proxy = self.config.get("discord_proxy") or None - self.client = DiscordBotClient(token, proxy) + allow_bot_messages = bool(self.config.get("discord_allow_bot_messages")) + self.client = DiscordBotClient(token, proxy, allow_bot_messages) self.client.on_message_received = on_received async def callback() -> None: 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 20528f57c..6c5a73ead 100644 --- a/dashboard/src/i18n/locales/en-US/features/config-metadata.json +++ b/dashboard/src/i18n/locales/en-US/features/config-metadata.json @@ -390,6 +390,10 @@ "description": "Discord Activity Name", "hint": "Optional Discord activity name. Leave empty to disable." }, + "discord_allow_bot_messages": { + "description": "Allow Bot Messages", + "hint": "When enabled, AstrBot will receive messages from other Discord bots. Useful for bot-to-bot communication scenarios (e.g., message forwarding). Disabled by default." + }, "discord_command_register": { "description": "Register Discord slash commands", "hint": "When enabled, AstrBot will automatically register plugin commands as Discord slash commands" diff --git a/dashboard/src/i18n/locales/ru-RU/features/config-metadata.json b/dashboard/src/i18n/locales/ru-RU/features/config-metadata.json index 2ff332c0b..3a71b2428 100644 --- a/dashboard/src/i18n/locales/ru-RU/features/config-metadata.json +++ b/dashboard/src/i18n/locales/ru-RU/features/config-metadata.json @@ -390,6 +390,10 @@ "description": "Название активности Discord", "hint": "Статус бота в Discord. Оставьте пустым для отключения." }, + "discord_allow_bot_messages": { + "description": "Разрешить сообщения от ботов", + "hint": "Если включено, AstrBot будет получать сообщения от других Discord-ботов. Полезно для взаимодействия между ботами (например, пересылка сообщений). По умолчанию отключено." + }, "discord_command_register": { "description": "Регистрировать слэш-команды Discord", "hint": "Если включено, команды плагинов будут зарегистрированы как /команды Discord." 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 15be50564..58bc6290d 100644 --- a/dashboard/src/i18n/locales/zh-CN/features/config-metadata.json +++ b/dashboard/src/i18n/locales/zh-CN/features/config-metadata.json @@ -392,6 +392,10 @@ "description": "Discord 活动名称", "hint": "可选的 Discord 活动名称。留空则不设置活动。" }, + "discord_allow_bot_messages": { + "description": "允许接收机器人消息", + "hint": "启用后,AstrBot 将接收来自其他 Discord 机器人的消息。适用于机器人间通信场景(如消息转发)。默认关闭。" + }, "discord_command_register": { "description": "注册 Discord 指令", "hint": "启用后,自动将插件指令注册为 Discord 斜杠指令"