fix(builders): preserve empty attachment files (#11588)

This commit is contained in:
Kushida
2026-07-29 17:31:30 +00:00
committed by GitHub
parent 219ad7ba07
commit 4b3f1c166d
3 changed files with 21 additions and 4 deletions
@@ -35,6 +35,25 @@ test('AttachmentBuilder handles 0 as a valid id', () => {
});
});
test('AttachmentBuilder preserves zero-byte string files', () => {
const attachment = new AttachmentBuilder().setId(0).setFilename('empty.txt').setFileData('');
expect(attachment.getRawFile()).toStrictEqual({
data: '',
key: 'files[0]',
name: 'empty.txt',
});
const message = new MessageBuilder().setContent('empty attachment').addAttachments(attachment);
expect(message.toFileBody().files).toStrictEqual([
{
data: '',
key: 'files[0]',
name: 'empty.txt',
},
]);
});
test('MessageBuilder.toFileBody returns JSON body and files', () => {
const msg = new MessageBuilder().setContent('here is a file').addAttachments(
new AttachmentBuilder()
+1 -1
View File
@@ -146,7 +146,7 @@ export class AttachmentBuilder implements JSONEncodable<RESTAPIAttachment> {
* @returns A {@link @discordjs/util#RawFile} object, or `undefined` if no file data is set
*/
public getRawFile(): Partial<RawFile> | undefined {
if (!this.fileData?.data) {
if (this.fileData.data === undefined) {
return;
}
+1 -3
View File
@@ -725,9 +725,7 @@ export class MessageBuilder
const files: RawFile[] = [];
for (const attachment of this.data.attachments) {
const rawFile = attachment.getRawFile();
// Only if data or content type are set, since that implies the intent is to send a new file.
// In case it's contentType but not data, a validation error will be thrown right after.
if (rawFile?.data || rawFile?.contentType) {
if (rawFile !== undefined) {
files.push(rawFile as RawFile);
}
}