Skip to content

fix(MessagePayload): allow AttachmentBuilder in files payload#11423

Open
Qjuh wants to merge 2 commits intodiscordjs:mainfrom
Qjuh:feat/attachment-builder-mainlib
Open

fix(MessagePayload): allow AttachmentBuilder in files payload#11423
Qjuh wants to merge 2 commits intodiscordjs:mainfrom
Qjuh:feat/attachment-builder-mainlib

Conversation

@Qjuh
Copy link
Member

@Qjuh Qjuh commented Feb 16, 2026

Fixes a regression of #11278 which causes issues when passing AttachmentBuilder in MessagePayload#options#files in any send method

@vercel
Copy link

vercel bot commented Feb 16, 2026

The latest updates on your projects. Learn more about Vercel for GitHub.

2 Skipped Deployments
Project Deployment Actions Updated (UTC)
discord-js Skipped Skipped Feb 16, 2026 6:45pm
discord-js-guide Skipped Skipped Feb 16, 2026 6:45pm

Request Review

@vercel vercel bot temporarily deployed to Preview – discord-js-guide February 16, 2026 17:31 Inactive
@vercel vercel bot temporarily deployed to Preview – discord-js February 16, 2026 17:31 Inactive
@coderabbitai
Copy link

coderabbitai bot commented Feb 16, 2026

📝 Walkthrough

Walkthrough

This pull request introduces support for raw-encodable files in Discord.js by adding a new RawFileEncodable type interface and accompanying type guard. The changes update file handling logic across the codebase to detect and process files implementing this interface, replacing references to FileBodyEncodable<APIAttachment> with the new type.

Changes

Cohort / File(s) Summary
Core Utilities
packages/util/src/encodables.ts
Added new RawFileEncodable interface extending JSONEncodable<RESTAPIAttachment> with getRawFile() method, and introduced isRawFileEncodable() type guard function for runtime detection.
Type Definitions
packages/discord.js/typings/index.d.ts, packages/discord.js/src/structures/interfaces/TextBasedChannel.js
Updated BaseMessageOptions.files property type signature from FileBodyEncodable<APIAttachment> to RawFileEncodable across TypeScript declarations and JSDoc typedefs.
File Resolution Logic
packages/discord.js/src/structures/MessagePayload.js
Added import of isRawFileEncodable and modified attachment assembly in resolveBody() to check for raw-encodable files and serialize via toJSON(), while resolveFile() returns getRawFile() immediately for raw-encodable objects.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

🚥 Pre-merge checks | ✅ 4
✅ Passed checks (4 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly describes the main change: fixing support for AttachmentBuilder in the files payload of MessagePayload, which aligns with the changeset's primary objective.
Description check ✅ Passed The description is directly related to the changeset, explaining that it fixes a regression where AttachmentBuilder cannot be passed in MessagePayload#options#files.
Docstring Coverage ✅ Passed Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%.
Merge Conflict Detection ✅ Passed ✅ No merge conflicts detected when merging into main

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing touches
  • 📝 Generate docstrings
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment

Tip

Issue Planner is now in beta. Read the docs and try it out! Share your feedback on Discord.


Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 2

🤖 Fix all issues with AI agents
In `@packages/discord.js/src/structures/MessagePayload.js`:
- Around line 193-206: The attachments mapping assigns id before spreading
file.toJSON(), which allows any id returned by AttachmentBuilder.toJSON() to
overwrite the index-based id; update the map callback used to build attachments
(the callback inside this.options.files?.map and the branches using
isRawFileEncodable(file)) so that the spread of file.toJSON() happens first and
then set id: index.toString() after the spread in both the raw and non-raw
branches, ensuring the generated index id cannot be overwritten by
file.toJSON().

In `@packages/util/src/encodables.ts`:
- Around line 64-83: Update the JSDoc in the RawFileEncodable block to fix the
typo “mutipart” → “multipart”, and harden the isRawFileEncodable type guard so
it not only checks isJSONEncodable(maybeEncodable) and the presence of
'getRawFile' but also verifies that (maybeEncodable as any).getRawFile is a
function before asserting the RawFileEncodable type; target the RawFileEncodable
interface, the isRawFileEncodable function and the getRawFile member when making
these changes.
📜 Review details

Configuration used: Organization UI

Review profile: ASSERTIVE

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between ccce987 and 1744446.

📒 Files selected for processing (4)
  • packages/discord.js/src/structures/MessagePayload.js
  • packages/discord.js/src/structures/interfaces/TextBasedChannel.js
  • packages/discord.js/typings/index.d.ts
  • packages/util/src/encodables.ts
🧰 Additional context used
🧬 Code graph analysis (3)
packages/util/src/encodables.ts (1)
packages/util/src/RawFile.ts (1)
  • RawFile (6-34)
packages/discord.js/src/structures/MessagePayload.js (1)
packages/util/src/encodables.ts (1)
  • isRawFileEncodable (81-83)
packages/discord.js/typings/index.d.ts (1)
packages/util/src/encodables.ts (1)
  • RawFileEncodable (69-74)
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (1)
  • GitHub Check: Tests
🔇 Additional comments (5)
packages/discord.js/typings/index.d.ts (2)

8-8: LGTM — typing import aligns with new raw-file support.


6724-6724: LGTM — BaseMessageOptions.files now accepts RawFileEncodable.

packages/discord.js/src/structures/interfaces/TextBasedChannel.js (1)

91-92: LGTM — JSDoc reflects RawFileEncodable support.

packages/discord.js/src/structures/MessagePayload.js (2)

4-4: LGTM!

Clean addition of isRawFileEncodable alongside the existing isJSONEncodable import.


286-287: LGTM!

Good placement of the isRawFileEncodable branch — it correctly sits between the primitive/stream check and the generic attachment-object fallback, and the early return avoids unnecessary resolution.

@codecov
Copy link

codecov bot commented Feb 16, 2026

Codecov Report

❌ Patch coverage is 0% with 2 lines in your changes missing coverage. Please review.
✅ Project coverage is 31.66%. Comparing base (c460a92) to head (021a70c).
⚠️ Report is 3 commits behind head on main.

Files with missing lines Patch % Lines
packages/util/src/encodables.ts 0.00% 2 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main   #11423      +/-   ##
==========================================
+ Coverage   31.56%   31.66%   +0.09%     
==========================================
  Files         386      386              
  Lines       13944    13967      +23     
  Branches     1098     1099       +1     
==========================================
+ Hits         4401     4422      +21     
- Misses       9409     9411       +2     
  Partials      134      134              
Flag Coverage Δ
builders 68.02% <ø> (+0.44%) ⬆️
core 0.00% <ø> (ø)
proxy 51.06% <ø> (ø)
rest 82.28% <ø> (ø)
util 34.54% <0.00%> (-1.31%) ⬇️
ws 32.21% <ø> (ø)

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.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@vercel vercel bot temporarily deployed to Preview – discord-js February 16, 2026 18:45 Inactive
@vercel vercel bot temporarily deployed to Preview – discord-js-guide February 16, 2026 18:45 Inactive
Copy link
Member

@almeidx almeidx left a comment

Choose a reason for hiding this comment

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

Another regression caused by the mentioned pull request, since you're touching this area of MessagePayload
It is not currently possible to edit a message's attachments (e.g., removing all the attachments), as it causes:

/home/cyph/discord.js/packages/discord.js/src/structures/MessagePayload.js:210
      attachments.push(
                  ^

TypeError: Cannot read properties of undefined (reading 'push')
    at MessagePayload.resolveBody (/home/cyph/discord.js/packages/discord.js/src/structures/MessagePayload.js:210:19)
    at GuildMessageManager.edit (/home/cyph/discord.js/packages/discord.js/src/managers/MessageManager.js:243:10)
    at Message.edit (/home/cyph/discord.js/packages/discord.js/src/structures/Message.js:862:34)
    at Client.<anonymous> (/home/cyph/discord.js/packages/discord.js/test/polls.js:77:11)
    at process.processTicksAndRejections (node:internal/process/task_queues:104:5)

Node.js v25.7.0

);

// Only passable during edits
if (Array.isArray(this.options.attachments)) {
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
if (Array.isArray(this.options.attachments)) {
if (Array.isArray(this.options.attachments)) {
attachments ??= [];

waveform: file.waveform,
duration_secs: file.duration,
}));
const attachments = this.options.files?.map((file, index) =>
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
const attachments = this.options.files?.map((file, index) =>
let attachments = this.options.files?.map((file, index) =>

@github-project-automation github-project-automation bot moved this from Todo to Review in Progress in discord.js Feb 27, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

Status: Review in Progress

Development

Successfully merging this pull request may close these issues.

2 participants