Skip to content

fix: upload loopback image URLs as Discord attachments - #9762

Open
wcqqq1214 wants to merge 1 commit into
AstrBotDevs:masterfrom
wcqqq1214:fix/9756-discord-local-image-url
Open

fix: upload loopback image URLs as Discord attachments#9762
wcqqq1214 wants to merge 1 commit into
AstrBotDevs:masterfrom
wcqqq1214:fix/9756-discord-local-image-url

Conversation

@wcqqq1214

@wcqqq1214 wcqqq1214 commented Aug 21, 2026

Copy link
Copy Markdown
Contributor

Fixes #9756.

text_to_image() defaults to loopback URLs such as http://127.0.0.1:<port>/.... Discord's _parse_to_discord treated any http* image as a public URL and put it in discord.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 existing MediaResolver path and are uploaded as discord.File attachments. 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 when ipaddress.ip_address(...).is_loopback (or localhost) 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.

Screenshot 2026-08-21 at 20 38 09

Verified locally against Discord (Astrbot#1126) with local T2I + file service (callback_api_base=http://127.0.0.1:6185). Existing tests/test_discord_adapter.py still 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.txt and pyproject.toml.
    / 我确保没有引入新依赖库,或者引入了新依赖库的同时将其添加到 requirements.txtpyproject.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:

  • Upload loopback image URLs as Discord file attachments so locally hosted images render correctly instead of appearing as blank embeds.

Enhancements:

  • Preserve embedded-image handling for publicly reachable HTTP(S) URLs while recognizing localhost, loopback IPs, and IPv4-mapped loopback addresses.

Summary by Sourcery

Ensure Discord delivers locally hosted image URLs as file attachments while preserving embeds for public image URLs.

Bug Fixes:

  • Upload images hosted on loopback or localhost URLs as Discord attachments so locally generated images render correctly instead of appearing as blank embeds.

Enhancements:

  • Continue embedding publicly reachable HTTP(S) image URLs while recognizing localhost, loopback IPs, and IPv4-mapped loopback addresses.

)

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
wcqqq1214 marked this pull request as ready for review August 21, 2026 12:48
@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 Aug 21, 2026

@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 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_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.
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.

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.

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]

1 participant