Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions astrbot/core/platform/sources/discord/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,9 @@ def _create_interaction_data(self, interaction: discord.Interaction) -> dict:

async def on_message(self, message: discord.Message) -> None:
"""当接收到消息时触发"""
if self.user is not None and message.author.id == self.user.id:
return

if message.author.bot and not self.allow_bot_messages:
return

Expand Down
36 changes: 36 additions & 0 deletions tests/test_discord_adapter.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
discord_platform_adapter,
discord_platform_event,
)
from astrbot.core.platform.sources.discord.client import DiscordBotClient
from astrbot.core.platform.sources.discord.discord_platform_adapter import (
DiscordPlatformAdapter,
)
Expand All @@ -26,6 +27,41 @@
_WAV_PATH = "/tmp/discord_voice.wav"


@pytest.mark.asyncio
@pytest.mark.parametrize("allow_bot_messages", [False, True])
@pytest.mark.parametrize(("author_id", "is_bot"), [(1, True), (2, True), (3, False)])
async def test_discord_filters_self_messages(allow_bot_messages, author_id, is_bot):
"""Reject self messages while preserving the other-bot setting.

Args:
allow_bot_messages: Whether messages from other bots are enabled.
author_id: The sender ID; 1 is the connected bot.
is_bot: Whether the sender is a bot.
"""
client = DiscordBotClient(token="test", allow_bot_messages=allow_bot_messages)
client._connection.user = SimpleNamespace(id=1)
client.on_message_received = AsyncMock()
message = SimpleNamespace(
id=42,
author=SimpleNamespace(
id=author_id, bot=is_bot, name="tester", display_name="tester"
),
content="hello",
clean_content="hello",
channel=SimpleNamespace(id=123),
guild=None,
mentions=[],
)

await client.on_message(message)

if author_id == 1 or (is_bot and not allow_bot_messages):
client.on_message_received.assert_not_awaited()
else:
client.on_message_received.assert_awaited_once()
assert client.on_message_received.call_args.args[0]["message"] is message


@pytest.mark.asyncio
@pytest.mark.parametrize("component_type", ["video", "video_url", "file"])
@pytest.mark.parametrize("with_text", [False, True])
Expand Down
Loading