@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.js CHANGED
@@ -111,6 +111,7 @@ __export(index_exports, {
111
111
  embedPredicate: () => embedPredicate,
112
112
  emojiPredicate: () => emojiPredicate,
113
113
  enableValidators: () => enableValidators,
114
+ fileBodyMessagePredicate: () => fileBodyMessagePredicate,
114
115
  filePredicate: () => filePredicate,
115
116
  fileUploadPredicate: () => fileUploadPredicate,
116
117
  idPredicate: () => idPredicate,
@@ -131,6 +132,7 @@ __export(index_exports, {
131
132
  pollAnswerPredicate: () => pollAnswerPredicate,
132
133
  pollPredicate: () => pollPredicate,
133
134
  pollQuestionPredicate: () => pollQuestionPredicate,
135
+ rawFilePredicate: () => rawFilePredicate,
134
136
  resolveAccessoryComponent: () => resolveAccessoryComponent,
135
137
  resolveBuilder: () => resolveBuilder,
136
138
  sectionPredicate: () => sectionPredicate,
@@ -4773,9 +4775,18 @@ var PollBuilder = class {
4773
4775
  };
4774
4776
 
4775
4777
  // src/messages/Assertions.ts
4778
+ var import_node_buffer = require("buffer");
4776
4779
  var import_v1045 = require("discord-api-types/v10");
4777
4780
  var import_zod13 = require("zod");
4781
+ var fileKeyRegex = /^files\[(?<placeholder>\d+?)]$/;
4782
+ var rawFilePredicate = import_zod13.z.object({
4783
+ data: import_zod13.z.union([import_zod13.z.instanceof(import_node_buffer.Buffer), import_zod13.z.instanceof(Uint8Array), import_zod13.z.string()]),
4784
+ name: import_zod13.z.string().min(1),
4785
+ contentType: import_zod13.z.string().optional(),
4786
+ key: import_zod13.z.string().regex(fileKeyRegex).optional()
4787
+ });
4778
4788
  var attachmentPredicate = import_zod13.z.object({
4789
+ // As a string it only makes sense for edits when we do have an attachment snowflake
4779
4790
  id: import_zod13.z.union([import_zod13.z.string(), import_zod13.z.number()]),
4780
4791
  description: import_zod13.z.string().max(1024).optional(),
4781
4792
  duration_secs: import_zod13.z.number().max(2 ** 31 - 1).optional(),
@@ -4862,6 +4873,11 @@ var messageComponentsV2Predicate = baseMessagePredicate.extend({
4862
4873
  poll: import_zod13.z.null().optional()
4863
4874
  });
4864
4875
  var messagePredicate = import_zod13.z.union([messageNoComponentsV2Predicate, messageComponentsV2Predicate]);
4876
+ var fileBodyMessagePredicate = import_zod13.z.object({
4877
+ body: messagePredicate,
4878
+ // No min length to support message edits
4879
+ files: rawFilePredicate.array().max(10)
4880
+ });
4865
4881
 
4866
4882
  // src/messages/AllowedMentions.ts
4867
4883
  var AllowedMentionsBuilder = class {
@@ -5046,6 +5062,16 @@ var AttachmentBuilder = class {
5046
5062
  * The API data associated with this attachment.
5047
5063
  */
5048
5064
  data;
5065
+ /**
5066
+ * This data is not included in the output of `toJSON()`. For this class specifically, this refers to binary data
5067
+ * that will wind up being included in the multipart/form-data request, if used with the `MessageBuilder`.
5068
+ * To retrieve this data, use {@link getRawFile}.
5069
+ *
5070
+ * @remarks This cannot be set via the constructor, primarily because of the behavior described
5071
+ * {@link https://discord.com/developers/docs/reference#editing-message-attachments | here}.
5072
+ * That is, when editing a message's attachments, you should only be providing file data for new attachments.
5073
+ */
5074
+ fileData;
5049
5075
  /**
5050
5076
  * Creates a new attachment builder.
5051
5077
  *
@@ -5053,6 +5079,7 @@ var AttachmentBuilder = class {
5053
5079
  */
5054
5080
  constructor(data = {}) {
5055
5081
  this.data = structuredClone(data);
5082
+ this.fileData = {};
5056
5083
  }
5057
5084
  /**
5058
5085
  * Sets the id of the attachment.
@@ -5111,6 +5138,54 @@ var AttachmentBuilder = class {
5111
5138
  this.data.filename = void 0;
5112
5139
  return this;
5113
5140
  }
5141
+ /**
5142
+ * Sets the file data to upload with this attachment.
5143
+ *
5144
+ * @param data - The file data
5145
+ * @remarks Note that this data is NOT included in the {@link toJSON} output. To retrieve it, use {@link getRawFile}.
5146
+ */
5147
+ setFileData(data) {
5148
+ this.fileData.data = data;
5149
+ return this;
5150
+ }
5151
+ /**
5152
+ * Clears the file data from this attachment.
5153
+ */
5154
+ clearFileData() {
5155
+ this.fileData.data = void 0;
5156
+ return this;
5157
+ }
5158
+ /**
5159
+ * Sets the content type of the file data to upload with this attachment.
5160
+ *
5161
+ * @remarks Note that this data is NOT included in the {@link toJSON} output. To retrieve it, use {@link getRawFile}.
5162
+ */
5163
+ setFileContentType(contentType) {
5164
+ this.fileData.contentType = contentType;
5165
+ return this;
5166
+ }
5167
+ /**
5168
+ * Clears the content type of the file data from this attachment.
5169
+ */
5170
+ clearFileContentType() {
5171
+ this.fileData.contentType = void 0;
5172
+ return this;
5173
+ }
5174
+ /**
5175
+ * Converts this attachment to a {@link RawFile} for uploading.
5176
+ *
5177
+ * @returns A {@link RawFile} object, or `undefined` if no file data is set
5178
+ */
5179
+ getRawFile() {
5180
+ if (!this.fileData?.data) {
5181
+ return;
5182
+ }
5183
+ return {
5184
+ ...this.fileData,
5185
+ name: this.data.filename,
5186
+ key: this.data.id ? `files[${this.data.id}]` : void 0
5187
+ };
5188
+ }
5114
5189
  /**
5115
5190
  * Sets the title of this attachment.
5116
5191
  *
@@ -5748,10 +5823,31 @@ var MessageBuilder = class {
5748
5823
  validate(messagePredicate, data, validationOverride);
5749
5824
  return data;
5750
5825
  }
5826
+ /**
5827
+ * Serializes this builder to both JSON body and file data for multipart/form-data requests.
5828
+ *
5829
+ * @param validationOverride - Force validation to run/not run regardless of your global preference
5830
+ * @remarks
5831
+ * This method extracts file data from attachments that have files set via {@link AttachmentBuilder.setFileData}.
5832
+ * The returned body includes attachment metadata, while files contains the binary data for upload.
5833
+ */
5834
+ toFileBody(validationOverride) {
5835
+ const body = this.toJSON(false);
5836
+ const files = [];
5837
+ for (const attachment of this.data.attachments) {
5838
+ const rawFile = attachment.getRawFile();
5839
+ if (rawFile?.data || rawFile?.contentType) {
5840
+ files.push(rawFile);
5841
+ }
5842
+ }
5843
+ const combined = { body, files };
5844
+ validate(fileBodyMessagePredicate, combined, validationOverride);
5845
+ return combined;
5846
+ }
5751
5847
  };
5752
5848
 
5753
5849
  // src/index.ts
5754
- var version = "2.0.0-dev.1763510521-315f42278";
5850
+ var version = "2.0.0-dev.1763683321-0b1226337";
5755
5851
  // Annotate the CommonJS export names for ESM import in node:
5756
5852
  0 && (module.exports = {
5757
5853
  ActionRowBuilder,
@@ -5844,6 +5940,7 @@ var version = "2.0.0-dev.1763510521-315f42278";
5844
5940
  embedPredicate,
5845
5941
  emojiPredicate,
5846
5942
  enableValidators,
5943
+ fileBodyMessagePredicate,
5847
5944
  filePredicate,
5848
5945
  fileUploadPredicate,
5849
5946
  idPredicate,
@@ -5864,6 +5961,7 @@ var version = "2.0.0-dev.1763510521-315f42278";
5864
5961
  pollAnswerPredicate,
5865
5962
  pollPredicate,
5866
5963
  pollQuestionPredicate,
5964
+ rawFilePredicate,
5867
5965
  resolveAccessoryComponent,
5868
5966
  resolveBuilder,
5869
5967
  sectionPredicate,