/
MessageEmbed.java
603 lines (546 loc) · 18.7 KB
/
MessageEmbed.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
/*
* Copyright 2015-2017 Austin Keener & Michael Ritter & Florian Spieß
*
* 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.entities;
import net.dv8tion.jda.core.AccountType;
import net.dv8tion.jda.core.EmbedBuilder;
import org.apache.http.util.Args;
import java.awt.Color;
import java.time.OffsetDateTime;
import java.util.List;
/**
* Represents an embed displayed by Discord.
* <br>A visual representation of an Embed can be found at:
* <a href="http://imgur.com/a/yOb5n" target="_blank">http://imgur.com/a/yOb5n</a>
* <br>This class has many possibilities for null values, so be careful!
*/
public interface MessageEmbed
{
int TITLE_MAX_LENGTH = 256;
int VALUE_MAX_LENGTH = 1024;
int TEXT_MAX_LENGTH = 2048;
int URL_MAX_LENGTH = 2000;
int EMBED_MAX_LENGTH_BOT = 4000;
int EMBED_MAX_LENGTH_CLIENT = 2000;
/**
* The that was originally placed into chat that spawned this embed.
*
* @return Never-null String containing the original message url.
*/
String getUrl();
/**
* The title of the embed. Typically this will be the html title of the webpage that is being embedded.<br>
* If no title could be found, like the case of {@link EmbedType EmbedType} = {@link net.dv8tion.jda.core.entities.EmbedType#IMAGE IMAGE},
* this method will return null.
*
* @return Possibly-null String containing the title of the embedded resource.
*/
String getTitle();
/**
* The description of the embedded resource.
* <br>This is provided only if Discord could find a description for the embedded resource using the provided url.
* <br>Commonly, this is null. Be careful when using it.
*
* @return Possibly-null String containing a description of the embedded resource.
*/
String getDescription();
/**
* The {@link net.dv8tion.jda.core.entities.EmbedType EmbedType} of this embed.
*
* @return The {@link net.dv8tion.jda.core.entities.EmbedType EmbedType} of this embed.
*/
EmbedType getType();
/**
* The information about the {@link net.dv8tion.jda.core.entities.MessageEmbed.Thumbnail Thumbnail} image to be displayed with the embed.
* <br>If a {@link net.dv8tion.jda.core.entities.MessageEmbed.Thumbnail Thumbnail} was not part of this embed, this returns null.
*
* @return Possibly-null {@link net.dv8tion.jda.core.entities.MessageEmbed.Thumbnail Thumbnail} instance
* containing general information on the displayable thumbnail.
*/
Thumbnail getThumbnail();
/**
* The information on site from which the embed was generated from.
* <br>If Discord did not generate any deliverable information about the site, this returns null.
*
* @return Possibly-null {@link net.dv8tion.jda.core.entities.MessageEmbed.Provider Provider}
* containing site information.
*/
Provider getSiteProvider();
/**
* The information on the creator of the embedded content.
* <br>This is typically used to represent the account on the providing site.
*
* @return Possibly-null {@link net.dv8tion.jda.core.entities.MessageEmbed.AuthorInfo AuthorInfo}
* containing author information.
*/
AuthorInfo getAuthor();
/**
* The information about the video which should be displayed as an embed.
* <br>This is used when sites with HTML5 players are linked and embedded. Most commonly Youtube.
* <br>If this {@link net.dv8tion.jda.core.entities.EmbedType EmbedType} != {@link net.dv8tion.jda.core.entities.EmbedType#VIDEO VIDEO}
* this will always return null.
*
* @return Possibly-null {@link net.dv8tion.jda.core.entities.MessageEmbed.VideoInfo VideoInfo}
* containing the information about the video which should be embedded.
*/
VideoInfo getVideoInfo();
/**
* The footer (bottom) of the embedded content.
* <br>This is typically used for timestamps or site icons.
*
* @return Possibly-null {@link net.dv8tion.jda.core.entities.MessageEmbed.Footer Footer}
* containing the embed footer content.
*/
Footer getFooter();
/**
* The information about the image in the message embed
*
* @return Possibly-null {@link net.dv8tion.jda.core.entities.MessageEmbed.ImageInfo ImageInfo}
* containing image information.
*/
ImageInfo getImage();
/**
* The fields in a message embed.
* <br>Message embeds can contain multiple fields, each with a name, value, and a boolean
* to determine if it will fall in-line with other fields. If the embed contains no
* fields, an empty list will be returned.
*
* @return Never-null (but possibly empty) List of {@link net.dv8tion.jda.core.entities.MessageEmbed.Field Field} objects
* containing field information.
*/
List<Field> getFields();
/**
* The color of the stripe on the side of the embed.
* <br>If the color is 0 (no color), this will return null.
*
* @return Possibly-null Color.
*/
Color getColor();
/**
* The timestamp of the embed.
*
* @return Possibly-null OffsetDateTime object representing the timestamp.
*/
OffsetDateTime getTimestamp();
/**
* The total amount of characters that is displayed when this embed is displayed by the Discord client.
*
* <p>An Embed can only have, at max, {@value #EMBED_MAX_LENGTH_BOT} displayable text characters for {@link net.dv8tion.jda.core.AccountType#BOT AccountType.BOT}
* accounts or {@value #EMBED_MAX_LENGTH_CLIENT} displayable text characters for {@link net.dv8tion.jda.core.AccountType#CLIENT AccountType.CLIENT} accounts.
*
* <p>Both of these values are defined by {@link #EMBED_MAX_LENGTH_BOT EMBED_MAX_LENGTH_BOT} and
* {@link #EMBED_MAX_LENGTH_CLIENT EMBED_MAX_LENGTH_CLIENT} respectively.
*
* @return A never-negative sum of all displayed text characters.
*/
int getLength();
/**
* Whether this MessageEmbed can be used in a message.
* <br>This applies to {@link net.dv8tion.jda.core.AccountType#BOT Bot}- and {@link net.dv8tion.jda.core.AccountType#CLIENT Client Accounts}
*
* <p>Total Character Limits
* <ul>
* <li>Bot: {@value #EMBED_MAX_LENGTH_BOT}</li>
* <li>Client: {@value #EMBED_MAX_LENGTH_CLIENT}</li>
* </ul>
*
* @param type
* The {@link net.dv8tion.jda.core.AccountType AccountType} to inspect
*
* @throws java.lang.IllegalArgumentException
* If the provided AccountType is {@code null} or not supported by this operation
*
* @return True, if this MessageEmbed can be used to send messages for this specified AccountType
*
* @see #getLength()
*/
default boolean isSendable(AccountType type)
{
Args.notNull(type, "AccountType");
final int length = getLength();
switch (type)
{
case BOT: return length <= EMBED_MAX_LENGTH_BOT;
case CLIENT: return length <= EMBED_MAX_LENGTH_CLIENT;
default: throw new IllegalArgumentException(String.format("Cannot check against AccountType '%s'!", type));
}
}
/**
* Represents the information Discord provided about a thumbnail image that should be
* displayed with an embed message.
*/
class Thumbnail
{
protected final String url;
protected final String proxyUrl;
protected final int width;
protected final int height;
public Thumbnail(String url, String proxyUrl, int width, int height)
{
this.url = url;
this.proxyUrl = proxyUrl;
this.width = width;
this.height = height;
}
/**
* The web url of this thumbnail image.
*
* @return Never-null String containing the url of the displayed image.
*/
public String getUrl()
{
return url;
}
/**
* The Discord proxied url of the thumbnail image.
* <br>This url is used to access the image through Discord instead of directly to prevent ip scraping.
*
* @return Never-null String containing the proxied url of this image.
*/
public String getProxyUrl()
{
return proxyUrl;
}
/**
* The width of the thumbnail image.
*
* @return Never-negative, Never-zero int containing the width of the image.
*/
public int getWidth()
{
return width;
}
/**
* The height of the thumbnail image.
*
* @return Never-negative, Never-zero int containing the height of the image.
*/
public int getHeight()
{
return height;
}
}
/**
* Multipurpose class that represents a provider of content,
* whether directly through creation or indirectly through hosting.
*/
class Provider
{
protected final String name;
protected final String url;
public Provider(String name, String url)
{
this.name = name;
this.url = url;
}
/**
* The name of the provider.
* <br>If this is an author, most likely the author's username.
* <br>If this is a website, most likely the site's name.
*
* @return Never-null String containing the name of the provider.
*/
public String getName()
{
return name;
}
/**
* The url of the provider.
*
* @return Possibly-null String containing the url of the provider.
*/
public String getUrl()
{
return url;
}
}
/**
* Represents the information provided to embed a video.
* <br>The videos represented are expected to be played using an HTML5 player from the
* site which the url belongs to.
*/
class VideoInfo
{
protected final String url;
protected final int width;
protected final int height;
public VideoInfo(String url, int width, int height)
{
this.url = url;
this.width = width;
this.height = height;
}
/**
* The url of the video.
*
* @return Never-null String containing the video url.
*/
public String getUrl()
{
return url;
}
/**
* The width of the video.
* <br>This usually isn't the actual video width, but instead the starting embed window size.
*
* <p>Basically: Don't rely on this to represent the actual video's quality or size.
*
* @return Non-negative, Non-zero int containing the width of the embedded video.
*/
public int getWidth()
{
return width;
}
/**
* The height of the video.
* <br>This usually isn't the actual video height, but instead the starting embed window size.
*
* <p>Basically: Don't rely on this to represent the actual video's quality or size.
*
* @return
* Non-negative, Non-zero int containing the height of the embedded video.
*/
public int getHeight()
{
return height;
}
}
/**
* Represents the information provided to embed an image.
*/
class ImageInfo
{
protected final String url;
protected final String proxyUrl;
protected final int width;
protected final int height;
public ImageInfo(String url, String proxyUrl, int width, int height)
{
this.url = url;
this.proxyUrl = proxyUrl;
this.width = width;
this.height = height;
}
/**
* The url of the image.
*
* @return Never-null String containing the image url.
*/
public String getUrl()
{
return url;
}
/**
* The url of the image, proxied by Discord
* <br>This url is used to access the image through Discord instead of directly to prevent ip scraping.
*
* @return Never-null String containing the proxied image url.
*/
public String getProxyUrl()
{
return proxyUrl;
}
/**
* The width of the image.
*
* @return Non-negative, Non-zero int containing the width of the embedded image.
*/
public int getWidth()
{
return width;
}
/**
* The height of the image.
*
* @return Non-negative, Non-zero int containing the height of the embedded image.
*/
public int getHeight()
{
return height;
}
}
/**
* Class that represents the author of content, possibly including an icon
* that Discord proxies.
*/
class AuthorInfo
{
protected final String name;
protected final String url;
protected final String iconUrl;
protected final String proxyIconUrl;
public AuthorInfo(String name, String url, String iconUrl, String proxyIconUrl)
{
this.name = name;
this.url = url;
this.iconUrl = iconUrl;
this.proxyIconUrl = proxyIconUrl;
}
/**
* The name of the Author.
* <br>This is most likely the name of the account associated with the embed
*
* @return Possibly-null String containing the name of the author.
*/
public String getName()
{
return name;
}
/**
* The url of the author.
*
* @return Possibly-null String containing the url of the author.
*/
public String getUrl()
{
return url;
}
/**
* The url of the author's icon.
*
* @return Possibly-null String containing the author's icon url.
*/
public String getIconUrl()
{
return iconUrl;
}
/**
* The url of the author's icon, proxied by Discord
* <br>This url is used to access the image through Discord instead of directly to prevent ip scraping.
*
* @return Possibly-null String containing the proxied icon url.
*/
public String getProxyIconUrl()
{
return proxyIconUrl;
}
}
/**
* Class that represents a footer at the bottom of an embed
*/
class Footer
{
protected final String text;
protected final String iconUrl;
protected final String proxyIconUrl;
public Footer(String text, String iconUrl, String proxyIconUrl)
{
this.text = text;
this.iconUrl = iconUrl;
this.proxyIconUrl = proxyIconUrl;
}
/**
* The text in the footer
*
* @return Possibly-null String containing the text in the footer.
*/
public String getText()
{
return text;
}
/**
* The url of the footer's icon.
*
* @return Possibly-null String containing the footer's icon url.
*/
public String getIconUrl()
{
return iconUrl;
}
/**
* The url of the footer's icon, proxied by Discord
* <br>This url is used to access the image through Discord instead of directly to prevent ip scraping.
*
* @return Possibly-null String containing the proxied icon url.
*/
public String getProxyIconUrl()
{
return proxyIconUrl;
}
}
/**
* Represents a field in an embed. A single embed contains an array of
* embed fields, each with a name and value, and a boolean determining if
* the field can display on the same line as previous fields if there is
* enough space horizontally.
*
* @since 3.0
* @author John A. Grosh
*/
class Field
{
protected final String name;
protected final String value;
protected final boolean inline;
protected Field(String name, String value, boolean inline, boolean checked)
{
if (checked)
{
if (name == null || value == null)
throw new IllegalArgumentException("Both Name and Value must be set!");
else if (name.length() > TITLE_MAX_LENGTH)
throw new IllegalArgumentException("Name cannot be longer than " + TITLE_MAX_LENGTH + " characters.");
else if (value.length() > VALUE_MAX_LENGTH)
throw new IllegalArgumentException("Value cannot be longer than " + VALUE_MAX_LENGTH + " characters.");
if (name.isEmpty())
this.name = EmbedBuilder.ZERO_WIDTH_SPACE;
else
this.name = name;
if (value.isEmpty())
this.value = EmbedBuilder.ZERO_WIDTH_SPACE;
else
this.value = value;
this.inline = inline;
}
else
{
this.name = name;
this.value = value;
this.inline = inline;
}
}
public Field(String name, String value, boolean inline)
{
this(name, value, inline, true);
}
/**
* The name of the field
*
* @return Possibly-null String containing the name of the field.
*/
public String getName()
{
return name;
}
/**
* The value of the field
*
* @return Possibly-null String containing the value (contents) of the field.
*/
public String getValue()
{
return value;
}
/**
* If the field is in line.
*
* @return true if the field can be in line with other fields, false otherwise.
*/
public boolean isInline()
{
return inline;
}
}
}