Skip to content

Commit fe0cbce

Browse files
MinaraAgentclaude
authored andcommitted
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>
1 parent 2aca47f commit fe0cbce

File tree

2 files changed

+6
-7
lines changed

2 files changed

+6
-7
lines changed

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

Lines changed: 5 additions & 4 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,9 +98,7 @@ def _create_interaction_data(self, interaction: discord.Interaction) -> dict:
9598

9699
async def on_message(self, message: discord.Message) -> None:
97100
"""当接收到消息时触发"""
98-
# 如果配置不允许接收 bot 的消息,则忽略 bot 消息(向后兼容原行为)
99-
allow_bots = getattr(self, "allow_bot_messages", False)
100-
if message.author.bot and not allow_bots:
101+
if message.author.bot and not self.allow_bot_messages:
101102
return
102103

103104
logger.debug(

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

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -140,10 +140,8 @@ async def on_received(message_data) -> None:
140140
return
141141

142142
proxy = self.config.get("discord_proxy") or None
143-
self.client = DiscordBotClient(token, proxy)
144-
# 从配置读取是否允许接收其他 bot 的消息
145143
allow_bot_messages = bool(self.config.get("discord_allow_bot_messages"))
146-
setattr(self.client, "allow_bot_messages", allow_bot_messages)
144+
self.client = DiscordBotClient(token, proxy, allow_bot_messages)
147145
self.client.on_message_received = on_received
148146

149147
async def callback() -> None:

0 commit comments

Comments
 (0)