Skip to content

fix: make ComponentEmoji id and name optional#91

Merged
shoucandanghehe merged 3 commits into
nonebot:masterfrom
RainyN0077:fix/component-emoji-optional-id
May 18, 2026
Merged

fix: make ComponentEmoji id and name optional#91
shoucandanghehe merged 3 commits into
nonebot:masterfrom
RainyN0077:fix/component-emoji-optional-id

Conversation

@RainyN0077
Copy link
Copy Markdown
Contributor

@RainyN0077 RainyN0077 commented May 18, 2026

Discord only includes the id field for custom emojis, not for standard unicode emojis (e.g. 🔗, 🟢, 🗑️). The current code defines id as required via Field(...) which causes pydantic ValidationError when receiving MESSAGE_UPDATE events containing button components with unicode emojis.

Root Cause

In nonebot/adapters/discord/api/model.py, ComponentEmoji is defined as:

class ComponentEmoji(BaseModel):
    id: str | None = Field(...)      # ← REQUIRED — but Discord omits it for unicode emojis
    name: str | None = Field(...)
    animated: Missing[bool] = UNSET

Per Discord API docs, the emoji in a button is a partial emoji where both id and name are optional.

Fix

Change id and name from required to optional while preserving Discord field semantics:

class ComponentEmoji(BaseModel):
    id: MissingOrNullable[Snowflake] = UNSET
    name: MissingOrNullable[str] = UNSET
    animated: Missing[bool] = UNSET

This follows Discord's nullable/optional field rules and models id as a Snowflake.

Tests

Added regression coverage for:

  • unicode component emoji payloads such as {"name": "🔗"} where id is omitted
  • custom component emoji payloads where id is nullable and parsed as Snowflake

Error before fix

pydantic_core._pydantic_core.ValidationError: 42 validation errors for union[GuildMessageUpdateEvent,DirectMessageUpdateEvent]
GuildMessageUpdateEvent.components.list[...].ActionRow.components.0.Button.emoji.ComponentEmoji.id
  Field required [type=missing, input_value={'name': '🔗'}, input_type=dict]

Closes #90

Discord only includes the `id` field for custom emojis, not for standard
unicode emojis (e.g. 🔗).  Defining `id` as required via `Field(...)` causes
pydantic ValidationError when receiving MESSAGE_UPDATE events containing
button components with unicode emojis.

Change both `id` and `name` to use `Missing[...] = UNSET` to match the
Discord API specification where both fields are optional in a partial emoji.

Fixes nonebot#90
Discord only includes the id field for custom emojis, not for standard
unicode emojis (e.g. 🔗).  Defining id as required via Field(...) causes
pydantic ValidationError when receiving MESSAGE_UPDATE events containing
button components with unicode emojis.

Change both id and
ame to use Missing[...] = UNSET to match the
Discord API specification where both fields are optional in a partial emoji.

Fixes nonebot#90
@codecov
Copy link
Copy Markdown

codecov Bot commented May 18, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 75.79%. Comparing base (66ea530) to head (1887900).

Additional details and impacted files
@@            Coverage Diff             @@
##           master      #91      +/-   ##
==========================================
+ Coverage   75.76%   75.79%   +0.03%     
==========================================
  Files          40       40              
  Lines        7286     7297      +11     
  Branches      380      380              
==========================================
+ Hits         5520     5531      +11     
  Misses       1684     1684              
  Partials       82       82              
Flag Coverage Δ
unittests 75.79% <100.00%> (+0.03%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@shoucandanghehe
Copy link
Copy Markdown
Member

lgtm, thx

@shoucandanghehe shoucandanghehe merged commit 2be0176 into nonebot:master May 18, 2026
19 checks passed
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.

ComponentEmoji.id should be optional — unicode emojis lack id field

2 participants