Repository navigation
fix(discord): declare fields on Discord message components - #10710
Open
rudycelekli wants to merge 2 commits into
Open
rudycelekli wants to merge 2 commits into
rudycelekli wants to merge 2 commits into
Conversation
Co-authored-by: Cursor <cursoragent@cursor.com>
Contributor
There was a problem hiding this comment.
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/components.py" line_range="116" />
<code_context>
"""Discord视图组件,包含按钮和选择菜单"""
type: str = "discord_view"
+ components: list = []
+ timeout: float | None = None
</code_context>
<issue_to_address>
**Invalid view items are silently ignored**
When a caller supplies a list containing values that are not message components, pydantic validates only that `components` is a list, so non-component entries are accepted; `to_discord_view()` ignores every entry that is not a `DiscordButton`, and callers can get a view missing the items they supplied.
Declare the field as `list[BaseMessageComponent]` so model validation matches the constructor contract.
</issue_to_address>Sourcery assessment
Approval pending. 1 finding to address first.
Blocking findings: astrbot/core/platform/sources/discord/components.py:116
5 tasks done
This branch has not been deployed
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.
Addresses the component construction part of #7965.
The Discord-specific message components (
DiscordEmbed,DiscordButton,DiscordReference,DiscordView, and the legacyDiscordViewComponent) extendBaseMessageComponent, which is a pydantic model. Their__init__assigned attributes such asself.titlewithout declaring them as fields and without callingsuper().__init__(), so every construction failed:As a result, plugins cannot send embeds, buttons or views through
MessageChainat all, even thoughDiscordPlatformEventalready knows how to render them.Modifications / 改动点
astrbot/core/platform/sources/discord/components.py: declare the attributes as model fields and pass them tosuper().__init__(). The constructor signatures and theto_discord_embed()/to_discord_view()logic are unchanged.astrbot/core/platform/sources/discord/discord_platform_event.py: same fix forDiscordViewComponent(viewis declared asAnybecausediscord.ui.Viewis not a pydantic type).tests/unit/test_discord_components.py: construct each component, put an embed in aMessageChainand convert it, and build a view with a normal button and a link button.The other points in the issue (duck-typed component detection and the respond-stage empty-chain check for embed-only chains) are separate behavior changes and are not part of this PR.
Screenshots or Test Results / 运行截图或测试结果
ruff format/ruff checkpass.Checklist / 检查清单
Summary by Sourcery
Restore functional Discord message component construction and validate its rendering through message chains and views.
Bug Fixes:
Enhancements:
Tests:
Review follow-up (2026-10-26)
Changed
componentstolist[BaseMessageComponent], matching the constructor contract. Added invalid-item controls (integer, string, None) and a serialization control ensuring concrete button fields survive the typed list. Uses the project's Pydantic v1 model semantics. Before: 3 failed / 4 passed; after: all 7 tests intests/unit/test_discord_components.pypass. Whole-repository Ruff lint/format checks pass. Local Python 3.13.5 validation; hosted Python 3.12 CI remains separate. No Discord network calls.