mirror of
https://github.com/discordjs/discord.js.git
synced 2026-09-17 08:47:27 +00:00
fix(builders): preserve empty attachment files (#11588)
This commit is contained in:
@@ -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()
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user