Skip to content

fix: treat Discord voice attachments as records#8036

Open
p1930n wants to merge 2 commits intoAstrBotDevs:masterfrom
p1930n:fix/7580-discord-voice-record
Open

fix: treat Discord voice attachments as records#8036
p1930n wants to merge 2 commits intoAstrBotDevs:masterfrom
p1930n:fix/7580-discord-voice-record

Conversation

@p1930n
Copy link
Copy Markdown

@p1930n p1930n commented May 6, 2026

Fixes #7580.

Modifications / 改动点

  • This is NOT a breaking change. / 这不是一个破坏性变更。

This PR fixes Discord voice-message handling by mapping audio attachments to Record instead of generic File components. The adapter now detects audio attachments by normalized audio/* content type, with a filename-extension fallback for Discord attachments that do not expose a content type.

It also keeps the outgoing path compatible with the new component type by allowing DiscordPlatformEvent._parse_to_discord() to send Record components as Discord files. Image attachments still map to Image, and non-audio non-image attachments still map to File.

Screenshots or Test Results / 运行截图或测试结果

  • Runtime verification with the Discord adapter enabled on astrbot:issue-7580-local:
    • Sending a Discord voice message logs [ComponentType.Record].
  • docker run --rm -v "D:\AstrBot - PRs\AstrBot_issue_7580:/workspace" -w /workspace astrbot:issue-7580-local sh -lc "python -m pip install -q pytest pytest-asyncio && python -m pytest tests/test_discord_adapter.py tests/test_mattermost_adapter.py tests/test_telegram_adapter.py -q"
    • 17 passed, 1 warning
  • docker run --rm -v "D:\AstrBot - PRs\AstrBot_issue_7580:/workspace" -w /workspace astrbot:issue-7580-local sh -lc "python -m pip install -q ruff && ruff check astrbot/core/platform/sources/discord/discord_platform_adapter.py astrbot/core/platform/sources/discord/discord_platform_event.py tests/test_discord_adapter.py && ruff format --check astrbot/core/platform/sources/discord/discord_platform_adapter.py astrbot/core/platform/sources/discord/discord_platform_event.py tests/test_discord_adapter.py"
    • All checks passed!
    • 3 files already formatted

Checklist / 检查清单

  • If there are new features added in the PR, I have discussed it with the authors through issues/emails, etc. / 如果 PR 中有新加入的功能,已经通过 Issue / 邮件等方式和作者讨论过。
  • My changes have been well-tested, and verification steps and runtime results have been provided above. / 我的更改经过了良好的测试,并已在上方提供验证步骤和运行结果。
  • I have ensured that no new dependencies are introduced, OR if new dependencies are introduced, they have been added to the appropriate locations in requirements.txt and pyproject.toml. / 我确保没有引入新依赖库,或者引入了新依赖库的同时将其添加到 requirements.txtpyproject.toml 文件相应位置。
  • My changes do not introduce malicious code. / 我的更改没有引入恶意代码。

Summary by Sourcery

Handle Discord voice attachments as audio records instead of generic files while preserving compatibility with outbound Discord messages.

New Features:

  • Map Discord audio attachments to Record components using content type and filename extension detection.

Bug Fixes:

  • Correctly classify Discord voice-message attachments so they are treated as audio records rather than generic files.

Enhancements:

  • Preserve original URLs when converting Discord audio attachments into Record components.
  • Allow outgoing Record components to be sent to Discord as file uploads with appropriate filenames.

Tests:

  • Add unit tests covering Discord attachment type mapping, URL preservation for audio records, and sending Record components as Discord files.

Copy link
Copy Markdown
Contributor

@gemini-code-assist gemini-code-assist Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request adds support for audio attachments in the Discord platform adapter by mapping them to the Record component and provides unit tests for this functionality. The review feedback identifies a potential regression in message sending because the Record component is not handled by the platform's message parser. Additionally, optimizations were suggested to pass the already computed content type to the audio detection logic to avoid redundant processing.

Comment thread astrbot/core/platform/sources/discord/discord_platform_adapter.py
Comment thread astrbot/core/platform/sources/discord/discord_platform_adapter.py Outdated
Comment thread astrbot/core/platform/sources/discord/discord_platform_adapter.py Outdated
@p1930n p1930n changed the title [codex] fix Discord voice attachments as records fix: treat Discord voice attachments as records May 6, 2026
@p1930n
Copy link
Copy Markdown
Author

p1930n commented May 6, 2026

Fixed in abf78b4.

Addressed all three review points:

  1. Added Record support in DiscordPlatformEvent._parse_to_discord(), so Discord audio components are not dropped when re-sent in bridge/echo-style flows.
  2. Added a unit test covering Record -> discord.File parsing.
  3. Updated _is_audio_attachment() to reuse the already computed content_type, and updated the call site accordingly.

@p1930n p1930n marked this pull request as ready for review May 6, 2026 08:20
@auto-assign auto-assign Bot requested review from Raven95676 and Soulter May 6, 2026 08:20
@dosubot dosubot Bot added size:M This PR changes 30-99 lines, ignoring generated files. area:platform The bug / feature is about IM platform adapter, such as QQ, Lark, Telegram, WebChat and so on. labels May 6, 2026
Copy link
Copy Markdown
Contributor

@sourcery-ai sourcery-ai Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey - I've reviewed your changes and they look great!


Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

@p1930n
Copy link
Copy Markdown
Author

p1930n commented May 6, 2026

Fixed in abf78b4.

Record is now handled in DiscordPlatformEvent._parse_to_discord(), with unit test coverage. _is_audio_attachment() also reuses the computed content_type as suggested

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area:platform The bug / feature is about IM platform adapter, such as QQ, Lark, Telegram, WebChat and so on. size:M This PR changes 30-99 lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug]Discord 适配器(Adapter)没有针对 Voice Message 做特殊映射

1 participant