Skip to content

Commit b8ccfe3

Browse files
MinaraAgentMinaraAgentclaude
authored
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 <noreply@anthropic.com> * 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 <noreply@anthropic.com> * 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 <noreply@anthropic.com> --------- Co-authored-by: MinaraAgent <minara-agent@users.noreply.github.com> Co-authored-by: Claude Opus 4.6 <noreply@anthropic.com>
1 parent 574e508 commit b8ccfe3

6 files changed

Lines changed: 25 additions & 3 deletions

File tree

astrbot/core/config/default.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -454,6 +454,7 @@ class ChatProviderTemplate(TypedDict):
454454
"discord_proxy": "",
455455
"discord_command_register": True,
456456
"discord_activity_name": "",
457+
"discord_allow_bot_messages": False,
457458
},
458459
"Misskey": {
459460
"id": "misskey",
@@ -919,6 +920,11 @@ class ChatProviderTemplate(TypedDict):
919920
"type": "string",
920921
"hint": "可选的 Discord 活动名称。留空则不设置活动。",
921922
},
923+
"discord_allow_bot_messages": {
924+
"description": "允许接收机器人消息",
925+
"type": "bool",
926+
"hint": "启用后,AstrBot 将接收来自其他 Discord 机器人的消息。适用于机器人间通信场景(如消息转发)。默认关闭。",
927+
},
922928
"port": {
923929
"description": "回调服务器端口",
924930
"type": "int",

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

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,12 @@
1515
class DiscordBotClient(discord.Bot):
1616
"""Discord客户端封装"""
1717

18-
def __init__(self, token: str, proxy: str | None = None) -> None:
18+
def __init__(
19+
self, token: str, proxy: str | None = None, allow_bot_messages: bool = False
20+
) -> None:
1921
self.token = token
2022
self.proxy = proxy
23+
self.allow_bot_messages = allow_bot_messages
2124

2225
# 设置Intent权限,遵循权限最小化原则
2326
intents = discord.Intents.default()
@@ -95,7 +98,7 @@ def _create_interaction_data(self, interaction: discord.Interaction) -> dict:
9598

9699
async def on_message(self, message: discord.Message) -> None:
97100
"""当接收到消息时触发"""
98-
if message.author.bot:
101+
if message.author.bot and not self.allow_bot_messages:
99102
return
100103

101104
logger.debug(

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,8 @@ async def on_received(message_data) -> None:
142142
return
143143

144144
proxy = self.config.get("discord_proxy") or None
145-
self.client = DiscordBotClient(token, proxy)
145+
allow_bot_messages = bool(self.config.get("discord_allow_bot_messages"))
146+
self.client = DiscordBotClient(token, proxy, allow_bot_messages)
146147
self.client.on_message_received = on_received
147148

148149
async def callback() -> None:

dashboard/src/i18n/locales/en-US/features/config-metadata.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -390,6 +390,10 @@
390390
"description": "Discord Activity Name",
391391
"hint": "Optional Discord activity name. Leave empty to disable."
392392
},
393+
"discord_allow_bot_messages": {
394+
"description": "Allow Bot Messages",
395+
"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."
396+
},
393397
"discord_command_register": {
394398
"description": "Register Discord slash commands",
395399
"hint": "When enabled, AstrBot will automatically register plugin commands as Discord slash commands"

dashboard/src/i18n/locales/ru-RU/features/config-metadata.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -390,6 +390,10 @@
390390
"description": "Название активности Discord",
391391
"hint": "Статус бота в Discord. Оставьте пустым для отключения."
392392
},
393+
"discord_allow_bot_messages": {
394+
"description": "Разрешить сообщения от ботов",
395+
"hint": "Если включено, AstrBot будет получать сообщения от других Discord-ботов. Полезно для взаимодействия между ботами (например, пересылка сообщений). По умолчанию отключено."
396+
},
393397
"discord_command_register": {
394398
"description": "Регистрировать слэш-команды Discord",
395399
"hint": "Если включено, команды плагинов будут зарегистрированы как /команды Discord."

dashboard/src/i18n/locales/zh-CN/features/config-metadata.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -392,6 +392,10 @@
392392
"description": "Discord 活动名称",
393393
"hint": "可选的 Discord 活动名称。留空则不设置活动。"
394394
},
395+
"discord_allow_bot_messages": {
396+
"description": "允许接收机器人消息",
397+
"hint": "启用后,AstrBot 将接收来自其他 Discord 机器人的消息。适用于机器人间通信场景(如消息转发)。默认关闭。"
398+
},
395399
"discord_command_register": {
396400
"description": "注册 Discord 指令",
397401
"hint": "启用后,自动将插件指令注册为 Discord 斜杠指令"

0 commit comments

Comments
 (0)