forked from discordjs/discord.js
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCheckbox.ts
More file actions
63 lines (59 loc) · 1.57 KB
/
Checkbox.ts
File metadata and controls
63 lines (59 loc) · 1.57 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
import type { APICheckboxComponent } from 'discord-api-types/v10';
import { ComponentType } from 'discord-api-types/v10';
import { ComponentBuilder } from '../Component';
import { checkboxPredicate } from './Assertions';
/**
* A builder that creates API-compatible JSON data for checkboxes.
*/
export class CheckboxBuilder extends ComponentBuilder<APICheckboxComponent> {
/**
* Creates a new checkbox from API data.
*
* @param data - The API data to create this checkbox with
* @example
* Creating a checkbox from an API data object:
* ```ts
* const checkbox = new CheckboxBuilder({
* custom_id: 'accept_terms',
* default: false,
* });
* ```
* @example
* Creating a checkbox using setters and API data:
* ```ts
* const checkbox = new CheckboxBuilder()
* .setCustomId('subscribe_newsletter')
* .setDefault(true);
* ```
*/
public constructor(data?: Partial<APICheckboxComponent>) {
super({ type: ComponentType.Checkbox, ...data });
}
/**
* Sets the custom ID of this checkbox.
*
* @param customId - The custom ID to use
*/
public setCustomId(customId: string) {
this.data.custom_id = customId;
return this;
}
/**
* Sets whether this checkbox is checked by default.
*
* @param isDefault - Whether the checkbox should be checked by default
*/
public setDefault(isDefault: boolean) {
this.data.default = isDefault;
return this;
}
/**
* {@inheritDoc ComponentBuilder.toJSON}
*/
public override toJSON(): APICheckboxComponent {
checkboxPredicate.parse(this.data);
return {
...this.data,
} as APICheckboxComponent;
}
}