|
12 | 12 | discord_platform_adapter, |
13 | 13 | discord_platform_event, |
14 | 14 | ) |
| 15 | +from astrbot.core.platform.sources.discord.client import DiscordBotClient |
15 | 16 | from astrbot.core.platform.sources.discord.discord_platform_adapter import ( |
16 | 17 | DiscordPlatformAdapter, |
17 | 18 | ) |
|
26 | 27 | _WAV_PATH = "/tmp/discord_voice.wav" |
27 | 28 |
|
28 | 29 |
|
| 30 | +@pytest.mark.asyncio |
| 31 | +@pytest.mark.parametrize("allow_bot_messages", [False, True]) |
| 32 | +@pytest.mark.parametrize(("author_id", "is_bot"), [(1, True), (2, True), (3, False)]) |
| 33 | +async def test_discord_filters_self_messages(allow_bot_messages, author_id, is_bot): |
| 34 | + """Reject self messages while preserving the other-bot setting. |
| 35 | +
|
| 36 | + Args: |
| 37 | + allow_bot_messages: Whether messages from other bots are enabled. |
| 38 | + author_id: The sender ID; 1 is the connected bot. |
| 39 | + is_bot: Whether the sender is a bot. |
| 40 | + """ |
| 41 | + client = DiscordBotClient(token="test", allow_bot_messages=allow_bot_messages) |
| 42 | + client._connection.user = SimpleNamespace(id=1) |
| 43 | + client.on_message_received = AsyncMock() |
| 44 | + message = SimpleNamespace( |
| 45 | + id=42, |
| 46 | + author=SimpleNamespace( |
| 47 | + id=author_id, bot=is_bot, name="tester", display_name="tester" |
| 48 | + ), |
| 49 | + content="hello", |
| 50 | + clean_content="hello", |
| 51 | + channel=SimpleNamespace(id=123), |
| 52 | + guild=None, |
| 53 | + mentions=[], |
| 54 | + ) |
| 55 | + |
| 56 | + await client.on_message(message) |
| 57 | + |
| 58 | + if author_id == 1 or (is_bot and not allow_bot_messages): |
| 59 | + client.on_message_received.assert_not_awaited() |
| 60 | + else: |
| 61 | + client.on_message_received.assert_awaited_once() |
| 62 | + assert client.on_message_received.call_args.args[0]["message"] is message |
| 63 | + |
| 64 | + |
29 | 65 | @pytest.mark.asyncio |
30 | 66 | @pytest.mark.parametrize("component_type", ["video", "video_url", "file"]) |
31 | 67 | @pytest.mark.parametrize("with_text", [False, True]) |
|
0 commit comments