Skip to content

fix: send Discord video components as attachments - #10423

Open
wcqqq1214 wants to merge 3 commits into
AstrBotDevs:masterfrom
wcqqq1214:fix/discord-video-send
Open

wcqqq1214 wants to merge 3 commits into
AstrBotDevs:masterfrom
wcqqq1214:fix/discord-video-send

Conversation

@wcqqq1214

@wcqqq1214 wcqqq1214 commented Oct 6, 2026 •

Copy link
Copy Markdown
Contributor

Discord silently drops Video components from send_message_to_user while the tool reports success. This change sends them as attachments, including when combined with text.

Modifications / 改动点

  • Resolve videos through the existing Video.convert_to_file_path() method and stream them as Discord attachments, preserving URL filenames when available.

  • Cover local/URL videos and file uploads with and without text, plus URL filename decoding and fallback behavior.

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

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

Verification: run pytest tests/test_discord_adapter.py tests/unit/test_message_tools.py -q (102 passed). ruff format ., ruff check ., and git diff --check passed.

Manual verification: send the same MP4 to the current Discord session using video, then plain text + video; confirm the attachment arrives in both cases. Use file as a control. Post-fix Discord verification is pending.

Before

Before

After

After

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 "Screenshots" have been provided above.
    / 我的更改经过了良好的测试,并已在上方提供了“验证步骤”和“运行截图”。

  • 📚 I checked the affected WebUI instructions and screenshots in docs/zh and docs/en against the changed navigation, page structure, and labels, and updated them in this PR (or explained why no documentation update is needed). For renamed, moved, or merged entry points, I included an old entry → new entry mapping in the documentation and changelog.
    / 我已对照变化后的 WebUI 入口、页面结构和术语,核对并在本 PR 中更新 docs/zh 和 docs/en 的相关操作说明与截图(或说明无需更新文档的原因)。入口改名、移动或合并时,已在文档和 changelog 中补充 旧入口 → 新入口 对照。

  • 🤓 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.txt 和 pyproject.toml 文件相应位置。

  • 😮 My changes do not introduce malicious code.
    / 我的更改没有引入恶意代码。

Summary by Sourcery

Ensure Discord video content is delivered as attachments instead of silently dropped components.

Bug Fixes:

  • Send Discord video components as file attachments so they are delivered reliably, including when combined with text.

Enhancements:

  • Preserve decoded filenames from video URLs while falling back to the resolved local filename when the URL has no usable filename.

Tests:

  • Add coverage for local and URL videos, file uploads, text combinations, URL filename decoding, and fallback behavior.

@wcqqq1214
wcqqq1214 marked this pull request as ready for review October 6, 2026 16:56

@sourcery-ai sourcery-ai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Hey - I've found 1 issue

Prompt for AI Agents
Please address the comments from this code review:

## Individual Comments

### Comment 1
<location path="astrbot/core/platform/sources/discord/discord_platform_event.py" line_range="393-395" />
<code_context>
+            elif isinstance(i, Video):
+                try:
+                    path = Path(await i.convert_to_file_path())
+                    video_bytes = await asyncio.to_thread(path.read_bytes)
+                    files.append(
+                        discord.File(BytesIO(video_bytes), filename=path.name),
+                    )
+                except Exception:
</code_context>
<issue_to_address>
**Large videos exhaust memory**

When a video is large or several video uploads are processed concurrently, `path.read_bytes()` loads each entire video into memory, and `BytesIO(video_bytes)` retains another buffer through upload. The combined memory use can exhaust the bot process’s memory and interrupt service.

Stream the video file to Discord instead of loading it into `video_bytes` and wrapping it in `BytesIO`.
</issue_to_address>

Sourcery assessment

Needs a human reviewer. 1 finding to address first, and if the path conversion or attachment handling is wrong, Discord could receive an incorrect video or a malformed outbound message that cannot be undone by reverting the code. The impact is bounded to messages sent while the change is deployed, but those already-sent messages would remain.}}burugburuWinvalid_formats maybe score must integer. Good. 极速赛车. Wait.

Blocking findings: astrbot/core/platform/sources/discord/discord_platform_event.py:395


Sourcery is free for open source - if you like our reviews please consider sharing them ✨

Comment thread astrbot/core/platform/sources/discord/discord_platform_event.py Outdated
@wcqqq1214

Copy link
Copy Markdown
Contributor Author

@sourcery-ai review

@sourcery-ai sourcery-ai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Hey - I've found 1 issue

Prompt for AI Agents
Please address the comments from this code review:

## Individual Comments

### Comment 1
<location path="astrbot/core/platform/sources/discord/discord_platform_event.py" line_range="393" />
<code_context>
+            elif isinstance(i, Video):
+                try:
+                    path = Path(await i.convert_to_file_path())
+                    files.append(discord.File(path, filename=path.name))
+                except Exception:
+                    logger.warning(
</code_context>
<issue_to_address>
**Remote videos get generated names**

When a video URL is sent, `Video.convert_to_file_path()` resolves URL videos to a generated temporary path, and `_parse_to_discord` uses that path’s basename as the attachment filename. Discord therefore shows names such as `media_video_<id>.mp4` instead of the source video’s filename.

Choose the attachment filename from the source URL’s path when available, with a suitable fallback for URLs without a filename.
</issue_to_address>

Sourcery assessment

Approval pending. 1 finding to address first.

Blocking findings: astrbot/core/platform/sources/discord/discord_platform_event.py:393


Sourcery is free for open source - if you like our reviews please consider sharing them ✨

Comment thread astrbot/core/platform/sources/discord/discord_platform_event.py Outdated
@wcqqq1214

Copy link
Copy Markdown
Contributor Author

@sourcery-ai review

@sourcery-ai sourcery-ai Bot left a comment

Copy link
Copy Markdown
Contributor

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 assessment

Approved.


Sourcery is free for open source - if you like our reviews please consider sharing them ✨

This branch has not been deployed

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant