/
EmbedBuilder.java
541 lines (516 loc) · 19.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
/*
* Copyright 2015-2017 Austin Keener & Michael Ritter
*
* 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.core;
import net.dv8tion.jda.core.entities.MessageEmbed;
import net.dv8tion.jda.core.entities.impl.MessageEmbedImpl;
import org.apache.http.util.Args;
import java.awt.Color;
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.core.entities.MessageEmbed MessageEmbeds}.
* <br>A visual breakdown of an Embed and how it relates to this class is available at
* <a href="http://imgur.com/a/yOb5n" target="_blank">http://imgur.com/a/yOb5n</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):\\/\\/.+\\..{2,}\\s*", Pattern.CASE_INSENSITIVE);
private String url;
private String title;
private StringBuilder description = new StringBuilder();
private OffsetDateTime timestamp;
private Color color;
private MessageEmbed.Thumbnail thumbnail;
private MessageEmbed.AuthorInfo author;
private MessageEmbed.Footer footer;
private MessageEmbed.ImageInfo image;
private final List<MessageEmbed.Field> fields;
/**
* Creates an EmbedBuilder to be used to creates an embed to send.
* <br>Every part of an embed can be removed or cleared by providing {@code null} to the setter method.
*/
public EmbedBuilder()
{
this(null);
}
/**
* Creates an EmbedBuilder using fields in an existing embed.
*
* @param embed
* the existing embed
*/
public EmbedBuilder(MessageEmbed embed)
{
fields = new LinkedList<>();
if(embed != null)
{
setDescription(embed.getDescription());
this.url = embed.getUrl();
this.title = embed.getTitle();
this.timestamp = embed.getTimestamp();
this.color = embed.getColor();
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.core.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.core.entities.MessageEmbed}
*/
public MessageEmbed build()
{
if (isEmpty())
throw new IllegalStateException("Cannot build an empty embed!");
if (description.length() > MessageEmbed.TEXT_MAX_LENGTH)
throw new IllegalStateException(String.format("Description is longer than %d! Please limit your input!", MessageEmbed.TEXT_MAX_LENGTH));
final String description = this.description.length() < 1 ? null : this.description.toString();
return new MessageEmbedImpl().setTitle(title)
.setUrl(url)
.setDescription(description)
.setTimestamp(timestamp)
.setColor(color)
.setThumbnail(thumbnail)
.setAuthor(author)
.setFooter(footer)
.setImage(image)
.setFields(fields);
}
/**
* 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
&& description == null
&& timestamp == null
&& color == null
&& thumbnail == null
&& author == null
&& footer == null
&& image == null
&& fields.isEmpty();
}
/**
* Sets the Title of the embed.
*
* <p><b><a href="http://i.imgur.com/JgZtxIM.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.core.entities.MessageEmbed#TITLE_MAX_LENGTH}.</li>
* <li>If the length of {@code url} is longer than {@link net.dv8tion.jda.core.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
*/
public EmbedBuilder setTitle(String title, String url)
{
if (title == null)
{
this.title = null;
this.url = null;
}
else
{
if (title.isEmpty())
throw new IllegalArgumentException("Title cannot be empty!");
if (title.length() > MessageEmbed.TITLE_MAX_LENGTH)
throw new IllegalArgumentException("Title cannot be longer than " + MessageEmbed.TITLE_MAX_LENGTH + " characters.");
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
*/
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="http://i.imgur.com/lbchtwk.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.core.entities.MessageEmbed#TEXT_MAX_LENGTH}
*
* @return the builder after the description has been set
*/
public EmbedBuilder setDescription(CharSequence description)
{
if (description == null || description.length() < 1)
{
this.description = new StringBuilder();
}
else
{
Args.check(description.length() <= MessageEmbed.TEXT_MAX_LENGTH,
"Description cannot be longer than %d characters.", MessageEmbed.TEXT_MAX_LENGTH);
this.description = new StringBuilder(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="http://i.imgur.com/lbchtwk.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.core.entities.MessageEmbed#TEXT_MAX_LENGTH}.</li>
* </ul>
*
* @return the builder after the description has been set
*/
public EmbedBuilder appendDescription(CharSequence description)
{
Args.notNull(description, "description");
Args.check(this.description.length() + description.length() <= MessageEmbed.TEXT_MAX_LENGTH,
"Description cannot be longer than %d characters.", MessageEmbed.TEXT_MAX_LENGTH);
this.description.append(description);
return this;
}
/**
* Sets the Timestamp of the embed.
*
* <p><b><a href="http://i.imgur.com/YP4NiER.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
*/
public EmbedBuilder setTimestamp(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.
* @param color the color of the embed
* @return the builder after the color has been set
*/
public EmbedBuilder setColor(Color color)
{
this.color = color;
return this;
}
/**
* Sets the Thumbnail of the embed.
*
* <p><b><a href="http://i.imgur.com/Zc3qwqB.png">Example</a></b>
*
* @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.core.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
*/
public EmbedBuilder setThumbnail(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="http://i.imgur.com/2hzuHFJ.png">Example</a></b>
*
* @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.core.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
*/
public EmbedBuilder setImage(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.
*
* <p><b><a href="http://i.imgur.com/JgZtxIM.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
* @param iconUrl
* the url of the icon for the author
*
* @throws java.lang.IllegalArgumentException
* <ul>
* <li>If the length of {@code url} is longer than {@link net.dv8tion.jda.core.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.core.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
*/
public EmbedBuilder setAuthor(String name, String url, 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
{
urlCheck(url);
urlCheck(iconUrl);
this.author = new MessageEmbed.AuthorInfo(name, url, iconUrl, null);
}
return this;
}
/**
* Sets the Footer of the embed.
*
* <p><b><a href="http://i.imgur.com/jdf4sbi.png">Example</a></b>
*
* @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.core.entities.MessageEmbed#TEXT_MAX_LENGTH}.</li>
* <li>If the length of {@code iconUrl} is longer than {@link net.dv8tion.jda.core.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
*/
public EmbedBuilder setFooter(String text, 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
{
if (text.length() > MessageEmbed.TEXT_MAX_LENGTH)
throw new IllegalArgumentException("Text cannot be longer than " + MessageEmbed.TEXT_MAX_LENGTH + " characters.");
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
*/
public EmbedBuilder addField(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.core.EmbedBuilder#ZERO_WIDTH_SPACE}.
*
* <p><b><a href="http://i.imgur.com/gnjzCoo.png">Example of Inline</a></b>
* <p><b><a href="http://i.imgur.com/Ky0KlsT.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.core.entities.MessageEmbed#TITLE_MAX_LENGTH}.</li>
* <li>If the length of {@code value} is greater than {@link net.dv8tion.jda.core.entities.MessageEmbed#TEXT_MAX_LENGTH}.</li>
* </ul>
*
* @return the builder after the field has been added
*/
public EmbedBuilder addField(String name, String value, boolean inline)
{
if (name == null && value == null)
return this;
else if (name == null || value == null)
throw new IllegalArgumentException("Both Name and Value must be set!");
else if (name.length() > MessageEmbed.TITLE_MAX_LENGTH)
throw new IllegalArgumentException("Name cannot be longer than " + MessageEmbed.TITLE_MAX_LENGTH + " characters.");
else if (value.length() > MessageEmbed.VALUE_MAX_LENGTH)
throw new IllegalArgumentException("Value cannot be longer than " + MessageEmbed.VALUE_MAX_LENGTH + " characters.");
if (name.isEmpty())
name = ZERO_WIDTH_SPACE;
if (value.isEmpty())
value = ZERO_WIDTH_SPACE;
this.fields.add(new MessageEmbed.Field(name, value, inline));
return this;
}
/**
* Adds a blank (empty) Field to the embed.
*
* <p><b><a href="http://i.imgur.com/tB6tYWy.png">Example of Inline</a></b>
* <p><b><a href="http://i.imgur.com/lQqgH3H.png">Example of Non-inline</a></b>
*
* @param inline
* whether or not this field should display inline
*
* @return the builder after the field has been added
*/
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.core.EmbedBuilder#EmbedBuilder(net.dv8tion.jda.core.entities.MessageEmbed) EmbedBuilder(MessageEmbed)}
* constructor or via the
* {@link net.dv8tion.jda.core.EmbedBuilder#addField(net.dv8tion.jda.core.entities.MessageEmbed.Field) addField} methods.
*
* @return the builder after the field has been added
*/
public EmbedBuilder clearFields()
{
this.fields.clear();
return this;
}
private void urlCheck(String url)
{
if (url == null)
return;
else if (url.length() > MessageEmbed.URL_MAX_LENGTH)
throw new IllegalArgumentException("URL cannot be longer than " + MessageEmbed.URL_MAX_LENGTH + " characters.");
else if (!URL_PATTERN.matcher(url).matches())
throw new IllegalArgumentException("URL must be a valid http or https url.");
}
}