-
-
Notifications
You must be signed in to change notification settings - Fork 719
/
EmbedBuilder.java
804 lines (769 loc) · 31.7 KB
/
EmbedBuilder.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
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
/*
* 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;
import net.dv8tion.jda.api.entities.EmbedType;
import net.dv8tion.jda.api.entities.MessageEmbed;
import net.dv8tion.jda.api.entities.Role;
import net.dv8tion.jda.internal.entities.EntityBuilder;
import net.dv8tion.jda.internal.utils.Checks;
import net.dv8tion.jda.internal.utils.Helpers;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import java.awt.*;
import java.time.*;
import java.time.temporal.TemporalAccessor;
import java.util.LinkedList;
import java.util.List;
import java.util.regex.Pattern;
/**
* Builder system used to build {@link net.dv8tion.jda.api.entities.MessageEmbed MessageEmbeds}.
*
* <br>A visual breakdown of an Embed and how it relates to this class is available at
* <a href="https://raw.githubusercontent.com/DV8FromTheWorld/JDA/assets/assets/docs/embeds/01-Overview.png" target="_blank">Embed Overview</a>.
*
* @since 3.0
* @author John A. Grosh
*/
public class EmbedBuilder
{
public final static String ZERO_WIDTH_SPACE = "\u200E";
public final static Pattern URL_PATTERN = Pattern.compile("\\s*(https?|attachment)://\\S+\\s*", Pattern.CASE_INSENSITIVE);
private final List<MessageEmbed.Field> fields = new LinkedList<>();
private final StringBuilder description = new StringBuilder();
private int color = Role.DEFAULT_COLOR_RAW;
private String url, title;
private OffsetDateTime timestamp;
private MessageEmbed.Thumbnail thumbnail;
private MessageEmbed.AuthorInfo author;
private MessageEmbed.Footer footer;
private MessageEmbed.ImageInfo image;
/**
* Constructs a new EmbedBuilder instance, which can be used to create {@link net.dv8tion.jda.api.entities.MessageEmbed MessageEmbeds}.
* These can then be sent to a channel using {@link net.dv8tion.jda.api.entities.MessageChannel#sendMessageEmbeds(MessageEmbed, MessageEmbed...)}.
* <br>Every part of an embed can be removed or cleared by providing {@code null} to the setter method.
*/
public EmbedBuilder() { }
public EmbedBuilder(@Nullable EmbedBuilder builder)
{
if (builder != null)
{
setDescription(builder.description.toString());
this.fields.addAll(builder.fields);
this.url = builder.url;
this.title = builder.title;
this.timestamp = builder.timestamp;
this.color = builder.color;
this.thumbnail = builder.thumbnail;
this.author = builder.author;
this.footer = builder.footer;
this.image = builder.image;
}
}
/**
* Creates an EmbedBuilder using fields in an existing embed.
*
* @param embed
* the existing embed
*/
public EmbedBuilder(@Nullable MessageEmbed embed)
{
if(embed != null)
{
setDescription(embed.getDescription());
this.url = embed.getUrl();
this.title = embed.getTitle();
this.timestamp = embed.getTimestamp();
this.color = embed.getColorRaw();
this.thumbnail = embed.getThumbnail();
this.author = embed.getAuthor();
this.footer = embed.getFooter();
this.image = embed.getImage();
if (embed.getFields() != null)
fields.addAll(embed.getFields());
}
}
/**
* Returns a {@link net.dv8tion.jda.api.entities.MessageEmbed MessageEmbed}
* that has been checked as being valid for sending.
*
* @throws java.lang.IllegalStateException
* If the embed is empty. Can be checked with {@link #isEmpty()}.
*
* @return the built, sendable {@link net.dv8tion.jda.api.entities.MessageEmbed}
*/
@Nonnull
public MessageEmbed build()
{
if (isEmpty())
throw new IllegalStateException("Cannot build an empty embed!");
if (description.length() > MessageEmbed.DESCRIPTION_MAX_LENGTH)
throw new IllegalStateException(Helpers.format("Description is longer than %d! Please limit your input!", MessageEmbed.DESCRIPTION_MAX_LENGTH));
if (length() > MessageEmbed.EMBED_MAX_LENGTH_BOT)
throw new IllegalStateException("Cannot build an embed with more than " + MessageEmbed.EMBED_MAX_LENGTH_BOT + " characters!");
final String description = this.description.length() < 1 ? null : this.description.toString();
return EntityBuilder.createMessageEmbed(url, title, description, EmbedType.RICH, timestamp,
color, thumbnail, null, author, null, footer, image, new LinkedList<>(fields));
}
/**
* Resets this builder to default state.
* <br>All parts will be either empty or null after this method has returned.
*
* @return The current EmbedBuilder with default values
*/
@Nonnull
public EmbedBuilder clear()
{
description.setLength(0);
fields.clear();
url = null;
title = null;
timestamp = null;
color = Role.DEFAULT_COLOR_RAW;
thumbnail = null;
author = null;
footer = null;
image = null;
return this;
}
/**
* Checks if the given embed is empty. Empty embeds will throw an exception if built
*
* @return true if the embed is empty and cannot be built
*/
public boolean isEmpty()
{
return title == null
&& timestamp == null
&& thumbnail == null
&& author == null
&& footer == null
&& image == null
&& color == Role.DEFAULT_COLOR_RAW
&& description.length() == 0
&& fields.isEmpty();
}
/**
* The overall length of the current EmbedBuilder in displayed characters.
* <br>Represents the {@link net.dv8tion.jda.api.entities.MessageEmbed#getLength() MessageEmbed.getLength()} value.
*
* @return length of the current builder state
*/
public int length()
{
int length = description.length();
synchronized (fields)
{
length = fields.stream().map(f -> f.getName().length() + f.getValue().length()).reduce(length, Integer::sum);
}
if (title != null)
length += title.length();
if (author != null)
length += author.getName().length();
if (footer != null)
length += footer.getText().length();
return length;
}
/**
* Checks whether the constructed {@link net.dv8tion.jda.api.entities.MessageEmbed MessageEmbed}
* is within the limits for a bot account.
*
* @return True, if the {@link #length() length} is less or equal to the specific limit
*
* @see MessageEmbed#EMBED_MAX_LENGTH_BOT
*/
public boolean isValidLength()
{
final int length = length();
return length <= MessageEmbed.EMBED_MAX_LENGTH_BOT;
}
/**
* Sets the Title of the embed.
* <br>Overload for {@link #setTitle(String, String)} without URL parameter.
*
* <p><b><a href="https://raw.githubusercontent.com/DV8FromTheWorld/JDA/assets/assets/docs/embeds/04-setTitle.png">Example</a></b>
*
* @param title
* the title of the embed
*
* @throws java.lang.IllegalArgumentException
* <ul>
* <li>If the provided {@code title} is an empty String.</li>
* <li>If the length of {@code title} is greater than {@link net.dv8tion.jda.api.entities.MessageEmbed#TITLE_MAX_LENGTH}.</li>
* </ul>
*
* @return the builder after the title has been set
*/
@Nonnull
public EmbedBuilder setTitle(@Nullable String title)
{
return setTitle(title, null);
}
/**
* Sets the Title of the embed.
* <br>You can provide {@code null} as url if no url should be used.
*
* <p><b><a href="https://raw.githubusercontent.com/DV8FromTheWorld/JDA/assets/assets/docs/embeds/04-setTitle.png">Example</a></b>
*
* @param title
* the title of the embed
* @param url
* Makes the title into a hyperlink pointed at this url.
*
* @throws java.lang.IllegalArgumentException
* <ul>
* <li>If the provided {@code title} is an empty String.</li>
* <li>If the length of {@code title} is greater than {@link net.dv8tion.jda.api.entities.MessageEmbed#TITLE_MAX_LENGTH}.</li>
* <li>If the length of {@code url} is longer than {@link net.dv8tion.jda.api.entities.MessageEmbed#URL_MAX_LENGTH}.</li>
* <li>If the provided {@code url} is not a properly formatted http or https url.</li>
* </ul>
*
* @return the builder after the title has been set
*/
@Nonnull
public EmbedBuilder setTitle(@Nullable String title, @Nullable String url)
{
if (title == null)
{
this.title = null;
this.url = null;
}
else
{
Checks.notEmpty(title, "Title");
Checks.check(title.length() <= MessageEmbed.TITLE_MAX_LENGTH, "Title cannot be longer than %d characters.", MessageEmbed.TITLE_MAX_LENGTH);
if (Helpers.isBlank(url))
url = null;
urlCheck(url);
this.title = title;
this.url = url;
}
return this;
}
/**
* The {@link java.lang.StringBuilder StringBuilder} used to
* build the description for the embed.
* <br>Note: To reset the description use {@link #setDescription(CharSequence) setDescription(null)}
*
* @return StringBuilder with current description context
*/
@Nonnull
public StringBuilder getDescriptionBuilder()
{
return description;
}
/**
* Sets the Description of the embed. This is where the main chunk of text for an embed is typically placed.
*
* <p><b><a href="https://raw.githubusercontent.com/DV8FromTheWorld/JDA/assets/assets/docs/embeds/05-setDescription.png">Example</a></b>
*
* @param description
* the description of the embed, {@code null} to reset
*
* @throws java.lang.IllegalArgumentException
* If the length of {@code description} is greater than {@link net.dv8tion.jda.api.entities.MessageEmbed#DESCRIPTION_MAX_LENGTH}
*
* @return the builder after the description has been set
*/
@Nonnull
public final EmbedBuilder setDescription(@Nullable CharSequence description)
{
this.description.setLength(0);
if (description != null && description.length() >= 1)
appendDescription(description);
return this;
}
/**
* Appends to the description of the embed. This is where the main chunk of text for an embed is typically placed.
*
* <p><b><a href="https://raw.githubusercontent.com/DV8FromTheWorld/JDA/assets/assets/docs/embeds/05-setDescription.png">Example</a></b>
*
* @param description
* the string to append to the description of the embed
*
* @throws java.lang.IllegalArgumentException
* <ul>
* <li>If the provided {@code description} String is null</li>
* <li>If the length of {@code description} is greater than {@link net.dv8tion.jda.api.entities.MessageEmbed#DESCRIPTION_MAX_LENGTH}.</li>
* </ul>
*
* @return the builder after the description has been set
*/
@Nonnull
public EmbedBuilder appendDescription(@Nonnull CharSequence description)
{
Checks.notNull(description, "description");
Checks.check(this.description.length() + description.length() <= MessageEmbed.DESCRIPTION_MAX_LENGTH,
"Description cannot be longer than %d characters.", MessageEmbed.DESCRIPTION_MAX_LENGTH);
this.description.append(description);
return this;
}
/**
* Sets the Timestamp of the embed.
*
* <p><b><a href="https://raw.githubusercontent.com/DV8FromTheWorld/JDA/assets/assets/docs/embeds/13-setTimestamp.png">Example</a></b>
*
* <p><b>Hint:</b> You can get the current time using {@link java.time.Instant#now() Instant.now()} or convert time from a
* millisecond representation by using {@link java.time.Instant#ofEpochMilli(long) Instant.ofEpochMilli(long)};
*
* @param temporal
* the temporal accessor of the timestamp
*
* @return the builder after the timestamp has been set
*/
@Nonnull
public EmbedBuilder setTimestamp(@Nullable TemporalAccessor temporal)
{
if (temporal == null)
{
this.timestamp = null;
}
else if (temporal instanceof OffsetDateTime)
{
this.timestamp = (OffsetDateTime) temporal;
}
else
{
ZoneOffset offset;
try
{
offset = ZoneOffset.from(temporal);
}
catch (DateTimeException ignore)
{
offset = ZoneOffset.UTC;
}
try
{
LocalDateTime ldt = LocalDateTime.from(temporal);
this.timestamp = OffsetDateTime.of(ldt, offset);
}
catch (DateTimeException ignore)
{
try
{
Instant instant = Instant.from(temporal);
this.timestamp = OffsetDateTime.ofInstant(instant, offset);
}
catch (DateTimeException ex)
{
throw new DateTimeException("Unable to obtain OffsetDateTime from TemporalAccessor: " +
temporal + " of type " + temporal.getClass().getName(), ex);
}
}
}
return this;
}
/**
* Sets the Color of the embed.
*
* <a href="https://raw.githubusercontent.com/DV8FromTheWorld/JDA/assets/assets/docs/embeds/02-setColor.png" target="_blank">Example</a>
*
* @param color
* The {@link java.awt.Color Color} of the embed
* or {@code null} to use no color
*
* @return the builder after the color has been set
*
* @see #setColor(int)
*/
@Nonnull
public EmbedBuilder setColor(@Nullable Color color)
{
this.color = color == null ? Role.DEFAULT_COLOR_RAW : color.getRGB();
return this;
}
/**
* Sets the raw RGB color value for the embed.
*
* <a href="https://raw.githubusercontent.com/DV8FromTheWorld/JDA/assets/assets/docs/embeds/02-setColor.png" target="_blank">Example</a>
*
* @param color
* The raw rgb value, or {@link Role#DEFAULT_COLOR_RAW} to use no color
*
* @return the builder after the color has been set
*
* @see #setColor(java.awt.Color)
*/
@Nonnull
public EmbedBuilder setColor(int color)
{
this.color = color;
return this;
}
/**
* Sets the Thumbnail of the embed.
*
* <p><b><a href="https://raw.githubusercontent.com/DV8FromTheWorld/JDA/assets/assets/docs/embeds/06-setThumbnail.png">Example</a></b>
*
* <p><b>Uploading images with Embeds</b>
* <br>When uploading an <u>image</u>
* (using {@link net.dv8tion.jda.api.entities.MessageChannel#sendFile(java.io.File, net.dv8tion.jda.api.utils.AttachmentOption...) MessageChannel.sendFile(...)})
* you can reference said image using the specified filename as URI {@code attachment://filename.ext}.
*
* <p><u>Example</u>
* <pre><code>
* MessageChannel channel; // = reference of a MessageChannel
* EmbedBuilder embed = new EmbedBuilder();
* InputStream file = new URL("https://http.cat/500").openStream();
* embed.setThumbnail("attachment://cat.png") // we specify this in sendFile as "cat.png"
* .setDescription("This is a cute cat :3");
* channel.sendFile(file, "cat.png").setEmbeds(embed.build()).queue();
* </code></pre>
*
* @param url
* the url of the thumbnail of the embed
*
* @throws java.lang.IllegalArgumentException
* <ul>
* <li>If the length of {@code url} is longer than {@link net.dv8tion.jda.api.entities.MessageEmbed#URL_MAX_LENGTH}.</li>
* <li>If the provided {@code url} is not a properly formatted http or https url.</li>
* </ul>
*
* @return the builder after the thumbnail has been set
*/
@Nonnull
public EmbedBuilder setThumbnail(@Nullable String url)
{
if (url == null)
{
this.thumbnail = null;
}
else
{
urlCheck(url);
this.thumbnail = new MessageEmbed.Thumbnail(url, null, 0, 0);
}
return this;
}
/**
* Sets the Image of the embed.
*
* <p><b><a href="https://raw.githubusercontent.com/DV8FromTheWorld/JDA/assets/assets/docs/embeds/11-setImage.png">Example</a></b>
*
* <p><b>Uploading images with Embeds</b>
* <br>When uploading an <u>image</u>
* (using {@link net.dv8tion.jda.api.entities.MessageChannel#sendFile(java.io.File, net.dv8tion.jda.api.utils.AttachmentOption...) MessageChannel.sendFile(...)})
* you can reference said image using the specified filename as URI {@code attachment://filename.ext}.
*
* <p><u>Example</u>
* <pre><code>
* MessageChannel channel; // = reference of a MessageChannel
* EmbedBuilder embed = new EmbedBuilder();
* InputStream file = new URL("https://http.cat/500").openStream();
* embed.setImage("attachment://cat.png") // we specify this in sendFile as "cat.png"
* .setDescription("This is a cute cat :3");
* channel.sendFile(file, "cat.png").setEmbeds(embed.build()).queue();
* </code></pre>
*
* @param url
* the url of the image of the embed
*
* @throws java.lang.IllegalArgumentException
* <ul>
* <li>If the length of {@code url} is longer than {@link net.dv8tion.jda.api.entities.MessageEmbed#URL_MAX_LENGTH}.</li>
* <li>If the provided {@code url} is not a properly formatted http or https url.</li>
* </ul>
*
* @return the builder after the image has been set
*
* @see net.dv8tion.jda.api.entities.MessageChannel#sendFile(java.io.File, String, net.dv8tion.jda.api.utils.AttachmentOption...) MessageChannel.sendFile(...)
*/
@Nonnull
public EmbedBuilder setImage(@Nullable String url)
{
if (url == null)
{
this.image = null;
}
else
{
urlCheck(url);
this.image = new MessageEmbed.ImageInfo(url, null, 0, 0);
}
return this;
}
/**
* Sets the Author of the embed. The author appears in the top left of the embed and can have a small
* image beside it along with the author's name being made clickable by way of providing a url.
* This convenience method just sets the name.
*
* <p><b><a href="https://raw.githubusercontent.com/DV8FromTheWorld/JDA/assets/assets/docs/embeds/03-setAuthor.png">Example</a></b>
*
* @param name
* the name of the author of the embed. If this is not set, the author will not appear in the embed
*
* @throws java.lang.IllegalArgumentException
* If the length of {@code name} is longer than {@link net.dv8tion.jda.api.entities.MessageEmbed#AUTHOR_MAX_LENGTH}.
*
* @return the builder after the author has been set
*/
@Nonnull
public EmbedBuilder setAuthor(@Nullable String name)
{
return setAuthor(name, null, null);
}
/**
* Sets the Author of the embed. The author appears in the top left of the embed and can have a small
* image beside it along with the author's name being made clickable by way of providing a url.
* This convenience method just sets the name and the url.
*
* <p><b><a href="https://raw.githubusercontent.com/DV8FromTheWorld/JDA/assets/assets/docs/embeds/03-setAuthor.png">Example</a></b>
*
* @param name
* the name of the author of the embed. If this is not set, the author will not appear in the embed
* @param url
* the url of the author of the embed
*
* @throws java.lang.IllegalArgumentException
* <ul>
* <li>If the length of {@code name} is longer than {@link net.dv8tion.jda.api.entities.MessageEmbed#AUTHOR_MAX_LENGTH}.</li>
* <li>If the length of {@code url} is longer than {@link net.dv8tion.jda.api.entities.MessageEmbed#URL_MAX_LENGTH}.</li>
* <li>If the provided {@code url} is not a properly formatted http or https url.</li>
* </ul>
*
* @return the builder after the author has been set
*/
@Nonnull
public EmbedBuilder setAuthor(@Nullable String name, @Nullable String url)
{
return setAuthor(name, url, null);
}
/**
* Sets the Author of the embed. The author appears in the top left of the embed and can have a small
* image beside it along with the author's name being made clickable by way of providing a url.
*
* <p><b><a href="https://raw.githubusercontent.com/DV8FromTheWorld/JDA/assets/assets/docs/embeds/03-setAuthor.png">Example</a></b>
*
* <p><b>Uploading images with Embeds</b>
* <br>When uploading an <u>image</u>
* (using {@link net.dv8tion.jda.api.entities.MessageChannel#sendFile(java.io.File, net.dv8tion.jda.api.utils.AttachmentOption...) MessageChannel.sendFile(...)})
* you can reference said image using the specified filename as URI {@code attachment://filename.ext}.
*
* <p><u>Example</u>
* <pre><code>
* MessageChannel channel; // = reference of a MessageChannel
* EmbedBuilder embed = new EmbedBuilder();
* InputStream file = new URL("https://http.cat/500").openStream();
* embed.setAuthor("Minn", null, "attachment://cat.png") // we specify this in sendFile as "cat.png"
* .setDescription("This is a cute cat :3");
* channel.sendFile(file, "cat.png").setEmbeds(embed.build()).queue();
* </code></pre>
*
* @param name
* the name of the author of the embed. If this is not set, the author will not appear in the embed
* @param url
* the url of the author of the embed
* @param iconUrl
* the url of the icon for the author
*
* @throws java.lang.IllegalArgumentException
* <ul>
* <li>If the length of {@code name} is longer than {@link net.dv8tion.jda.api.entities.MessageEmbed#AUTHOR_MAX_LENGTH}.</li>
* <li>If the length of {@code url} is longer than {@link net.dv8tion.jda.api.entities.MessageEmbed#URL_MAX_LENGTH}.</li>
* <li>If the provided {@code url} is not a properly formatted http or https url.</li>
* <li>If the length of {@code iconUrl} is longer than {@link net.dv8tion.jda.api.entities.MessageEmbed#URL_MAX_LENGTH}.</li>
* <li>If the provided {@code iconUrl} is not a properly formatted http or https url.</li>
* </ul>
*
* @return the builder after the author has been set
*/
@Nonnull
public EmbedBuilder setAuthor(@Nullable String name, @Nullable String url, @Nullable String iconUrl)
{
//We only check if the name is null because its presence is what determines if the
// the author will appear in the embed.
if (name == null)
{
this.author = null;
}
else
{
Checks.check(name.length() <= MessageEmbed.AUTHOR_MAX_LENGTH, "Name cannot be longer than %d characters.", MessageEmbed.AUTHOR_MAX_LENGTH);
urlCheck(url);
urlCheck(iconUrl);
this.author = new MessageEmbed.AuthorInfo(name, url, iconUrl, null);
}
return this;
}
/**
* Sets the Footer of the embed without icon.
*
* <p><b><a href="https://raw.githubusercontent.com/DV8FromTheWorld/JDA/assets/assets/docs/embeds/12-setFooter.png">Example</a></b>
*
* @param text
* the text of the footer of the embed. If this is not set or set to null, the footer will not appear in the embed.
*
* @throws java.lang.IllegalArgumentException
* If the length of {@code text} is longer than {@link net.dv8tion.jda.api.entities.MessageEmbed#TEXT_MAX_LENGTH}.
*
* @return the builder after the footer has been set
*/
@Nonnull
public EmbedBuilder setFooter(@Nullable String text)
{
return setFooter(text, null);
}
/**
* Sets the Footer of the embed.
*
* <p><b><a href="https://raw.githubusercontent.com/DV8FromTheWorld/JDA/assets/assets/docs/embeds/12-setFooter.png">Example</a></b>
*
* <p><b>Uploading images with Embeds</b>
* <br>When uploading an <u>image</u>
* (using {@link net.dv8tion.jda.api.entities.MessageChannel#sendFile(java.io.File, net.dv8tion.jda.api.utils.AttachmentOption...) MessageChannel.sendFile(...)})
* you can reference said image using the specified filename as URI {@code attachment://filename.ext}.
*
* <p><u>Example</u>
* <pre><code>
* MessageChannel channel; // = reference of a MessageChannel
* EmbedBuilder embed = new EmbedBuilder();
* InputStream file = new URL("https://http.cat/500").openStream();
* embed.setFooter("Cool footer!", "attachment://cat.png") // we specify this in sendFile as "cat.png"
* .setDescription("This is a cute cat :3");
* channel.sendFile(file, "cat.png").setEmbeds(embed.build()).queue();
* </code></pre>
*
* @param text
* the text of the footer of the embed. If this is not set, the footer will not appear in the embed.
* @param iconUrl
* the url of the icon for the footer
*
* @throws java.lang.IllegalArgumentException
* <ul>
* <li>If the length of {@code text} is longer than {@link net.dv8tion.jda.api.entities.MessageEmbed#TEXT_MAX_LENGTH}.</li>
* <li>If the length of {@code iconUrl} is longer than {@link net.dv8tion.jda.api.entities.MessageEmbed#URL_MAX_LENGTH}.</li>
* <li>If the provided {@code iconUrl} is not a properly formatted http or https url.</li>
* </ul>
*
* @return the builder after the footer has been set
*/
@Nonnull
public EmbedBuilder setFooter(@Nullable String text, @Nullable String iconUrl)
{
//We only check if the text is null because its presence is what determines if the
// footer will appear in the embed.
if (text == null)
{
this.footer = null;
}
else
{
Checks.check(text.length() <= MessageEmbed.TEXT_MAX_LENGTH, "Text cannot be longer than %d characters.", MessageEmbed.TEXT_MAX_LENGTH);
urlCheck(iconUrl);
this.footer = new MessageEmbed.Footer(text, iconUrl, null);
}
return this;
}
/**
* Copies the provided Field into a new Field for this builder.
* <br>For additional documentation, see {@link #addField(String, String, boolean)}
*
* @param field
* the field object to add
*
* @return the builder after the field has been added
*/
@Nonnull
public EmbedBuilder addField(@Nullable MessageEmbed.Field field)
{
return field == null ? this : addField(field.getName(), field.getValue(), field.isInline());
}
/**
* Adds a Field to the embed.
*
* <p>Note: If a blank string is provided to either {@code name} or {@code value}, the blank string is replaced
* with {@link net.dv8tion.jda.api.EmbedBuilder#ZERO_WIDTH_SPACE}.
*
* <p><b><a href="https://raw.githubusercontent.com/DV8FromTheWorld/JDA/assets/assets/docs/embeds/07-addField.png">Example of Inline</a></b>
* <p><b><a href="https://raw.githubusercontent.com/DV8FromTheWorld/JDA/assets/assets/docs/embeds/08-addField.png">Example if Non-inline</a></b>
*
* @param name
* the name of the Field, displayed in bold above the {@code value}.
* @param value
* the contents of the field.
* @param inline
* whether or not this field should display inline.
*
* @throws java.lang.IllegalArgumentException
* <ul>
* <li>If only {@code name} or {@code value} is set. Both must be set.</li>
* <li>If the length of {@code name} is greater than {@link net.dv8tion.jda.api.entities.MessageEmbed#TITLE_MAX_LENGTH}.</li>
* <li>If the length of {@code value} is greater than {@link net.dv8tion.jda.api.entities.MessageEmbed#VALUE_MAX_LENGTH}.</li>
* </ul>
*
* @return the builder after the field has been added
*/
@Nonnull
public EmbedBuilder addField(@Nullable String name, @Nullable String value, boolean inline)
{
if (name == null && value == null)
return this;
this.fields.add(new MessageEmbed.Field(name, value, inline));
return this;
}
/**
* Adds a blank (empty) Field to the embed.
*
* <p><b><a href="https://raw.githubusercontent.com/DV8FromTheWorld/JDA/assets/assets/docs/embeds/07-addField.png">Example of Inline</a></b>
* <p><b><a href="https://raw.githubusercontent.com/DV8FromTheWorld/JDA/assets/assets/docs/embeds/08-addField.png">Example if Non-inline</a></b>
*
* @param inline
* whether or not this field should display inline
*
* @return the builder after the field has been added
*/
@Nonnull
public EmbedBuilder addBlankField(boolean inline)
{
this.fields.add(new MessageEmbed.Field(ZERO_WIDTH_SPACE, ZERO_WIDTH_SPACE, inline));
return this;
}
/**
* Clears all fields from the embed, such as those created with the
* {@link net.dv8tion.jda.api.EmbedBuilder#EmbedBuilder(net.dv8tion.jda.api.entities.MessageEmbed) EmbedBuilder(MessageEmbed)}
* constructor or via the
* {@link net.dv8tion.jda.api.EmbedBuilder#addField(net.dv8tion.jda.api.entities.MessageEmbed.Field) addField} methods.
*
* @return the builder after the field has been added
*/
@Nonnull
public EmbedBuilder clearFields()
{
this.fields.clear();
return this;
}
/**
* <b>Modifiable</b> list of {@link net.dv8tion.jda.api.entities.MessageEmbed MessageEmbed} Fields that the builder will
* use for {@link #build()}.
* <br>You can add/remove Fields and restructure this {@link java.util.List List} and it will then be applied in the
* built MessageEmbed. These fields will be available again through {@link net.dv8tion.jda.api.entities.MessageEmbed#getFields() MessageEmbed.getFields()}.
*
* @return Mutable List of {@link net.dv8tion.jda.api.entities.MessageEmbed.Field Fields}
*/
@Nonnull
public List<MessageEmbed.Field> getFields()
{
return fields;
}
private void urlCheck(@Nullable String url)
{
if (url != null)
{
Checks.check(url.length() <= MessageEmbed.URL_MAX_LENGTH, "URL cannot be longer than %d characters.", MessageEmbed.URL_MAX_LENGTH);
Checks.check(URL_PATTERN.matcher(url).matches(), "URL must be a valid http(s) or attachment url.");
}
}
}