@lunibee/builders 0.2.1 → 0.2.3
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/README.md +24 -24
- package/package.json +4 -1
- package/src/attachment.ts +10 -1
- package/src/commands.ts +205 -88
- package/src/components.ts +200 -85
- package/src/embed.ts +10 -1
- package/src/index.ts +103 -34
package/README.md
CHANGED
|
@@ -8,31 +8,31 @@ bun add @lunibee/builders
|
|
|
8
8
|
|
|
9
9
|
```ts
|
|
10
10
|
import {
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
11
|
+
CreateEmbed,
|
|
12
|
+
CreateActionRow,
|
|
13
|
+
CreateButton,
|
|
14
|
+
ButtonType,
|
|
15
|
+
CreateStringSelectMenu,
|
|
16
|
+
CreateSlashCommand,
|
|
17
17
|
} from "@lunibee/builders";
|
|
18
18
|
|
|
19
|
-
const embed = new
|
|
19
|
+
const embed = new CreateEmbed()
|
|
20
20
|
.setTitle("Welcome")
|
|
21
21
|
.setDescription("Pick a role below.")
|
|
22
22
|
.setColor(0xf5c542)
|
|
23
23
|
.addFields([{ name: "Server", value: "Lunibee" }]);
|
|
24
24
|
|
|
25
|
-
const buttons = new
|
|
26
|
-
new
|
|
25
|
+
const buttons = new CreateActionRow().addComponents(
|
|
26
|
+
new CreateButton().setCustomId("yes").setLabel("Yes").setStyle(ButtonType.Success),
|
|
27
27
|
);
|
|
28
28
|
|
|
29
|
-
const menu = new
|
|
30
|
-
new
|
|
29
|
+
const menu = new CreateActionRow().addComponents(
|
|
30
|
+
new CreateStringSelectMenu()
|
|
31
31
|
.setCustomId("role")
|
|
32
32
|
.addOptions({ label: "Developer", value: "dev" }),
|
|
33
33
|
);
|
|
34
34
|
|
|
35
|
-
const ping = new
|
|
35
|
+
const ping = new CreateSlashCommand()
|
|
36
36
|
.setName("ping")
|
|
37
37
|
.setDescription("Replies with pong")
|
|
38
38
|
.addStringOption((option) => option.setName("message").setDescription("Echo text"));
|
|
@@ -44,26 +44,26 @@ console.log(embed.toJSON(), buttons.toJSON(), menu.toJSON(), ping.toJSON());
|
|
|
44
44
|
|
|
45
45
|
| Area | Builders |
|
|
46
46
|
|---|---|
|
|
47
|
-
| Embeds | `
|
|
48
|
-
| Buttons & rows | `
|
|
49
|
-
| Select menus | `
|
|
50
|
-
| Modals | `
|
|
51
|
-
| Slash commands | `
|
|
52
|
-
| Context menus | `
|
|
53
|
-
| Components V2 | `
|
|
54
|
-
| Files | `
|
|
47
|
+
| Embeds | `CreateEmbed` |
|
|
48
|
+
| Buttons & rows | `CreateButton`, `CreateActionRow` |
|
|
49
|
+
| Select menus | `CreateStringSelect` (alias `CreateStringSelectMenu`), `CreateEntitySelect`, `CreateUserSelectMenu`, `CreateRoleSelectMenu`, `CreateChannelSelectMenu`, `CreateMentionableSelectMenu` |
|
|
50
|
+
| Modals | `CreateModal`, `CreateTextInput` |
|
|
51
|
+
| Slash commands | `CreateSlashCommand`, `CreateSubcommand`, `CreateSubcommandGroup`, option builders |
|
|
52
|
+
| Context menus | `CreateContextMenuCommand` (`setType(2 \| 3)`), `CreateUserCommand`, `CreateMessageCommand` |
|
|
53
|
+
| Components V2 | `CreateContainer`, `CreateSection`, `CreateTextDisplay`, `CreateMediaGallery`, `CreateFileComponent`, `CreateSeparator`, `CreateThumbnail` |
|
|
54
|
+
| Files | `CreateAttachment` |
|
|
55
55
|
|
|
56
56
|
Every builder validates Discord's limits as you set values and serialises with `toJSON()`.
|
|
57
57
|
|
|
58
58
|
## What's new in 0.2.1
|
|
59
59
|
|
|
60
|
-
- `
|
|
60
|
+
- `CreateModal.addTextInputs(...inputs)` wraps each text input in its own row.
|
|
61
61
|
- `componentsV2Message(components, options?)` builds a Components V2 message with the required flag.
|
|
62
|
-
- `
|
|
63
|
-
- `
|
|
62
|
+
- `CreateEmbed` setters (`setTitle`, `setDescription`, `setURL`, `setThumbnail`, `setImage`) clear the field with `null`.
|
|
63
|
+
- `TextInputType` and `ComponentEnum` are usable as types; `CreateModal.toJSON()` is typed with a required `custom_id` and `title`.
|
|
64
64
|
|
|
65
65
|
## What's new in 0.2.0
|
|
66
66
|
|
|
67
|
-
`toJSON()` validates whole components (required fields, value bounds, action-row rules, button field exclusivity); `
|
|
67
|
+
`toJSON()` validates whole components (required fields, value bounds, action-row rules, button field exclusivity); `CreateButton.setSKUId()` for premium buttons.
|
|
68
68
|
|
|
69
69
|
Docs: https://lunibee.js.org/core-concepts/builders/
|
package/package.json
CHANGED
|
@@ -1,10 +1,13 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@lunibee/builders",
|
|
3
|
-
"version": "0.2.
|
|
3
|
+
"version": "0.2.3",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"module": "./src/index.ts",
|
|
6
6
|
"types": "./src/index.ts",
|
|
7
7
|
"exports": {
|
|
8
8
|
".": "./src/index.ts"
|
|
9
|
+
},
|
|
10
|
+
"dependencies": {
|
|
11
|
+
"@lunibee/types": "^0.2.3"
|
|
9
12
|
}
|
|
10
13
|
}
|
package/src/attachment.ts
CHANGED
|
@@ -8,7 +8,7 @@ export interface AttachmentData {
|
|
|
8
8
|
}
|
|
9
9
|
|
|
10
10
|
/** Builder for message attachments and file uploads. */
|
|
11
|
-
export class
|
|
11
|
+
export class CreateAttachment {
|
|
12
12
|
public name: string;
|
|
13
13
|
public description?: string;
|
|
14
14
|
public file: Uint8Array | ArrayBuffer | Blob | Buffer | string;
|
|
@@ -68,3 +68,12 @@ export class AttachmentBuilder {
|
|
|
68
68
|
);
|
|
69
69
|
}
|
|
70
70
|
}
|
|
71
|
+
|
|
72
|
+
// ─── Deprecated names (0.2.2), removed in 0.3.0 ────────────────────────────────
|
|
73
|
+
// Builders are now `CreateX` (`ButtonBuilder` → `CreateButton`); `…Style` became
|
|
74
|
+
// `…Type` and `…Type` became `…Enum`.
|
|
75
|
+
|
|
76
|
+
/** @deprecated Use {@link CreateAttachment}. Removed in 0.3.0. */
|
|
77
|
+
export const AttachmentBuilder = CreateAttachment;
|
|
78
|
+
/** @deprecated Use {@link CreateAttachment}. Removed in 0.3.0. */
|
|
79
|
+
export type AttachmentBuilder = CreateAttachment;
|
package/src/commands.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
/** Discord application command option types. */
|
|
2
|
-
export const
|
|
2
|
+
export const ApplicationCommandOptionEnum = {
|
|
3
3
|
Subcommand: 1,
|
|
4
4
|
SubcommandGroup: 2,
|
|
5
5
|
String: 3,
|
|
@@ -12,9 +12,22 @@ export const ApplicationCommandOptionType = {
|
|
|
12
12
|
Number: 10,
|
|
13
13
|
Attachment: 11,
|
|
14
14
|
} as const;
|
|
15
|
+
/** One {@link ApplicationCommandOptionEnum} value, usable as a type. */
|
|
16
|
+
export type ApplicationCommandOptionEnum =
|
|
17
|
+
(typeof ApplicationCommandOptionEnum)[keyof typeof ApplicationCommandOptionEnum];
|
|
18
|
+
|
|
19
|
+
/** Throws unless every context is 0 (guilds), 1 (the bot's DMs) or 2 (other DMs). */
|
|
20
|
+
function validateContexts(contexts: number[]): void {
|
|
21
|
+
if (
|
|
22
|
+
contexts.some(
|
|
23
|
+
(context) => context !== 0 && context !== 1 && context !== 2,
|
|
24
|
+
)
|
|
25
|
+
)
|
|
26
|
+
throw new RangeError("Command contexts must be 0, 1 or 2.");
|
|
27
|
+
}
|
|
15
28
|
|
|
16
29
|
/** Builds Discord application command payloads. */
|
|
17
|
-
export class
|
|
30
|
+
export class CreateSlashCommand {
|
|
18
31
|
readonly #data: Record<string, unknown> = { type: 1 };
|
|
19
32
|
/** Sets the command name. */ public setName(name: string): this {
|
|
20
33
|
validateName(name, "Command name");
|
|
@@ -28,9 +41,14 @@ export class SlashCommandBuilder {
|
|
|
28
41
|
this.#data.description = description;
|
|
29
42
|
return this;
|
|
30
43
|
}
|
|
31
|
-
/** Sets
|
|
32
|
-
|
|
33
|
-
|
|
44
|
+
/** Sets where the command can be used: 0 guilds, 1 the bot's DMs, 2 other DMs. @throws {RangeError} For any other value. */
|
|
45
|
+
public setContexts(...contexts: number[]): this {
|
|
46
|
+
validateContexts(contexts);
|
|
47
|
+
this.#data.contexts = [...contexts];
|
|
48
|
+
return this;
|
|
49
|
+
}
|
|
50
|
+
/** @deprecated Discord deprecated `dm_permission`. Use {@link CreateSlashCommand.setContexts}: `setContexts(0)` for guilds only, `setContexts(0, 1)` to allow the bot's DMs. Removed in 0.3.0. */
|
|
51
|
+
public setDMPermission(enabled: boolean): this {
|
|
34
52
|
this.#data.dm_permission = enabled;
|
|
35
53
|
return this;
|
|
36
54
|
}
|
|
@@ -57,61 +75,59 @@ export class SlashCommandBuilder {
|
|
|
57
75
|
return this;
|
|
58
76
|
}
|
|
59
77
|
/** Adds a string option. */ public addStringOption(
|
|
60
|
-
configure: (option:
|
|
78
|
+
configure: (option: CreateStringOption) => CreateStringOption,
|
|
61
79
|
): this {
|
|
62
|
-
return this.addOption(configure(new
|
|
80
|
+
return this.addOption(configure(new CreateStringOption()));
|
|
63
81
|
}
|
|
64
82
|
/** Adds an integer option. */ public addIntegerOption(
|
|
65
|
-
configure: (option:
|
|
83
|
+
configure: (option: CreateIntegerOption) => CreateIntegerOption,
|
|
66
84
|
): this {
|
|
67
|
-
return this.addOption(configure(new
|
|
85
|
+
return this.addOption(configure(new CreateIntegerOption()));
|
|
68
86
|
}
|
|
69
87
|
/** Adds a number option. */ public addNumberOption(
|
|
70
|
-
configure: (option:
|
|
88
|
+
configure: (option: CreateNumberOption) => CreateNumberOption,
|
|
71
89
|
): this {
|
|
72
|
-
return this.addOption(configure(new
|
|
90
|
+
return this.addOption(configure(new CreateNumberOption()));
|
|
73
91
|
}
|
|
74
92
|
/** Adds a boolean option. */ public addBooleanOption(
|
|
75
|
-
configure: (option:
|
|
93
|
+
configure: (option: CreateBooleanOption) => CreateBooleanOption,
|
|
76
94
|
): this {
|
|
77
|
-
return this.addOption(configure(new
|
|
95
|
+
return this.addOption(configure(new CreateBooleanOption()));
|
|
78
96
|
}
|
|
79
97
|
/** Adds a user option. */ public addUserOption(
|
|
80
|
-
configure: (option:
|
|
98
|
+
configure: (option: CreateUserOption) => CreateUserOption,
|
|
81
99
|
): this {
|
|
82
|
-
return this.addOption(configure(new
|
|
100
|
+
return this.addOption(configure(new CreateUserOption()));
|
|
83
101
|
}
|
|
84
102
|
/** Adds a channel option. */ public addChannelOption(
|
|
85
|
-
configure: (option:
|
|
103
|
+
configure: (option: CreateChannelOption) => CreateChannelOption,
|
|
86
104
|
): this {
|
|
87
|
-
return this.addOption(configure(new
|
|
105
|
+
return this.addOption(configure(new CreateChannelOption()));
|
|
88
106
|
}
|
|
89
107
|
/** Adds a role option. */ public addRoleOption(
|
|
90
|
-
configure: (option:
|
|
108
|
+
configure: (option: CreateRoleOption) => CreateRoleOption,
|
|
91
109
|
): this {
|
|
92
|
-
return this.addOption(configure(new
|
|
110
|
+
return this.addOption(configure(new CreateRoleOption()));
|
|
93
111
|
}
|
|
94
112
|
/** Adds a mentionable option. */ public addMentionableOption(
|
|
95
|
-
configure: (
|
|
96
|
-
option: MentionableOptionBuilder,
|
|
97
|
-
) => MentionableOptionBuilder,
|
|
113
|
+
configure: (option: CreateMentionableOption) => CreateMentionableOption,
|
|
98
114
|
): this {
|
|
99
|
-
return this.addOption(configure(new
|
|
115
|
+
return this.addOption(configure(new CreateMentionableOption()));
|
|
100
116
|
}
|
|
101
117
|
/** Adds an attachment option. */ public addAttachmentOption(
|
|
102
|
-
configure: (option:
|
|
118
|
+
configure: (option: CreateAttachmentOption) => CreateAttachmentOption,
|
|
103
119
|
): this {
|
|
104
|
-
return this.addOption(configure(new
|
|
120
|
+
return this.addOption(configure(new CreateAttachmentOption()));
|
|
105
121
|
}
|
|
106
122
|
/** Adds a subcommand. */ public addSubcommand(
|
|
107
|
-
configure: (option:
|
|
123
|
+
configure: (option: CreateSubcommand) => CreateSubcommand,
|
|
108
124
|
): this {
|
|
109
|
-
return this.addOption(configure(new
|
|
125
|
+
return this.addOption(configure(new CreateSubcommand()));
|
|
110
126
|
}
|
|
111
127
|
/** Adds a subcommand group. */ public addSubcommandGroup(
|
|
112
|
-
configure: (option:
|
|
128
|
+
configure: (option: CreateSubcommandGroup) => CreateSubcommandGroup,
|
|
113
129
|
): this {
|
|
114
|
-
return this.addOption(configure(new
|
|
130
|
+
return this.addOption(configure(new CreateSubcommandGroup()));
|
|
115
131
|
}
|
|
116
132
|
/** Serializes the command payload. */ public toJSON(): Record<
|
|
117
133
|
string,
|
|
@@ -120,7 +136,7 @@ export class SlashCommandBuilder {
|
|
|
120
136
|
return structuredClone(this.#data);
|
|
121
137
|
}
|
|
122
138
|
/** Adds a validated top-level command option. */ protected addOption(
|
|
123
|
-
option:
|
|
139
|
+
option: CreateCommandOption,
|
|
124
140
|
): this {
|
|
125
141
|
const options = (this.#data.options as unknown[] | undefined) ?? [];
|
|
126
142
|
if (options.length >= 25)
|
|
@@ -154,7 +170,7 @@ export class SlashCommandBuilder {
|
|
|
154
170
|
}
|
|
155
171
|
|
|
156
172
|
/** Base builder for command options. */
|
|
157
|
-
export class
|
|
173
|
+
export class CreateCommandOption {
|
|
158
174
|
protected readonly data: Record<string, unknown>;
|
|
159
175
|
/** Creates an option builder. */ public constructor(type: number) {
|
|
160
176
|
this.data = { type };
|
|
@@ -190,9 +206,9 @@ export class CommandOptionBuilder {
|
|
|
190
206
|
}
|
|
191
207
|
|
|
192
208
|
/** Builds a string command option. */
|
|
193
|
-
export class
|
|
209
|
+
export class CreateStringOption extends CreateCommandOption {
|
|
194
210
|
/** Creates a string option. */ public constructor() {
|
|
195
|
-
super(
|
|
211
|
+
super(ApplicationCommandOptionEnum.String);
|
|
196
212
|
}
|
|
197
213
|
/** Adds string choices. */ public addChoices(
|
|
198
214
|
...choices: Array<{ name: string; value: string }>
|
|
@@ -237,9 +253,9 @@ export class StringOptionBuilder extends CommandOptionBuilder {
|
|
|
237
253
|
}
|
|
238
254
|
}
|
|
239
255
|
/** Builds an integer command option. */
|
|
240
|
-
export class
|
|
256
|
+
export class CreateIntegerOption extends CreateCommandOption {
|
|
241
257
|
public constructor() {
|
|
242
|
-
super(
|
|
258
|
+
super(ApplicationCommandOptionEnum.Integer);
|
|
243
259
|
}
|
|
244
260
|
public setMinValue(value: number): this {
|
|
245
261
|
validateNumberRange(
|
|
@@ -268,7 +284,7 @@ export class IntegerOptionBuilder extends CommandOptionBuilder {
|
|
|
268
284
|
this.data.max_value = value;
|
|
269
285
|
return this;
|
|
270
286
|
}
|
|
271
|
-
public setAutocomplete(enabled = true): this {
|
|
287
|
+
public override setAutocomplete(enabled = true): this {
|
|
272
288
|
if (
|
|
273
289
|
enabled &&
|
|
274
290
|
Array.isArray(this.data.choices) &&
|
|
@@ -293,9 +309,9 @@ export class IntegerOptionBuilder extends CommandOptionBuilder {
|
|
|
293
309
|
}
|
|
294
310
|
}
|
|
295
311
|
/** Builds a number command option. */
|
|
296
|
-
export class
|
|
312
|
+
export class CreateNumberOption extends CreateCommandOption {
|
|
297
313
|
public constructor() {
|
|
298
|
-
super(
|
|
314
|
+
super(ApplicationCommandOptionEnum.Number);
|
|
299
315
|
}
|
|
300
316
|
public setMinValue(value: number): this {
|
|
301
317
|
validateNumberRange(
|
|
@@ -324,7 +340,7 @@ export class NumberOptionBuilder extends CommandOptionBuilder {
|
|
|
324
340
|
this.data.max_value = value;
|
|
325
341
|
return this;
|
|
326
342
|
}
|
|
327
|
-
public setAutocomplete(enabled = true): this {
|
|
343
|
+
public override setAutocomplete(enabled = true): this {
|
|
328
344
|
if (
|
|
329
345
|
enabled &&
|
|
330
346
|
Array.isArray(this.data.choices) &&
|
|
@@ -348,34 +364,34 @@ export class NumberOptionBuilder extends CommandOptionBuilder {
|
|
|
348
364
|
return this;
|
|
349
365
|
}
|
|
350
366
|
}
|
|
351
|
-
/** Builds a boolean command option. */ export class
|
|
367
|
+
/** Builds a boolean command option. */ export class CreateBooleanOption extends CreateCommandOption {
|
|
352
368
|
/** Creates a boolean option. */ public constructor() {
|
|
353
|
-
super(
|
|
369
|
+
super(ApplicationCommandOptionEnum.Boolean);
|
|
354
370
|
}
|
|
355
371
|
}
|
|
356
|
-
/** Builds a user command option. */ export class
|
|
372
|
+
/** Builds a user command option. */ export class CreateUserOption extends CreateCommandOption {
|
|
357
373
|
/** Creates a user option. */ public constructor() {
|
|
358
|
-
super(
|
|
374
|
+
super(ApplicationCommandOptionEnum.User);
|
|
359
375
|
}
|
|
360
376
|
}
|
|
361
|
-
/** Builds a role command option. */ export class
|
|
377
|
+
/** Builds a role command option. */ export class CreateRoleOption extends CreateCommandOption {
|
|
362
378
|
/** Creates a role option. */ public constructor() {
|
|
363
|
-
super(
|
|
379
|
+
super(ApplicationCommandOptionEnum.Role);
|
|
364
380
|
}
|
|
365
381
|
}
|
|
366
|
-
/** Builds a mentionable command option. */ export class
|
|
382
|
+
/** Builds a mentionable command option. */ export class CreateMentionableOption extends CreateCommandOption {
|
|
367
383
|
/** Creates a mentionable option. */ public constructor() {
|
|
368
|
-
super(
|
|
384
|
+
super(ApplicationCommandOptionEnum.Mentionable);
|
|
369
385
|
}
|
|
370
386
|
}
|
|
371
|
-
/** Builds an attachment command option. */ export class
|
|
387
|
+
/** Builds an attachment command option. */ export class CreateAttachmentOption extends CreateCommandOption {
|
|
372
388
|
/** Creates an attachment option. */ public constructor() {
|
|
373
|
-
super(
|
|
389
|
+
super(ApplicationCommandOptionEnum.Attachment);
|
|
374
390
|
}
|
|
375
391
|
}
|
|
376
|
-
/** Builds a channel command option. */ export class
|
|
392
|
+
/** Builds a channel command option. */ export class CreateChannelOption extends CreateCommandOption {
|
|
377
393
|
/** Creates a channel option. */ public constructor() {
|
|
378
|
-
super(
|
|
394
|
+
super(ApplicationCommandOptionEnum.Channel);
|
|
379
395
|
}
|
|
380
396
|
/** Restricts accepted channel types. */ public addChannelTypes(
|
|
381
397
|
...types: number[]
|
|
@@ -388,58 +404,56 @@ export class NumberOptionBuilder extends CommandOptionBuilder {
|
|
|
388
404
|
return this;
|
|
389
405
|
}
|
|
390
406
|
}
|
|
391
|
-
/** Builds a nested subcommand. */ export class
|
|
407
|
+
/** Builds a nested subcommand. */ export class CreateSubcommand extends CreateCommandOption {
|
|
392
408
|
/** Creates a subcommand. */ public constructor() {
|
|
393
|
-
super(
|
|
409
|
+
super(ApplicationCommandOptionEnum.Subcommand);
|
|
394
410
|
}
|
|
395
411
|
/** Adds a string option to this subcommand. */ public addStringOption(
|
|
396
|
-
configure: (option:
|
|
412
|
+
configure: (option: CreateStringOption) => CreateStringOption,
|
|
397
413
|
): this {
|
|
398
|
-
return this.addChildOption(configure(new
|
|
414
|
+
return this.addChildOption(configure(new CreateStringOption()));
|
|
399
415
|
}
|
|
400
416
|
/** Adds an integer option to this subcommand. */ public addIntegerOption(
|
|
401
|
-
configure: (option:
|
|
417
|
+
configure: (option: CreateIntegerOption) => CreateIntegerOption,
|
|
402
418
|
): this {
|
|
403
|
-
return this.addChildOption(configure(new
|
|
419
|
+
return this.addChildOption(configure(new CreateIntegerOption()));
|
|
404
420
|
}
|
|
405
421
|
/** Adds a number option to this subcommand. */ public addNumberOption(
|
|
406
|
-
configure: (option:
|
|
422
|
+
configure: (option: CreateNumberOption) => CreateNumberOption,
|
|
407
423
|
): this {
|
|
408
|
-
return this.addChildOption(configure(new
|
|
424
|
+
return this.addChildOption(configure(new CreateNumberOption()));
|
|
409
425
|
}
|
|
410
426
|
/** Adds a boolean option to this subcommand. */ public addBooleanOption(
|
|
411
|
-
configure: (option:
|
|
427
|
+
configure: (option: CreateBooleanOption) => CreateBooleanOption,
|
|
412
428
|
): this {
|
|
413
|
-
return this.addChildOption(configure(new
|
|
429
|
+
return this.addChildOption(configure(new CreateBooleanOption()));
|
|
414
430
|
}
|
|
415
431
|
/** Adds a user option to this subcommand. */ public addUserOption(
|
|
416
|
-
configure: (option:
|
|
432
|
+
configure: (option: CreateUserOption) => CreateUserOption,
|
|
417
433
|
): this {
|
|
418
|
-
return this.addChildOption(configure(new
|
|
434
|
+
return this.addChildOption(configure(new CreateUserOption()));
|
|
419
435
|
}
|
|
420
436
|
/** Adds a channel option to this subcommand. */ public addChannelOption(
|
|
421
|
-
configure: (option:
|
|
437
|
+
configure: (option: CreateChannelOption) => CreateChannelOption,
|
|
422
438
|
): this {
|
|
423
|
-
return this.addChildOption(configure(new
|
|
439
|
+
return this.addChildOption(configure(new CreateChannelOption()));
|
|
424
440
|
}
|
|
425
441
|
/** Adds a role option to this subcommand. */ public addRoleOption(
|
|
426
|
-
configure: (option:
|
|
442
|
+
configure: (option: CreateRoleOption) => CreateRoleOption,
|
|
427
443
|
): this {
|
|
428
|
-
return this.addChildOption(configure(new
|
|
444
|
+
return this.addChildOption(configure(new CreateRoleOption()));
|
|
429
445
|
}
|
|
430
446
|
/** Adds a mentionable option to this subcommand. */ public addMentionableOption(
|
|
431
|
-
configure: (
|
|
432
|
-
option: MentionableOptionBuilder,
|
|
433
|
-
) => MentionableOptionBuilder,
|
|
447
|
+
configure: (option: CreateMentionableOption) => CreateMentionableOption,
|
|
434
448
|
): this {
|
|
435
|
-
return this.addChildOption(configure(new
|
|
449
|
+
return this.addChildOption(configure(new CreateMentionableOption()));
|
|
436
450
|
}
|
|
437
451
|
/** Adds an attachment option to this subcommand. */ public addAttachmentOption(
|
|
438
|
-
configure: (option:
|
|
452
|
+
configure: (option: CreateAttachmentOption) => CreateAttachmentOption,
|
|
439
453
|
): this {
|
|
440
|
-
return this.addChildOption(configure(new
|
|
454
|
+
return this.addChildOption(configure(new CreateAttachmentOption()));
|
|
441
455
|
}
|
|
442
|
-
protected addChildOption(option:
|
|
456
|
+
protected addChildOption(option: CreateCommandOption): this {
|
|
443
457
|
const options = (this.data.options as unknown[] | undefined) ?? [];
|
|
444
458
|
if (options.length >= 25)
|
|
445
459
|
throw new RangeError(
|
|
@@ -469,7 +483,7 @@ export class NumberOptionBuilder extends CommandOptionBuilder {
|
|
|
469
483
|
options.some(
|
|
470
484
|
(existing) =>
|
|
471
485
|
(existing as Record<string, unknown>).type ===
|
|
472
|
-
|
|
486
|
+
ApplicationCommandOptionEnum.Subcommand,
|
|
473
487
|
)
|
|
474
488
|
)
|
|
475
489
|
throw new RangeError(
|
|
@@ -480,19 +494,19 @@ export class NumberOptionBuilder extends CommandOptionBuilder {
|
|
|
480
494
|
return this;
|
|
481
495
|
}
|
|
482
496
|
}
|
|
483
|
-
/** Builds a nested subcommand group. */ export class
|
|
497
|
+
/** Builds a nested subcommand group. */ export class CreateSubcommandGroup extends CreateCommandOption {
|
|
484
498
|
/** Creates a subcommand group. */ public constructor() {
|
|
485
|
-
super(
|
|
499
|
+
super(ApplicationCommandOptionEnum.SubcommandGroup);
|
|
486
500
|
}
|
|
487
501
|
/** Adds a subcommand to this group. */ public addSubcommand(
|
|
488
|
-
configure: (option:
|
|
502
|
+
configure: (option: CreateSubcommand) => CreateSubcommand,
|
|
489
503
|
): this {
|
|
490
504
|
const options = (this.data.options as unknown[] | undefined) ?? [];
|
|
491
505
|
if (options.length >= 25)
|
|
492
506
|
throw new RangeError(
|
|
493
507
|
"A subcommand group cannot contain more than 25 subcommands.",
|
|
494
508
|
);
|
|
495
|
-
const payload = configure(new
|
|
509
|
+
const payload = configure(new CreateSubcommand()).toJSON();
|
|
496
510
|
if (
|
|
497
511
|
options.some(
|
|
498
512
|
(existing) =>
|
|
@@ -502,7 +516,7 @@ export class NumberOptionBuilder extends CommandOptionBuilder {
|
|
|
502
516
|
throw new RangeError(
|
|
503
517
|
`Duplicate subcommand name: ${String(payload.name)}.`,
|
|
504
518
|
);
|
|
505
|
-
if (payload.type !==
|
|
519
|
+
if (payload.type !== ApplicationCommandOptionEnum.Subcommand)
|
|
506
520
|
throw new TypeError(
|
|
507
521
|
"Subcommand group children must be subcommands.",
|
|
508
522
|
);
|
|
@@ -553,8 +567,8 @@ function validateNumberRange(
|
|
|
553
567
|
// ─── Context Menu Builders ────────────────────────────────────────────────────
|
|
554
568
|
|
|
555
569
|
/** Builder for application commands that appear in right-click context menus.
|
|
556
|
-
* Discord.js-familiar: `new
|
|
557
|
-
export class
|
|
570
|
+
* Discord.js-familiar: `new CreateContextMenuCommand().setName("x").setType(2)`. */
|
|
571
|
+
export class CreateContextMenuCommand {
|
|
558
572
|
protected readonly data: Record<string, unknown>;
|
|
559
573
|
public constructor(type: 2 | 3 = 2) {
|
|
560
574
|
this.data = { type };
|
|
@@ -592,16 +606,30 @@ export class ContextMenuCommandBuilder {
|
|
|
592
606
|
permissions === null ? null : BigInt(permissions).toString();
|
|
593
607
|
return this;
|
|
594
608
|
}
|
|
595
|
-
/**
|
|
609
|
+
/** @deprecated Discord deprecated `dm_permission`. Use {@link CreateContextMenuCommand.setContexts}: `setContexts(0)` for guilds only, `setContexts(0, 1)` to allow the bot's DMs. Removed in 0.3.0. */
|
|
596
610
|
public setDMPermission(enabled: boolean): this {
|
|
597
611
|
this.data.dm_permission = enabled;
|
|
598
612
|
return this;
|
|
599
613
|
}
|
|
600
|
-
/** Sets command integration types. */
|
|
614
|
+
/** Sets command integration types (0 guild install, 1 user install). */
|
|
601
615
|
public setIntegrationTypes(...types: number[]): this {
|
|
602
616
|
this.data.integration_types = [...types];
|
|
603
617
|
return this;
|
|
604
618
|
}
|
|
619
|
+
/** Sets where the command can be used: 0 guilds, 1 the bot's DMs, 2 other DMs. @throws {RangeError} For any other value. */
|
|
620
|
+
public setContexts(...contexts: number[]): this {
|
|
621
|
+
validateContexts(contexts);
|
|
622
|
+
this.data.contexts = [...contexts];
|
|
623
|
+
return this;
|
|
624
|
+
}
|
|
625
|
+
/** Sets the name shown per locale, e.g. `{ "es-ES": "Ver perfil" }`; null clears it. */
|
|
626
|
+
public setNameLocalizations(
|
|
627
|
+
localizations: Record<string, string> | null,
|
|
628
|
+
): this {
|
|
629
|
+
this.data.name_localizations =
|
|
630
|
+
localizations === null ? null : { ...localizations };
|
|
631
|
+
return this;
|
|
632
|
+
}
|
|
605
633
|
/** Serializes the command payload for the Discord API. */
|
|
606
634
|
public toJSON(): Record<string, unknown> {
|
|
607
635
|
return structuredClone(this.data);
|
|
@@ -610,9 +638,9 @@ export class ContextMenuCommandBuilder {
|
|
|
610
638
|
|
|
611
639
|
/** Builds a User context menu command (appears when right-clicking a user, type 2).
|
|
612
640
|
* @example
|
|
613
|
-
* new
|
|
641
|
+
* new CreateUserCommand().setName("View Profile").toJSON()
|
|
614
642
|
*/
|
|
615
|
-
export class
|
|
643
|
+
export class CreateUserCommand extends CreateContextMenuCommand {
|
|
616
644
|
public constructor() {
|
|
617
645
|
super(2);
|
|
618
646
|
}
|
|
@@ -620,10 +648,99 @@ export class UserCommandBuilder extends ContextMenuCommandBuilder {
|
|
|
620
648
|
|
|
621
649
|
/** Builds a Message context menu command (appears when right-clicking a message, type 3).
|
|
622
650
|
* @example
|
|
623
|
-
* new
|
|
651
|
+
* new CreateMessageCommand().setName("Translate Message").toJSON()
|
|
624
652
|
*/
|
|
625
|
-
export class
|
|
653
|
+
export class CreateMessageCommand extends CreateContextMenuCommand {
|
|
626
654
|
public constructor() {
|
|
627
655
|
super(3);
|
|
628
656
|
}
|
|
629
657
|
}
|
|
658
|
+
|
|
659
|
+
// ─── Deprecated names (0.2.2), removed in 0.3.0 ────────────────────────────────
|
|
660
|
+
// Builders are now `CreateX` (`ButtonBuilder` → `CreateButton`); `…Style` became
|
|
661
|
+
// `…Type` and `…Type` became `…Enum`.
|
|
662
|
+
|
|
663
|
+
/** @deprecated Use {@link CreateAttachmentOption}. Removed in 0.3.0. */
|
|
664
|
+
export const AttachmentOptionBuilder = CreateAttachmentOption;
|
|
665
|
+
/** @deprecated Use {@link CreateAttachmentOption}. Removed in 0.3.0. */
|
|
666
|
+
export type AttachmentOptionBuilder = CreateAttachmentOption;
|
|
667
|
+
|
|
668
|
+
/** @deprecated Use {@link CreateBooleanOption}. Removed in 0.3.0. */
|
|
669
|
+
export const BooleanOptionBuilder = CreateBooleanOption;
|
|
670
|
+
/** @deprecated Use {@link CreateBooleanOption}. Removed in 0.3.0. */
|
|
671
|
+
export type BooleanOptionBuilder = CreateBooleanOption;
|
|
672
|
+
|
|
673
|
+
/** @deprecated Use {@link CreateChannelOption}. Removed in 0.3.0. */
|
|
674
|
+
export const ChannelOptionBuilder = CreateChannelOption;
|
|
675
|
+
/** @deprecated Use {@link CreateChannelOption}. Removed in 0.3.0. */
|
|
676
|
+
export type ChannelOptionBuilder = CreateChannelOption;
|
|
677
|
+
|
|
678
|
+
/** @deprecated Use {@link CreateCommandOption}. Removed in 0.3.0. */
|
|
679
|
+
export const CommandOptionBuilder = CreateCommandOption;
|
|
680
|
+
/** @deprecated Use {@link CreateCommandOption}. Removed in 0.3.0. */
|
|
681
|
+
export type CommandOptionBuilder = CreateCommandOption;
|
|
682
|
+
|
|
683
|
+
/** @deprecated Use {@link CreateContextMenuCommand}. Removed in 0.3.0. */
|
|
684
|
+
export const ContextMenuCommandBuilder = CreateContextMenuCommand;
|
|
685
|
+
/** @deprecated Use {@link CreateContextMenuCommand}. Removed in 0.3.0. */
|
|
686
|
+
export type ContextMenuCommandBuilder = CreateContextMenuCommand;
|
|
687
|
+
|
|
688
|
+
/** @deprecated Use {@link CreateIntegerOption}. Removed in 0.3.0. */
|
|
689
|
+
export const IntegerOptionBuilder = CreateIntegerOption;
|
|
690
|
+
/** @deprecated Use {@link CreateIntegerOption}. Removed in 0.3.0. */
|
|
691
|
+
export type IntegerOptionBuilder = CreateIntegerOption;
|
|
692
|
+
|
|
693
|
+
/** @deprecated Use {@link CreateMentionableOption}. Removed in 0.3.0. */
|
|
694
|
+
export const MentionableOptionBuilder = CreateMentionableOption;
|
|
695
|
+
/** @deprecated Use {@link CreateMentionableOption}. Removed in 0.3.0. */
|
|
696
|
+
export type MentionableOptionBuilder = CreateMentionableOption;
|
|
697
|
+
|
|
698
|
+
/** @deprecated Use {@link CreateMessageCommand}. Removed in 0.3.0. */
|
|
699
|
+
export const MessageCommandBuilder = CreateMessageCommand;
|
|
700
|
+
/** @deprecated Use {@link CreateMessageCommand}. Removed in 0.3.0. */
|
|
701
|
+
export type MessageCommandBuilder = CreateMessageCommand;
|
|
702
|
+
|
|
703
|
+
/** @deprecated Use {@link CreateNumberOption}. Removed in 0.3.0. */
|
|
704
|
+
export const NumberOptionBuilder = CreateNumberOption;
|
|
705
|
+
/** @deprecated Use {@link CreateNumberOption}. Removed in 0.3.0. */
|
|
706
|
+
export type NumberOptionBuilder = CreateNumberOption;
|
|
707
|
+
|
|
708
|
+
/** @deprecated Use {@link CreateRoleOption}. Removed in 0.3.0. */
|
|
709
|
+
export const RoleOptionBuilder = CreateRoleOption;
|
|
710
|
+
/** @deprecated Use {@link CreateRoleOption}. Removed in 0.3.0. */
|
|
711
|
+
export type RoleOptionBuilder = CreateRoleOption;
|
|
712
|
+
|
|
713
|
+
/** @deprecated Use {@link CreateSlashCommand}. Removed in 0.3.0. */
|
|
714
|
+
export const SlashCommandBuilder = CreateSlashCommand;
|
|
715
|
+
/** @deprecated Use {@link CreateSlashCommand}. Removed in 0.3.0. */
|
|
716
|
+
export type SlashCommandBuilder = CreateSlashCommand;
|
|
717
|
+
|
|
718
|
+
/** @deprecated Use {@link CreateStringOption}. Removed in 0.3.0. */
|
|
719
|
+
export const StringOptionBuilder = CreateStringOption;
|
|
720
|
+
/** @deprecated Use {@link CreateStringOption}. Removed in 0.3.0. */
|
|
721
|
+
export type StringOptionBuilder = CreateStringOption;
|
|
722
|
+
|
|
723
|
+
/** @deprecated Use {@link CreateSubcommand}. Removed in 0.3.0. */
|
|
724
|
+
export const SubcommandBuilder = CreateSubcommand;
|
|
725
|
+
/** @deprecated Use {@link CreateSubcommand}. Removed in 0.3.0. */
|
|
726
|
+
export type SubcommandBuilder = CreateSubcommand;
|
|
727
|
+
|
|
728
|
+
/** @deprecated Use {@link CreateSubcommandGroup}. Removed in 0.3.0. */
|
|
729
|
+
export const SubcommandGroupBuilder = CreateSubcommandGroup;
|
|
730
|
+
/** @deprecated Use {@link CreateSubcommandGroup}. Removed in 0.3.0. */
|
|
731
|
+
export type SubcommandGroupBuilder = CreateSubcommandGroup;
|
|
732
|
+
|
|
733
|
+
/** @deprecated Use {@link CreateUserCommand}. Removed in 0.3.0. */
|
|
734
|
+
export const UserCommandBuilder = CreateUserCommand;
|
|
735
|
+
/** @deprecated Use {@link CreateUserCommand}. Removed in 0.3.0. */
|
|
736
|
+
export type UserCommandBuilder = CreateUserCommand;
|
|
737
|
+
|
|
738
|
+
/** @deprecated Use {@link CreateUserOption}. Removed in 0.3.0. */
|
|
739
|
+
export const UserOptionBuilder = CreateUserOption;
|
|
740
|
+
/** @deprecated Use {@link CreateUserOption}. Removed in 0.3.0. */
|
|
741
|
+
export type UserOptionBuilder = CreateUserOption;
|
|
742
|
+
|
|
743
|
+
/** @deprecated Use {@link ApplicationCommandOptionEnum}. Removed in 0.3.0. */
|
|
744
|
+
export const ApplicationCommandOptionType = ApplicationCommandOptionEnum;
|
|
745
|
+
/** @deprecated Use {@link ApplicationCommandOptionEnum}. Removed in 0.3.0. */
|
|
746
|
+
export type ApplicationCommandOptionType = ApplicationCommandOptionEnum;
|