@discordjs/builders 2.0.0-dev.1763510521-315f42278 → 2.0.0-dev.1763683321-0b1226337

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.mjs CHANGED
@@ -4645,9 +4645,18 @@ var PollBuilder = class {
4645
4645
  };
4646
4646
 
4647
4647
  // src/messages/Assertions.ts
4648
+ import { Buffer as Buffer2 } from "buffer";
4648
4649
  import { AllowedMentionsTypes, ComponentType as ComponentType27, MessageFlags, MessageReferenceType } from "discord-api-types/v10";
4649
4650
  import { z as z13 } from "zod";
4651
+ var fileKeyRegex = /^files\[(?<placeholder>\d+?)]$/;
4652
+ var rawFilePredicate = z13.object({
4653
+ data: z13.union([z13.instanceof(Buffer2), z13.instanceof(Uint8Array), z13.string()]),
4654
+ name: z13.string().min(1),
4655
+ contentType: z13.string().optional(),
4656
+ key: z13.string().regex(fileKeyRegex).optional()
4657
+ });
4650
4658
  var attachmentPredicate = z13.object({
4659
+ // As a string it only makes sense for edits when we do have an attachment snowflake
4651
4660
  id: z13.union([z13.string(), z13.number()]),
4652
4661
  description: z13.string().max(1024).optional(),
4653
4662
  duration_secs: z13.number().max(2 ** 31 - 1).optional(),
@@ -4734,6 +4743,11 @@ var messageComponentsV2Predicate = baseMessagePredicate.extend({
4734
4743
  poll: z13.null().optional()
4735
4744
  });
4736
4745
  var messagePredicate = z13.union([messageNoComponentsV2Predicate, messageComponentsV2Predicate]);
4746
+ var fileBodyMessagePredicate = z13.object({
4747
+ body: messagePredicate,
4748
+ // No min length to support message edits
4749
+ files: rawFilePredicate.array().max(10)
4750
+ });
4737
4751
 
4738
4752
  // src/messages/AllowedMentions.ts
4739
4753
  var AllowedMentionsBuilder = class {
@@ -4918,6 +4932,16 @@ var AttachmentBuilder = class {
4918
4932
  * The API data associated with this attachment.
4919
4933
  */
4920
4934
  data;
4935
+ /**
4936
+ * This data is not included in the output of `toJSON()`. For this class specifically, this refers to binary data
4937
+ * that will wind up being included in the multipart/form-data request, if used with the `MessageBuilder`.
4938
+ * To retrieve this data, use {@link getRawFile}.
4939
+ *
4940
+ * @remarks This cannot be set via the constructor, primarily because of the behavior described
4941
+ * {@link https://discord.com/developers/docs/reference#editing-message-attachments | here}.
4942
+ * That is, when editing a message's attachments, you should only be providing file data for new attachments.
4943
+ */
4944
+ fileData;
4921
4945
  /**
4922
4946
  * Creates a new attachment builder.
4923
4947
  *
@@ -4925,6 +4949,7 @@ var AttachmentBuilder = class {
4925
4949
  */
4926
4950
  constructor(data = {}) {
4927
4951
  this.data = structuredClone(data);
4952
+ this.fileData = {};
4928
4953
  }
4929
4954
  /**
4930
4955
  * Sets the id of the attachment.
@@ -4983,6 +5008,54 @@ var AttachmentBuilder = class {
4983
5008
  this.data.filename = void 0;
4984
5009
  return this;
4985
5010
  }
5011
+ /**
5012
+ * Sets the file data to upload with this attachment.
5013
+ *
5014
+ * @param data - The file data
5015
+ * @remarks Note that this data is NOT included in the {@link toJSON} output. To retrieve it, use {@link getRawFile}.
5016
+ */
5017
+ setFileData(data) {
5018
+ this.fileData.data = data;
5019
+ return this;
5020
+ }
5021
+ /**
5022
+ * Clears the file data from this attachment.
5023
+ */
5024
+ clearFileData() {
5025
+ this.fileData.data = void 0;
5026
+ return this;
5027
+ }
5028
+ /**
5029
+ * Sets the content type of the file data to upload with this attachment.
5030
+ *
5031
+ * @remarks Note that this data is NOT included in the {@link toJSON} output. To retrieve it, use {@link getRawFile}.
5032
+ */
5033
+ setFileContentType(contentType) {
5034
+ this.fileData.contentType = contentType;
5035
+ return this;
5036
+ }
5037
+ /**
5038
+ * Clears the content type of the file data from this attachment.
5039
+ */
5040
+ clearFileContentType() {
5041
+ this.fileData.contentType = void 0;
5042
+ return this;
5043
+ }
5044
+ /**
5045
+ * Converts this attachment to a {@link RawFile} for uploading.
5046
+ *
5047
+ * @returns A {@link RawFile} object, or `undefined` if no file data is set
5048
+ */
5049
+ getRawFile() {
5050
+ if (!this.fileData?.data) {
5051
+ return;
5052
+ }
5053
+ return {
5054
+ ...this.fileData,
5055
+ name: this.data.filename,
5056
+ key: this.data.id ? `files[${this.data.id}]` : void 0
5057
+ };
5058
+ }
4986
5059
  /**
4987
5060
  * Sets the title of this attachment.
4988
5061
  *
@@ -5620,10 +5693,31 @@ var MessageBuilder = class {
5620
5693
  validate(messagePredicate, data, validationOverride);
5621
5694
  return data;
5622
5695
  }
5696
+ /**
5697
+ * Serializes this builder to both JSON body and file data for multipart/form-data requests.
5698
+ *
5699
+ * @param validationOverride - Force validation to run/not run regardless of your global preference
5700
+ * @remarks
5701
+ * This method extracts file data from attachments that have files set via {@link AttachmentBuilder.setFileData}.
5702
+ * The returned body includes attachment metadata, while files contains the binary data for upload.
5703
+ */
5704
+ toFileBody(validationOverride) {
5705
+ const body = this.toJSON(false);
5706
+ const files = [];
5707
+ for (const attachment of this.data.attachments) {
5708
+ const rawFile = attachment.getRawFile();
5709
+ if (rawFile?.data || rawFile?.contentType) {
5710
+ files.push(rawFile);
5711
+ }
5712
+ }
5713
+ const combined = { body, files };
5714
+ validate(fileBodyMessagePredicate, combined, validationOverride);
5715
+ return combined;
5716
+ }
5623
5717
  };
5624
5718
 
5625
5719
  // src/index.ts
5626
- var version = "2.0.0-dev.1763510521-315f42278";
5720
+ var version = "2.0.0-dev.1763683321-0b1226337";
5627
5721
  export {
5628
5722
  ActionRowBuilder,
5629
5723
  AllowedMentionsBuilder,
@@ -5715,6 +5809,7 @@ export {
5715
5809
  embedPredicate,
5716
5810
  emojiPredicate,
5717
5811
  enableValidators,
5812
+ fileBodyMessagePredicate,
5718
5813
  filePredicate,
5719
5814
  fileUploadPredicate,
5720
5815
  idPredicate,
@@ -5735,6 +5830,7 @@ export {
5735
5830
  pollAnswerPredicate,
5736
5831
  pollPredicate,
5737
5832
  pollQuestionPredicate,
5833
+ rawFilePredicate,
5738
5834
  resolveAccessoryComponent,
5739
5835
  resolveBuilder,
5740
5836
  sectionPredicate,