MessageButton.js
4.43 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
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
'use strict';
const BaseMessageComponent = require('./BaseMessageComponent');
const { RangeError } = require('../errors');
const { MessageButtonStyles, MessageComponentTypes } = require('../util/Constants');
const Util = require('../util/Util');
/**
* Represents a button message component.
* @extends {BaseMessageComponent}
*/
class MessageButton extends BaseMessageComponent {
/**
* @typedef {BaseMessageComponentOptions} MessageButtonOptions
* @property {string} [label] The text to be displayed on this button
* @property {string} [customId] A unique string to be sent in the interaction when clicked
* @property {MessageButtonStyleResolvable} [style] The style of this button
* @property {EmojiIdentifierResolvable} [emoji] The emoji to be displayed to the left of the text
* @property {string} [url] Optional URL for link-style buttons
* @property {boolean} [disabled=false] Disables the button to prevent interactions
*/
/**
* @param {MessageButton|MessageButtonOptions} [data={}] MessageButton to clone or raw data
*/
constructor(data = {}) {
super({ type: 'BUTTON' });
this.setup(data);
}
setup(data) {
/**
* The text to be displayed on this button
* @type {?string}
*/
this.label = data.label ?? null;
/**
* A unique string to be sent in the interaction when clicked
* @type {?string}
*/
this.customId = data.custom_id ?? data.customId ?? null;
/**
* The style of this button
* @type {?MessageButtonStyle}
*/
this.style = data.style ? MessageButton.resolveStyle(data.style) : null;
/**
* Emoji for this button
* @type {?RawEmoji}
*/
this.emoji = data.emoji ? Util.resolvePartialEmoji(data.emoji) : null;
/**
* The URL this button links to, if it is a Link style button
* @type {?string}
*/
this.url = data.url ?? null;
/**
* Whether this button is currently disabled
* @type {boolean}
*/
this.disabled = data.disabled ?? false;
}
/**
* Sets the custom id for this button
* @param {string} customId A unique string to be sent in the interaction when clicked
* @returns {MessageButton}
*/
setCustomId(customId) {
this.customId = Util.verifyString(customId, RangeError, 'BUTTON_CUSTOM_ID');
return this;
}
/**
* Sets the interactive status of the button
* @param {boolean} [disabled=true] Whether this button should be disabled
* @returns {MessageButton}
*/
setDisabled(disabled = true) {
this.disabled = disabled;
return this;
}
/**
* Set the emoji of this button
* @param {EmojiIdentifierResolvable} emoji The emoji to be displayed on this button
* @returns {MessageButton}
*/
setEmoji(emoji) {
this.emoji = Util.resolvePartialEmoji(emoji);
return this;
}
/**
* Sets the label of this button
* @param {string} label The text to be displayed on this button
* @returns {MessageButton}
*/
setLabel(label) {
this.label = Util.verifyString(label, RangeError, 'BUTTON_LABEL');
return this;
}
/**
* Sets the style of this button
* @param {MessageButtonStyleResolvable} style The style of this button
* @returns {MessageButton}
*/
setStyle(style) {
this.style = MessageButton.resolveStyle(style);
return this;
}
/**
* Sets the URL of this button.
* <info>MessageButton#style must be LINK when setting a URL</info>
* @param {string} url The URL of this button
* @returns {MessageButton}
*/
setURL(url) {
this.url = Util.verifyString(url, RangeError, 'BUTTON_URL');
return this;
}
/**
* Transforms the button to a plain object.
* @returns {APIMessageButton} The raw data of this button
*/
toJSON() {
return {
custom_id: this.customId,
disabled: this.disabled,
emoji: this.emoji,
label: this.label,
style: MessageButtonStyles[this.style],
type: MessageComponentTypes[this.type],
url: this.url,
};
}
/**
* Data that can be resolved to a MessageButtonStyle. This can be
* * MessageButtonStyle
* * number
* @typedef {number|MessageButtonStyle} MessageButtonStyleResolvable
*/
/**
* Resolves the style of a button
* @param {MessageButtonStyleResolvable} style The style to resolve
* @returns {MessageButtonStyle}
* @private
*/
static resolveStyle(style) {
return typeof style === 'string' ? style : MessageButtonStyles[style];
}
}
module.exports = MessageButton;