-
-
Notifications
You must be signed in to change notification settings - Fork 28
/
Button.ts
93 lines (84 loc) · 2.3 KB
/
Button.ts
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
import {
type APIButtonComponentWithCustomId,
type APIButtonComponentWithURL,
type APIMessageComponentEmoji,
type ButtonStyle,
ComponentType,
} from 'discord-api-types/v10';
import { throwError } from '..';
import type { EmojiResolvable, When } from '../common';
import { resolvePartialEmoji } from '../structures/extra/functions';
export type ButtonStylesForID = Exclude<ButtonStyle, ButtonStyle.Link>;
/**
* Represents a button component.
* @template Type - The type of the button component.
*/
export class Button<Type extends boolean = boolean> {
/**
* Creates a new Button instance.
* @param data - The initial data for the button.
*/
constructor(public data: Partial<When<Type, APIButtonComponentWithCustomId, APIButtonComponentWithURL>> = {}) {
this.data.type = ComponentType.Button;
}
/**
* Sets the custom ID for the button.
* @param id - The custom ID to set.
* @returns The modified Button instance.
*/
setCustomId(id: string) {
// @ts-expect-error
this.data.custom_id = id;
return this;
}
/**
* Sets the URL for the button.
* @param url - The URL to set.
* @returns The modified Button instance.
*/
setURL(url: string) {
// @ts-expect-error
this.data.url = url;
return this;
}
/**
* Sets the label for the button.
* @param label - The label to set.
* @returns The modified Button instance.
*/
setLabel(label: string) {
this.data.label = label;
return this;
}
/**
* Sets the emoji for the button.
* @param emoji - The emoji to set.
* @returns The modified Button instance.
*/
setEmoji(emoji: EmojiResolvable) {
const resolve = resolvePartialEmoji(emoji);
if (!resolve) return throwError('Invalid Emoji');
this.data.emoji = resolve as APIMessageComponentEmoji;
return this;
}
/**
* Sets the disabled state of the button.
* @param disabled - Whether the button should be disabled or not.
* @returns The modified Button instance.
*/
setDisabled(disabled = true) {
this.data.disabled = disabled;
return this;
}
setStyle(style: ButtonStyle) {
this.data.style = style;
return this;
}
/**
* Converts the Button instance to its JSON representation.
* @returns The JSON representation of the Button instance.
*/
toJSON() {
return { ...this.data } as When<Type, APIButtonComponentWithCustomId, APIButtonComponentWithURL>;
}
}