@ovencord/builders 1.11.1

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.
Files changed (84) hide show
  1. package/LICENSE +191 -0
  2. package/README.md +92 -0
  3. package/package.json +70 -0
  4. package/src/Assertions.ts +15 -0
  5. package/src/components/ActionRow.ts +346 -0
  6. package/src/components/Assertions.ts +190 -0
  7. package/src/components/Component.ts +47 -0
  8. package/src/components/Components.ts +275 -0
  9. package/src/components/button/Button.ts +34 -0
  10. package/src/components/button/CustomIdButton.ts +74 -0
  11. package/src/components/button/LinkButton.ts +39 -0
  12. package/src/components/button/PremiumButton.ts +26 -0
  13. package/src/components/button/mixins/EmojiOrLabelButtonMixin.ts +52 -0
  14. package/src/components/fileUpload/Assertions.ts +12 -0
  15. package/src/components/fileUpload/FileUpload.ts +109 -0
  16. package/src/components/label/Assertions.ts +28 -0
  17. package/src/components/label/Label.ts +215 -0
  18. package/src/components/selectMenu/BaseSelectMenu.ts +89 -0
  19. package/src/components/selectMenu/ChannelSelectMenu.ts +115 -0
  20. package/src/components/selectMenu/MentionableSelectMenu.ts +126 -0
  21. package/src/components/selectMenu/RoleSelectMenu.ts +89 -0
  22. package/src/components/selectMenu/StringSelectMenu.ts +165 -0
  23. package/src/components/selectMenu/StringSelectMenuOption.ts +113 -0
  24. package/src/components/selectMenu/UserSelectMenu.ts +89 -0
  25. package/src/components/textInput/Assertions.ts +15 -0
  26. package/src/components/textInput/TextInput.ts +154 -0
  27. package/src/components/v2/Assertions.ts +82 -0
  28. package/src/components/v2/Container.ts +254 -0
  29. package/src/components/v2/File.ts +81 -0
  30. package/src/components/v2/MediaGallery.ts +128 -0
  31. package/src/components/v2/MediaGalleryItem.ts +85 -0
  32. package/src/components/v2/Section.ts +266 -0
  33. package/src/components/v2/Separator.ts +82 -0
  34. package/src/components/v2/TextDisplay.ts +63 -0
  35. package/src/components/v2/Thumbnail.ts +100 -0
  36. package/src/index.ts +109 -0
  37. package/src/interactions/commands/Command.ts +87 -0
  38. package/src/interactions/commands/SharedName.ts +68 -0
  39. package/src/interactions/commands/SharedNameAndDescription.ts +69 -0
  40. package/src/interactions/commands/chatInput/Assertions.ts +180 -0
  41. package/src/interactions/commands/chatInput/ChatInputCommand.ts +40 -0
  42. package/src/interactions/commands/chatInput/ChatInputCommandSubcommands.ts +117 -0
  43. package/src/interactions/commands/chatInput/mixins/ApplicationCommandNumericOptionMinMaxValueMixin.ts +52 -0
  44. package/src/interactions/commands/chatInput/mixins/ApplicationCommandOptionChannelTypesMixin.ts +57 -0
  45. package/src/interactions/commands/chatInput/mixins/ApplicationCommandOptionWithAutocompleteMixin.ts +32 -0
  46. package/src/interactions/commands/chatInput/mixins/ApplicationCommandOptionWithChoicesMixin.ts +43 -0
  47. package/src/interactions/commands/chatInput/mixins/SharedChatInputCommandOptions.ts +204 -0
  48. package/src/interactions/commands/chatInput/mixins/SharedSubcommands.ts +61 -0
  49. package/src/interactions/commands/chatInput/options/ApplicationCommandOptionBase.ts +66 -0
  50. package/src/interactions/commands/chatInput/options/attachment.ts +20 -0
  51. package/src/interactions/commands/chatInput/options/boolean.ts +20 -0
  52. package/src/interactions/commands/chatInput/options/channel.ts +28 -0
  53. package/src/interactions/commands/chatInput/options/integer.ts +34 -0
  54. package/src/interactions/commands/chatInput/options/mentionable.ts +20 -0
  55. package/src/interactions/commands/chatInput/options/number.ts +34 -0
  56. package/src/interactions/commands/chatInput/options/role.ts +20 -0
  57. package/src/interactions/commands/chatInput/options/string.ts +71 -0
  58. package/src/interactions/commands/chatInput/options/user.ts +20 -0
  59. package/src/interactions/commands/contextMenu/Assertions.ts +31 -0
  60. package/src/interactions/commands/contextMenu/ContextMenuCommand.ts +42 -0
  61. package/src/interactions/commands/contextMenu/MessageCommand.ts +19 -0
  62. package/src/interactions/commands/contextMenu/UserCommand.ts +19 -0
  63. package/src/interactions/modals/Assertions.ts +27 -0
  64. package/src/interactions/modals/Modal.ts +158 -0
  65. package/src/messages/AllowedMentions.ts +193 -0
  66. package/src/messages/Assertions.ts +148 -0
  67. package/src/messages/Attachment.ts +209 -0
  68. package/src/messages/Message.ts +692 -0
  69. package/src/messages/MessageReference.ts +111 -0
  70. package/src/messages/embed/Assertions.ts +53 -0
  71. package/src/messages/embed/Embed.ts +352 -0
  72. package/src/messages/embed/EmbedAuthor.ts +83 -0
  73. package/src/messages/embed/EmbedField.ts +67 -0
  74. package/src/messages/embed/EmbedFooter.ts +65 -0
  75. package/src/messages/poll/Assertions.ts +20 -0
  76. package/src/messages/poll/Poll.ts +243 -0
  77. package/src/messages/poll/PollAnswer.ts +77 -0
  78. package/src/messages/poll/PollAnswerMedia.ts +38 -0
  79. package/src/messages/poll/PollMedia.ts +41 -0
  80. package/src/messages/poll/PollQuestion.ts +20 -0
  81. package/src/util/ValidationError.ts +21 -0
  82. package/src/util/normalizeArray.ts +19 -0
  83. package/src/util/resolveBuilder.ts +40 -0
  84. package/src/util/validation.ts +58 -0
@@ -0,0 +1,209 @@
1
+ import type { Buffer } from 'node:buffer';
2
+ import type { JSONEncodable, RawFile } from '@ovencord/util';
3
+ import type { RESTAPIAttachment, Snowflake } from 'discord-api-types/v10';
4
+ import { validate } from '../util/validation.js';
5
+ import { attachmentPredicate } from './Assertions.js';
6
+
7
+ /**
8
+ * A builder that creates API-compatible JSON data for attachments.
9
+ */
10
+ export class AttachmentBuilder implements JSONEncodable<RESTAPIAttachment> {
11
+ /**
12
+ * The API data associated with this attachment.
13
+ */
14
+ private readonly data: Partial<RESTAPIAttachment>;
15
+
16
+ /**
17
+ * This data is not included in the output of `toJSON()`. For this class specifically, this refers to binary data
18
+ * that will wind up being included in the multipart/form-data request, if used with the `MessageBuilder`.
19
+ * To retrieve this data, use {@link getRawFile}.
20
+ *
21
+ * @remarks This cannot be set via the constructor, primarily because of the behavior described
22
+ * {@link https://discord.com/developers/docs/reference#editing-message-attachments | here}.
23
+ * That is, when editing a message's attachments, you should only be providing file data for new attachments.
24
+ */
25
+ private readonly fileData: Partial<Pick<RawFile, 'contentType' | 'data'>>;
26
+
27
+ /**
28
+ * Creates a new attachment builder.
29
+ *
30
+ * @param data - The API data to create this attachment with
31
+ * @example
32
+ * ```ts
33
+ * const attachment = new AttachmentBuilder().setId(1).setFileData(':)').setFilename('smiley.txt')
34
+ * ```
35
+ * @remarks Please note that the `id` field is required, it's rather easy to miss!
36
+ */
37
+ public constructor(data: Partial<RESTAPIAttachment> = {}) {
38
+ this.data = structuredClone(data);
39
+ this.fileData = {};
40
+ }
41
+
42
+ /**
43
+ * Sets the id of the attachment.
44
+ *
45
+ * @param id - The id of the attachment
46
+ */
47
+ public setId(id: Snowflake | number): this {
48
+ this.data.id = id;
49
+ return this;
50
+ }
51
+
52
+ /**
53
+ * Sets the description of this attachment.
54
+ *
55
+ * @param description - The description of the attachment
56
+ */
57
+ public setDescription(description: string): this {
58
+ this.data.description = description;
59
+ return this;
60
+ }
61
+
62
+ /**
63
+ * Clears the description of this attachment.
64
+ */
65
+ public clearDescription(): this {
66
+ this.data.description = undefined;
67
+ return this;
68
+ }
69
+
70
+ /**
71
+ * Sets the duration of this attachment (audio clips).
72
+ *
73
+ * @param duration - The duration of the attachment in seconds
74
+ */
75
+ public setDuration(duration: number): this {
76
+ this.data.duration_secs = duration;
77
+ return this;
78
+ }
79
+
80
+ /**
81
+ * Clears the duration of this attachment.
82
+ */
83
+ public clearDuration(): this {
84
+ this.data.duration_secs = undefined;
85
+ return this;
86
+ }
87
+
88
+ /**
89
+ * Sets the filename of this attachment.
90
+ *
91
+ * @param filename - The filename of the attachment
92
+ */
93
+ public setFilename(filename: string): this {
94
+ this.data.filename = filename;
95
+ return this;
96
+ }
97
+
98
+ /**
99
+ * Clears the filename of this attachment.
100
+ */
101
+ public clearFilename(): this {
102
+ this.data.filename = undefined;
103
+ return this;
104
+ }
105
+
106
+ /**
107
+ * Sets the file data to upload with this attachment.
108
+ *
109
+ * @param data - The file data
110
+ * @remarks Note that this data is NOT included in the {@link toJSON} output. To retrieve it, use {@link getRawFile}.
111
+ */
112
+ public setFileData(data: Buffer | Uint8Array | string): this {
113
+ this.fileData.data = data as any;
114
+ return this;
115
+ }
116
+
117
+ /**
118
+ * Clears the file data from this attachment.
119
+ */
120
+ public clearFileData(): this {
121
+ this.fileData.data = undefined;
122
+ return this;
123
+ }
124
+
125
+ /**
126
+ * Sets the content type of the file data to upload with this attachment.
127
+ *
128
+ * @remarks Note that this data is NOT included in the {@link toJSON} output. To retrieve it, use {@link getRawFile}.
129
+ */
130
+ public setFileContentType(contentType: string): this {
131
+ this.fileData.contentType = contentType;
132
+ return this;
133
+ }
134
+
135
+ /**
136
+ * Clears the content type of the file data from this attachment.
137
+ */
138
+ public clearFileContentType(): this {
139
+ this.fileData.contentType = undefined;
140
+ return this;
141
+ }
142
+
143
+ /**
144
+ * Converts this attachment to a {@link RawFile} for uploading.
145
+ *
146
+ * @returns A {@link RawFile} object, or `undefined` if no file data is set
147
+ */
148
+ public getRawFile(): Partial<RawFile> | undefined {
149
+ if (!this.fileData?.data) {
150
+ return;
151
+ }
152
+
153
+ return {
154
+ ...this.fileData,
155
+ name: this.data.filename,
156
+ key: this.data.id === undefined ? undefined : `files[${this.data.id}]`,
157
+ };
158
+ }
159
+
160
+ /**
161
+ * Sets the title of this attachment.
162
+ *
163
+ * @param title - The title of the attachment
164
+ */
165
+ public setTitle(title: string): this {
166
+ this.data.title = title;
167
+ return this;
168
+ }
169
+
170
+ /**
171
+ * Clears the title of this attachment.
172
+ */
173
+ public clearTitle(): this {
174
+ this.data.title = undefined;
175
+ return this;
176
+ }
177
+
178
+ /**
179
+ * Sets the waveform of this attachment (audio clips).
180
+ *
181
+ * @param waveform - The waveform of the attachment
182
+ */
183
+ public setWaveform(waveform: string): this {
184
+ this.data.waveform = waveform;
185
+ return this;
186
+ }
187
+
188
+ /**
189
+ * Clears the waveform of this attachment.
190
+ */
191
+ public clearWaveform(): this {
192
+ this.data.waveform = undefined;
193
+ return this;
194
+ }
195
+
196
+ /**
197
+ * Serializes this builder to API-compatible JSON data.
198
+ *
199
+ * Note that by disabling validation, there is no guarantee that the resulting object will be valid.
200
+ *
201
+ * @param validationOverride - Force validation to run/not run regardless of your global preference
202
+ */
203
+ public toJSON(validationOverride?: boolean): RESTAPIAttachment {
204
+ const clone = structuredClone(this.data);
205
+ validate(attachmentPredicate, clone, validationOverride);
206
+
207
+ return clone as RESTAPIAttachment;
208
+ }
209
+ }