/
Button.java
571 lines (543 loc) · 20.1 KB
/
Button.java
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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
/*
* Copyright 2015 Austin Keener, Michael Ritter, Florian Spieß, and the JDA contributors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package net.dv8tion.jda.api.interactions.components;
import net.dv8tion.jda.api.entities.Emoji;
import net.dv8tion.jda.internal.interactions.ButtonImpl;
import net.dv8tion.jda.internal.utils.Checks;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
/**
* Represents a Message Button.
* <br>These buttons are located below the message in {@link ActionRow ActionRows}.
*
* <p>Each button has either a {@code custom_id} or URL attached.
* The id has to be provided by the user and can be used to identify the button in the {@link net.dv8tion.jda.api.events.interaction.ButtonClickEvent ButtonClickEvent}.
*
* <h2>Example Usage</h2>
* <pre>{@code
* public class HelloBot extends ListenerAdapter {
* public void onSlashCommand(SlashCommandEvent event) {
* if (event.getName().equals("hello")) {
* event.reply("Click the button to say hello")
* .addActionRow(
* Button.primary("hello", "Click Me"), // Button with only a label
* Button.success("emoji", Emoji.fromMarkdown("<:minn:245267426227388416>"))) // Button with only an emoji
* .queue();
* } else if (event.getName().equals("info")) {
* event.reply("Click the buttons for more info")
* .addActionRow( // link buttons don't send events, they just open a link in the browser when clicked
* Button.link("https://github.com/DV8FromTheWorld/JDA", "GitHub")
* .withEmoji(Emoji.fromMarkdown("<:github:849286315580719104>")), // Link Button with label and emoji
* Button.link("https://ci.dv8tion.net/job/JDA/javadoc/", "Javadocs")) // Link Button with only a label
* .queue();
* }
* }
*
* public void onButtonClick(ButtonClickEvent event) {
* if (event.getComponentId().equals("hello")) {
* event.reply("Hello :)").queue();
* }
* }
* }
* }</pre>
*
* To see what each button looks like here is an example cheatsheet:
* <br>
* <img alt="ButtonExample" src="https://raw.githubusercontent.com/DV8FromTheWorld/JDA/52377f69d1f3bfba909c51a449ac6b258f606956/assets/wiki/interactions/ButtonExamples.png">
*
* @see net.dv8tion.jda.api.requests.restaction.interactions.ReplyAction#addActionRow(Component...) ReplyAction.addActionRow(Component...)
* @see net.dv8tion.jda.api.requests.restaction.interactions.ReplyAction#addActionRows(ActionRow...) ReplyAction.addActionRows(ActionRow...)
*/
public interface Button extends Component
{
/**
* The visible text on the button.
*
* @return The button label
*/
@Nonnull
String getLabel();
/**
* The style of this button.
*
* @return {@link ButtonStyle}
*/
@Nonnull
ButtonStyle getStyle();
/**
* The target URL for this button, if it is a {@link ButtonStyle#LINK LINK}-Style Button.
*
* @return The target URL or null
*/
@Nullable
String getUrl();
/**
* The emoji attached to this button.
* <br>This can be either {@link Emoji#isUnicode() unicode} or {@link Emoji#isCustom()} custom.
*
* <p>You can use {@link #withEmoji(Emoji)} to create a button with an Emoji.
*
* @return {@link Emoji} for this button
*/
@Nullable
Emoji getEmoji();
/**
* Whether this button is disabled.
*
* <p>You can use {@link #asDisabled()} or {@link #asEnabled()} to create enabled/disabled instances.
*
* @return True, if this button is disabled
*/
boolean isDisabled();
/**
* Returns a copy of this button with {@link #isDisabled()} set to true.
*
* @return New disabled button instance
*/
@Nonnull
default Button asDisabled()
{
return new ButtonImpl(getId(), getLabel(), getStyle(), getUrl(), true, getEmoji());
}
/**
* Returns a copy of this button with {@link #isDisabled()} set to false.
*
* @return New enabled button instance
*/
@Nonnull
default Button asEnabled()
{
return new ButtonImpl(getId(), getLabel(), getStyle(), getUrl(), false, getEmoji());
}
/**
* Returns a copy of this button with {@link #isDisabled()} set to the provided value.
*
* @param disabled
* True, if this button should be disabled
*
* @return New enabled/disabled button instance
*/
@Nonnull
default Button withDisabled(boolean disabled)
{
return new ButtonImpl(getId(), getLabel(), getStyle(), getUrl(), disabled, getEmoji());
}
/**
* Returns a copy of this button with the attached Emoji.
*
* @param emoji
* The emoji to use
*
* @return New button with emoji
*/
@Nonnull
default Button withEmoji(@Nullable Emoji emoji)
{
return new ButtonImpl(getId(), getLabel(), getStyle(), getUrl(), isDisabled(), emoji);
}
/**
* Returns a copy of this button with the provided label.
*
* @param label
* The label to use
*
* @throws IllegalArgumentException
* If the label is not between 1-80 characters
*
* @return New button with the changed label
*/
@Nonnull
default Button withLabel(@Nonnull String label)
{
Checks.notEmpty(label, "Label");
Checks.notLonger(label, 80, "Label");
return new ButtonImpl(getId(), label, getStyle(), getUrl(), isDisabled(), getEmoji());
}
/**
* Returns a copy of this button with the provided id.
*
* @param id
* The id to use
*
* @throws IllegalArgumentException
* If the id is not between 1-100 characters
*
* @return New button with the changed id
*/
@Nonnull
default Button withId(@Nonnull String id)
{
Checks.notEmpty(id, "ID");
Checks.notLonger(id, 100, "ID");
return new ButtonImpl(id, getLabel(), getStyle(), null, isDisabled(), getEmoji());
}
/**
* Returns a copy of this button with the provided url.
*
* @param url
* The url to use
*
* @throws IllegalArgumentException
* If the url is null, empty, or longer than 512 characters
*
* @return New button with the changed url
*/
@Nonnull
default Button withUrl(@Nonnull String url)
{
Checks.notEmpty(url, "URL");
Checks.notLonger(url, 512, "URL");
return new ButtonImpl(null, getLabel(), ButtonStyle.LINK, url, isDisabled(), getEmoji());
}
/**
* Returns a copy of this button with the provided style.
*
* <p>You cannot use this convert link buttons.
*
* @param style
* The style to use
*
* @throws IllegalArgumentException
* If the style is null or tries to change whether this button is a LINK button
*
* @return New button with the changed style
*/
@Nonnull
default Button withStyle(@Nonnull ButtonStyle style)
{
Checks.notNull(style, "Style");
Checks.check(style != ButtonStyle.UNKNOWN, "Cannot make button with unknown style!");
if (getStyle() == ButtonStyle.LINK && style != ButtonStyle.LINK)
throw new IllegalArgumentException("You cannot change a link button to another style!");
if (getStyle() != ButtonStyle.LINK && style == ButtonStyle.LINK)
throw new IllegalArgumentException("You cannot change a styled button to a link button!");
return new ButtonImpl(getId(), getLabel(), style, getUrl(), isDisabled(), getEmoji());
}
/**
* Creates a button with {@link ButtonStyle#PRIMARY PRIMARY} Style.
* <br>The button is enabled and has no emoji attached by default.
* You can use {@link #asDisabled()} and {@link #withEmoji(Emoji)} to further configure it.
*
* @param id
* The custom button ID
* @param label
* The text to display on the button
*
* @throws IllegalArgumentException
* If any argument is empty or null, the label is longer than 80 characters, or the id is longer than 100 characters
*
* @return The button instance
*/
@Nonnull
static Button primary(@Nonnull String id, @Nonnull String label)
{
Checks.notEmpty(id, "Id");
Checks.notEmpty(label, "Label");
Checks.notLonger(id, 100, "Id");
Checks.notLonger(label, 80, "Label");
return new ButtonImpl(id, label, ButtonStyle.PRIMARY, false, null);
}
/**
* Creates a button with {@link ButtonStyle#PRIMARY PRIMARY} Style.
* <br>The button is enabled and has no text label.
* To use labels you can use {@code primary(id, label).withEmoji(emoji)}
*
* <p>To disable the button you can use {@link #asDisabled()}.
*
* @param id
* The custom button ID
* @param emoji
* The emoji to use as the button label
*
* @throws IllegalArgumentException
* If any argument is empty or null or the id is longer than 100 characters
*
* @return The button instance
*/
@Nonnull
static Button primary(@Nonnull String id, @Nonnull Emoji emoji)
{
Checks.notEmpty(id, "Id");
Checks.notNull(emoji, "Emoji");
Checks.notLonger(id, 100, "Id");
return new ButtonImpl(id, "", ButtonStyle.PRIMARY, false, emoji);
}
/**
* Creates a button with {@link ButtonStyle#SECONDARY SECONDARY} Style.
* <br>The button is enabled and has no emoji attached by default.
* You can use {@link #asDisabled()} and {@link #withEmoji(Emoji)} to further configure it.
*
* @param id
* The custom button ID
* @param label
* The text to display on the button
*
* @throws IllegalArgumentException
* If any argument is empty or null, the label is longer than 80 characters, or the id is longer than 100 characters
*
* @return The button instance
*/
@Nonnull
static Button secondary(@Nonnull String id, @Nonnull String label)
{
Checks.notEmpty(id, "Id");
Checks.notEmpty(label, "Label");
Checks.notLonger(id, 100, "Id");
Checks.notLonger(label, 80, "Label");
return new ButtonImpl(id, label, ButtonStyle.SECONDARY, false, null);
}
/**
* Creates a button with {@link ButtonStyle#SECONDARY SECONDARY} Style.
* <br>The button is enabled and has no text label.
* To use labels you can use {@code secondary(id, label).withEmoji(emoji)}
*
* <p>To disable the button you can use {@link #asDisabled()}.
*
* @param id
* The custom button ID
* @param emoji
* The emoji to use as the button label
*
* @throws IllegalArgumentException
* If any argument is empty or null or the id is longer than 100 characters
*
* @return The button instance
*/
@Nonnull
static Button secondary(@Nonnull String id, @Nonnull Emoji emoji)
{
Checks.notEmpty(id, "Id");
Checks.notNull(emoji, "Emoji");
Checks.notLonger(id, 100, "Id");
return new ButtonImpl(id, "", ButtonStyle.SECONDARY, false, emoji);
}
/**
* Creates a button with {@link ButtonStyle#SUCCESS SUCCESS} Style.
* <br>The button is enabled and has no emoji attached by default.
* You can use {@link #asDisabled()} and {@link #withEmoji(Emoji)} to further configure it.
*
* @param id
* The custom button ID
* @param label
* The text to display on the button
*
* @throws IllegalArgumentException
* If any argument is empty or null, the label is longer than 80 characters, or the id is longer than 100 characters
*
* @return The button instance
*/
@Nonnull
static Button success(@Nonnull String id, @Nonnull String label)
{
Checks.notEmpty(id, "Id");
Checks.notEmpty(label, "Label");
Checks.notLonger(id, 100, "Id");
Checks.notLonger(label, 80, "Label");
return new ButtonImpl(id, label, ButtonStyle.SUCCESS, false, null);
}
/**
* Creates a button with {@link ButtonStyle#SUCCESS SUCCESS} Style.
* <br>The button is enabled and has no text label.
* To use labels you can use {@code success(id, label).withEmoji(emoji)}
*
* <p>To disable the button you can use {@link #asDisabled()}.
*
* @param id
* The custom button ID
* @param emoji
* The emoji to use as the button label
*
* @throws IllegalArgumentException
* If any argument is empty or null or the id is longer than 100 characters
*
* @return The button instance
*/
@Nonnull
static Button success(@Nonnull String id, @Nonnull Emoji emoji)
{
Checks.notEmpty(id, "Id");
Checks.notNull(emoji, "Emoji");
Checks.notLonger(id, 100, "Id");
return new ButtonImpl(id, "", ButtonStyle.SUCCESS, false, emoji);
}
/**
* Creates a button with {@link ButtonStyle#DANGER DANGER} Style.
* <br>The button is enabled and has no emoji attached by default.
* You can use {@link #asDisabled()} and {@link #withEmoji(Emoji)} to further configure it.
*
* @param id
* The custom button ID
* @param label
* The text to display on the button
*
* @throws IllegalArgumentException
* If any argument is empty or null, the label is longer than 80 characters, or the id is longer than 100 characters
*
* @return The button instance
*/
@Nonnull
static Button danger(@Nonnull String id, @Nonnull String label)
{
Checks.notEmpty(id, "Id");
Checks.notEmpty(label, "Label");
Checks.notLonger(id, 100, "Id");
Checks.notLonger(label, 80, "Label");
return new ButtonImpl(id, label, ButtonStyle.DANGER, false, null);
}
/**
* Creates a button with {@link ButtonStyle#DANGER DANGER} Style.
* <br>The button is enabled and has no text label.
* To use labels you can use {@code danger(id, label).withEmoji(emoji)}
*
* <p>To disable the button you can use {@link #asDisabled()}.
*
* @param id
* The custom button ID
* @param emoji
* The emoji to use as the button label
*
* @throws IllegalArgumentException
* If any argument is empty or null or the id is longer than 100 characters
*
* @return The button instance
*/
@Nonnull
static Button danger(@Nonnull String id, @Nonnull Emoji emoji)
{
Checks.notEmpty(id, "Id");
Checks.notNull(emoji, "Emoji");
Checks.notLonger(id, 100, "Id");
return new ButtonImpl(id, "", ButtonStyle.DANGER, false, emoji);
}
/**
* Creates a button with {@link ButtonStyle#LINK LINK} Style.
* <br>The button is enabled and has no emoji attached by default.
* You can use {@link #asDisabled()} and {@link #withEmoji(Emoji)} to further configure it.
*
* <p>Note that link buttons never send a {@link net.dv8tion.jda.api.events.interaction.ButtonClickEvent ButtonClickEvent}.
* These buttons only open a link for the user.
*
* @param url
* The target URL for this button
* @param label
* The text to display on the button
*
* @throws IllegalArgumentException
* If any argument is empty or null, the label is longer than 80 characters, or the url is longer than 512 characters
*
* @return The button instance
*/
@Nonnull
static Button link(@Nonnull String url, @Nonnull String label)
{
Checks.notEmpty(url, "URL");
Checks.notEmpty(label, "Label");
Checks.notLonger(url, 512, "URL");
Checks.notLonger(label, 80, "Label");
return new ButtonImpl(null, label, ButtonStyle.LINK, url, false, null);
}
/**
* Creates a button with {@link ButtonStyle#LINK LINK} Style.
* <br>The button is enabled and has no text label.
* To use labels you can use {@code link(url, label).withEmoji(emoji)}
*
* <p>To disable the button you can use {@link #asDisabled()}.
*
* <p>Note that link buttons never send a {@link net.dv8tion.jda.api.events.interaction.ButtonClickEvent ButtonClickEvent}.
* These buttons only open a link for the user.
*
* @param url
* The target URL for this button
* @param emoji
* The emoji to use as the button label
*
* @throws IllegalArgumentException
* If any argument is empty or null or the url is longer than 512 characters
*
* @return The button instance
*/
@Nonnull
static Button link(@Nonnull String url, @Nonnull Emoji emoji)
{
Checks.notEmpty(url, "URL");
Checks.notNull(emoji, "Emoji");
Checks.notLonger(url, 512, "URL");
return new ButtonImpl(null, "", ButtonStyle.LINK, url, false, emoji);
}
/**
* Create a button with the provided {@link ButtonStyle style}, URL or ID, and label.
* <br>The button is enabled and has no emoji attached by default.
* You can use {@link #asDisabled()} and {@link #withEmoji(Emoji)} to further configure it.
*
* <p>See {@link #link(String, String)} or {@link #primary(String, String)} for more details.
*
* @param style
* The button style
* @param idOrUrl
* Either the ID or URL for this button
* @param label
* The text to display on the button
*
* @throws IllegalArgumentException
* If any argument is empty or null, the label is longer than 80 characters, the id is longer than 100 characters, or the url is longer than 512 characters
*
* @return The button instance
*/
@Nonnull
static Button of(@Nonnull ButtonStyle style, @Nonnull String idOrUrl, @Nonnull String label)
{
Checks.check(style != ButtonStyle.UNKNOWN, "Cannot make button with unknown style!");
Checks.notNull(style, "Style");
Checks.notNull(label, "Label");
Checks.notLonger(label, 80, "Label");
if (style == ButtonStyle.LINK)
return link(idOrUrl, label);
Checks.notEmpty(idOrUrl, "Id");
Checks.notLonger(idOrUrl, 100, "Id");
return new ButtonImpl(idOrUrl, label, style, false, null);
}
/**
* Create a button with the provided {@link ButtonStyle style}, URL or ID, and emoji.
* <br>The button is enabled and has no text label.
* To use labels you can use {@code of(style, idOrUrl, label).withEmoji(emoji)}
*
* <p>See {@link #link(String, Emoji)} or {@link #primary(String, Emoji)} for more details.
*
* @param style
* The button style
* @param idOrUrl
* Either the ID or URL for this button
* @param emoji
* The emoji to use as the button label
*
* @throws IllegalArgumentException
* If any argument is empty or null, the id is longer than 100 characters, or the url is longer than 512 characters
*
* @return The button instance
*/
@Nonnull
static Button of(@Nonnull ButtonStyle style, @Nonnull String idOrUrl, @Nonnull Emoji emoji)
{
Checks.check(style != ButtonStyle.UNKNOWN, "Cannot make button with unknown style!");
Checks.notNull(style, "Style");
Checks.notNull(emoji, "Emoji");
if (style == ButtonStyle.LINK)
return link(idOrUrl, emoji);
Checks.notEmpty(idOrUrl, "Id");
Checks.notLonger(idOrUrl, 100, "Id");
return new ButtonImpl(idOrUrl, "", style, false, emoji);
}
}