fix: upload loopback image URLs as Discord attachments - #9762
Open
wcqqq1214 wants to merge 1 commit into
Open
Conversation
) Discord cannot reach loopback hosts, so http://127.0.0.1:<port>/... URLs from text_to_image() embed as a blank bar. Route them through MediaResolver to upload as discord.File attachments instead.
wcqqq1214
marked this pull request as ready for review
August 21, 2026 12:48
Contributor
There was a problem hiding this comment.
Hey - I've left some high level feedback:
- Consider extracting the loopback/localhost detection logic into a small helper function (e.g.,
is_loopback_host(url: str) -> bool) to keep_parse_to_discordeasier to read and to centralize any future changes to this behavior. - When checking for
localhostand parsing IPs, it may be safer to normalize the hostname (e.g.,lower()), and explicitly handle IPv6 literals with zone indices (like::1%lo0) so they are consistently treated as unreachable loopback hosts rather than falling through to the embed path.
Prompt for AI Agents
Please address the comments from this code review:
## Overall Comments
- Consider extracting the loopback/localhost detection logic into a small helper function (e.g., `is_loopback_host(url: str) -> bool`) to keep `_parse_to_discord` easier to read and to centralize any future changes to this behavior.
- When checking for `localhost` and parsing IPs, it may be safer to normalize the hostname (e.g., `lower()`), and explicitly handle IPv6 literals with zone indices (like `::1%lo0`) so they are consistently treated as unreachable loopback hosts rather than falling through to the embed path.Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #9756.
text_to_image()defaults to loopback URLs such ashttp://127.0.0.1:<port>/.... Discord's_parse_to_discordtreated anyhttp*image as a public URL and put it indiscord.Embed.set_image(). Discord's servers cannot fetch loopback hosts, so the image rendered as a blank bar.Loopback hosts (
127.0.0.0/8,localhost,::1, IPv4-mapped::ffff:127.0.0.1) now fall through to the existingMediaResolverpath and are uploaded asdiscord.Fileattachments. Public HTTP(S) URLs are unchanged.Modifications / 改动点
astrbot/core/platform/sources/discord/discord_platform_event.py: parse the image URL host and skip the Embed path whenipaddress.ip_address(...).is_loopback(orlocalhost) is true.This is NOT a breaking change. / 这不是一个破坏性变更。
Screenshots or Test Results / 运行截图或测试结果
Before: Discord rendered the local T2I URL as a blank white bar (embed whose image Discord cannot fetch).
After: Discord now receives the image as an attachment instead of a blank embed. The sparse layout is from the local PIL renderer used only for this repro, not from the adapter change.
Verified locally against Discord (
Astrbot#1126) with local T2I + file service (callback_api_base=http://127.0.0.1:6185). Existingtests/test_discord_adapter.pystill pass.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 have ensured that no new dependencies are introduced, OR if new dependencies are introduced, they have been added to the appropriate locations in
requirements.txtandpyproject.toml./ 我确保没有引入新依赖库,或者引入了新依赖库的同时将其添加到
requirements.txt和pyproject.toml文件相应位置。😮 My changes do not introduce malicious code.
/ 我的更改没有引入恶意代码。
Summary by Sourcery
Fix Discord image delivery for loopback URLs by routing them through attachment handling while retaining embeds for public URLs.
Bug Fixes:
Enhancements:
Summary by Sourcery
Ensure Discord delivers locally hosted image URLs as file attachments while preserving embeds for public image URLs.
Bug Fixes:
Enhancements: