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>
This commit is contained in:
MinaraAgent
2026-04-11 01:27:46 +08:00
committed by GitHub
co-authored by Claude Opus 4.6 MinaraAgent
parent 574e5089ba
commit b8ccfe3f64
6 changed files with 25 additions and 3 deletions
+6
View File
@@ -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",
@@ -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(
@@ -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:
@@ -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"
@@ -390,6 +390,10 @@
"description": "Название активности Discord",
"hint": "Статус бота в Discord. Оставьте пустым для отключения."
},
"discord_allow_bot_messages": {
"description": "Разрешить сообщения от ботов",
"hint": "Если включено, AstrBot будет получать сообщения от других Discord-ботов. Полезно для взаимодействия между ботами (например, пересылка сообщений). По умолчанию отключено."
},
"discord_command_register": {
"description": "Регистрировать слэш-команды Discord",
"hint": "Если включено, команды плагинов будут зарегистрированы как /команды Discord."
@@ -392,6 +392,10 @@
"description": "Discord 活动名称",
"hint": "可选的 Discord 活动名称。留空则不设置活动。"
},
"discord_allow_bot_messages": {
"description": "允许接收机器人消息",
"hint": "启用后,AstrBot 将接收来自其他 Discord 机器人的消息。适用于机器人间通信场景(如消息转发)。默认关闭。"
},
"discord_command_register": {
"description": "注册 Discord 指令",
"hint": "启用后,自动将插件指令注册为 Discord 斜杠指令"