/
Activity.java
668 lines (619 loc) · 21.5 KB
/
Activity.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
/*
* 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.entities;
import net.dv8tion.jda.annotations.Incubating;
import net.dv8tion.jda.internal.entities.EntityBuilder;
import net.dv8tion.jda.internal.utils.Checks;
import net.dv8tion.jda.internal.utils.EncodingUtil;
import net.dv8tion.jda.internal.utils.Helpers;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import java.time.Instant;
import java.time.temporal.TemporalUnit;
import java.util.Objects;
import java.util.regex.Pattern;
/**
* Represents a Discord {@link Activity Activity}.
* <br>This should contain all information provided from Discord about a Activity.
*
* @since 2.1
* @author John A. Grosh
*
* @see #of(ActivityType, String)
* @see #of(ActivityType, String, String)
* @see #playing(String)
* @see #watching(String)
* @see #listening(String)
* @see #streaming(String, String)
* @see #competing(String)
*/
public interface Activity
{
/** The Pattern used for {@link #isValidStreamingUrl(String)} */
Pattern STREAMING_URL = Pattern.compile("https?://(www\\.)?(twitch\\.tv/|youtube\\.com/watch\\?v=).+", Pattern.CASE_INSENSITIVE);
/**
* Whether this is a <a href="https://discord.com/developers/docs/rich-presence/best-practices" target="_blank">Rich Presence</a>
* <br>If {@code false} the result of {@link #asRichPresence()} is {@code null}
*
* @return {@code true} if this is a {@link net.dv8tion.jda.api.entities.RichPresence RichPresence}
*/
boolean isRich();
/**
* {@link net.dv8tion.jda.api.entities.RichPresence RichPresence} representation of
* this Activity.
*
* @return RichPresence or {@code null} if {@link #isRich()} returns {@code false}
*/
@Nullable
RichPresence asRichPresence();
/**
* The displayed name of the {@link Activity Activity}.
* <br>For {@link ActivityType#CUSTOM_STATUS} this will return the custom status text.
*
* @return String containing the Activity's name.
*/
@Nonnull
String getName();
/**
* The URL of the {@link Activity Activity} if the game is actually a Stream.
* <br>This will return null for regular games.
*
* @return Possibly-null String containing the Activity's URL.
*/
@Nullable
String getUrl();
/**
* The type of {@link Activity Activity}.
*
* @return Never-null {@link net.dv8tion.jda.api.entities.Activity.ActivityType ActivityType} representing the type of Activity
*/
@Nonnull
ActivityType getType();
/**
* Information on the match duration, start, and end.
*
* @return {@link net.dv8tion.jda.api.entities.Activity.Timestamps Timestamps} wrapper of {@code null} if unset
*/
@Nullable
Timestamps getTimestamps();
/**
* The emoji (or custom emoji) attached to a custom status.
*
* @return Possibly-null {@link Emoji} used for custom status
*/
@Nullable
Emoji getEmoji();
/**
* Creates a new Activity instance with the specified name.
* <br>In order to appear as "streaming" in the official client you must
* provide a valid (see documentation of method) streaming URL in {@link #streaming(String, String) Activity.streaming(String, String)}.
*
* @param name
* The not-null name of the newly created game
*
* @throws IllegalArgumentException
* if the specified name is null, empty, blank or longer than 128 characters
*
* @return A valid Activity instance with the provided name with {@link net.dv8tion.jda.api.entities.Activity.ActivityType#DEFAULT}
*/
@Nonnull
static Activity playing(@Nonnull String name)
{
Checks.notBlank(name, "Name");
name = name.trim();
Checks.notLonger(name, 128, "Name");
return EntityBuilder.createActivity(name, null, ActivityType.DEFAULT);
}
/**
* Creates a new Activity instance with the specified name and url.
* <br>The specified URL must be valid according to discord standards in order to display as "streaming" in the official client.
* A valid streaming URL must be derived from {@code https://twitch.tv/} or {@code https://youtube.com/watch?v=} and can be verified using {@link #isValidStreamingUrl(String)}. (see documentation)
*
* @param name
* The not-null name of the newly created game
* @param url
* The streaming url to use, required to display as "streaming"
*
* @throws IllegalArgumentException
* If the specified name is null, empty or longer than 128 characters
*
* @return A valid Activity instance with the provided name and url
*
* @see #isValidStreamingUrl(String)
*/
@Nonnull
static Activity streaming(@Nonnull String name, @Nullable String url)
{
Checks.notEmpty(name, "Provided game name");
name = Helpers.isBlank(name) ? name : name.trim();
Checks.notLonger(name, 128, "Name");
ActivityType type;
if (isValidStreamingUrl(url))
type = ActivityType.STREAMING;
else
type = ActivityType.DEFAULT;
return EntityBuilder.createActivity(name, url, type);
}
/**
* Creates a new Activity instance with the specified name.
* <br>This will display as {@code Listening name} in the official client
*
* @param name
* The not-null name of the newly created game
*
* @throws IllegalArgumentException
* if the specified name is null, empty, blank or longer than 128 characters
*
* @return A valid Activity instance with the provided name with {@link net.dv8tion.jda.api.entities.Activity.ActivityType#LISTENING}
*/
@Nonnull
static Activity listening(@Nonnull String name)
{
Checks.notBlank(name, "Name");
name = name.trim();
Checks.notLonger(name, 128, "Name");
return EntityBuilder.createActivity(name, null, ActivityType.LISTENING);
}
/**
* Creates a new Activity instance with the specified name.
* <br>This will display as {@code Watching name} in the official client
*
* @param name
* The not-null name of the newly created game
*
* @throws IllegalArgumentException
* if the specified name is null, empty, blank or longer than 128 characters
*
* @return A valid Activity instance with the provided name with {@link net.dv8tion.jda.api.entities.Activity.ActivityType#WATCHING}
*
* @incubating This feature is not yet confirmed for the official bot API
*/
@Nonnull
@Incubating
static Activity watching(@Nonnull String name)
{
Checks.notBlank(name, "Name");
name = name.trim();
Checks.notLonger(name, 128, "Name");
return EntityBuilder.createActivity(name, null, ActivityType.WATCHING);
}
/**
* Creates a new Activity instance with the specified name.
* <br>This will display as {@code Competing in name} in the official client
*
* @param name
* The not-null name of the newly created game
*
* @throws IllegalArgumentException
* If the specified name is null, empty, blank or longer than 128 characters
*
* @return A valid Activity instance with the provided name with {@link net.dv8tion.jda.api.entities.Activity.ActivityType#COMPETING}
*
* @since 4.2.1
*/
@Nonnull
static Activity competing(@Nonnull String name)
{
Checks.notBlank(name, "Name");
name = name.trim();
Checks.notLonger(name, 128, "Name");
return EntityBuilder.createActivity(name, null, ActivityType.COMPETING);
}
/**
* Creates a new Activity instance with the specified name and url.
*
* @param type
* The {@link net.dv8tion.jda.api.entities.Activity.ActivityType ActivityType} to use
* @param name
* The not-null name of the newly created game
*
* @throws IllegalArgumentException
* If the specified name is null, empty or longer than 128 characters
*
* @return A valid Activity instance with the provided name and url
*/
@Nonnull
static Activity of(@Nonnull ActivityType type, @Nonnull String name)
{
return of(type, name, null);
}
/**
* Creates a new Activity instance with the specified name and url.
* <br>The provided url would only be used for {@link net.dv8tion.jda.api.entities.Activity.ActivityType#STREAMING ActivityType.STREAMING}
* and should be a twitch url.
*
* @param type
* The {@link net.dv8tion.jda.api.entities.Activity.ActivityType ActivityType} to use
* @param name
* The not-null name of the newly created game
* @param url
* The streaming url to use, required to display as "streaming".
*
* @throws IllegalArgumentException
* If the specified name is null, empty or longer than 128 characters
*
* @return A valid Activity instance with the provided name and url
*
* @see #isValidStreamingUrl(String)
*/
@Nonnull
static Activity of(@Nonnull ActivityType type, @Nonnull String name, @Nullable String url)
{
Checks.notNull(type, "Type");
switch (type)
{
case DEFAULT:
return playing(name);
case STREAMING:
return streaming(name, url);
case LISTENING:
return listening(name);
case WATCHING:
return watching(name);
case COMPETING:
return competing(name);
default:
throw new IllegalArgumentException("ActivityType " + type + " is not supported!");
}
}
/**
* Checks if a given String is a valid Twitch/Youtube streaming url (ie, one that will display "Streaming" on the Discord client).
*
* @param url
* The url to check.
*
* @return True if the provided url is valid for triggering Discord's streaming status
*/
static boolean isValidStreamingUrl(@Nullable String url)
{
return url != null && STREAMING_URL.matcher(url).matches();
}
/**
* The type game being played, differentiating between a game and stream types.
*/
enum ActivityType
{
/**
* The ActivityType used to represent a normal {@link Activity Activity} status.
*/
DEFAULT(0),
/**
* Used to indicate that the {@link Activity Activity} is a stream
* <br>This type is displayed as "Streaming" in the discord client.
*/
STREAMING(1),
/**
* Used to indicate that the {@link Activity Activity} should display
* as {@code Listening...} in the official client.
*/
LISTENING(2),
/**
* Used to indicate that the {@link Activity Activity} should display
* as {@code Watching...} in the official client.
*
* @incubating This feature is not yet confirmed for the official bot API
*/
@Incubating
WATCHING(3),
/**
* Used to indicate that the {@link Activity Activity} should display as a custom status
* in the official client.
*
* @incubating This feature is currently not officially documented and might change
*/
@Incubating
CUSTOM_STATUS(4),
/**
* Used to indicate that the {@link Activity Activity} should display
* as {@code Competing in...} in the official client.
*
* @since 4.2.1
*/
COMPETING(5);
private final int key;
ActivityType(int key)
{
this.key = key;
}
/**
* The Discord defined id key for this ActivityType.
*
* @return the id key.
*/
public int getKey()
{
return key;
}
/**
* Gets the ActivityType related to the provided key.
* <br>If an unknown key is provided, this returns {@link #DEFAULT}
*
* @param key
* The Discord key referencing a ActivityType.
*
* @return The ActivityType that has the key provided, or {@link #DEFAULT} for unknown key.
*/
@Nonnull
public static ActivityType fromKey(int key)
{
switch (key)
{
case 0:
default:
return DEFAULT;
case 1:
return STREAMING;
case 2:
return LISTENING;
case 3:
return WATCHING;
case 4:
return CUSTOM_STATUS;
case 5:
return COMPETING;
}
}
}
/**
* Represents the start and end timestamps for a running match
*/
class Timestamps
{
protected final long start;
protected final long end;
public Timestamps(long start, long end)
{
this.start = start;
this.end = end;
}
/**
* Epoch second timestamp of match start, or {@code 0} of unset.
*
* @return Epoch second timestamp of match start, or {@code 0} of unset.
*/
public long getStart()
{
return start;
}
/**
* Shortcut for {@code Instant.ofEpochSecond(start)}
*
* @return Instant of match start, or {@code null} if unset
*/
@Nullable
public Instant getStartTime()
{
return start <= 0 ? null : Instant.ofEpochMilli(start);
}
/**
* Epoch second timestamp of match end, or {@code 0} of unset.
*
* @return Epoch second timestamp of match end, or {@code 0} of unset.
*/
public long getEnd()
{
return end;
}
/**
* Shortcut for {@code Instant.ofEpochSecond(start)}
*
* @return Instant of match start, or {@code null} if unset
*/
@Nullable
public Instant getEndTime()
{
return end <= 0 ? null : Instant.ofEpochMilli(end);
}
/**
* Calculates the amount of time until {@link #getEndTime()} in terms of the specified unit.
* <br>If {@link #getEndTime()} is {@code null} this will be negative.
*
* @param unit
* The {@link java.time.temporal.TemporalUnit TemporalUnit} to return
*
* @throws IllegalArgumentException
* If the provided unit is {@code null}
* @throws ArithmeticException
* If a numeric overflow occurs
* @throws java.time.DateTimeException
* If the amount cannot be calculated
* @throws java.time.temporal.UnsupportedTemporalTypeException
* If the provided unit is not supported
*
* @return Remaining time in the provided {@link java.time.temporal.TemporalUnit TemporalUnit} or {@code -1} if unset
*
* @see java.time.Instant#until(java.time.temporal.Temporal, java.time.temporal.TemporalUnit) Instant.until(Temporal, TemporalUnit)
* @see java.time.temporal.TemporalUnit
*/
public long getRemainingTime(TemporalUnit unit)
{
Checks.notNull(unit, "TemporalUnit");
Instant end = getEndTime();
return end != null ? Instant.now().until(end, unit) : -1;
}
/**
* Calculates the elapsed time from {@link #getStartTime()} to now in terms of the specified unit.
* <br>If {@link #getStartTime()} is {@code null} this will be negative.
*
* @param unit
* The {@link java.time.temporal.TemporalUnit TemporalUnit} to return
*
* @throws IllegalArgumentException
* If the provided unit is {@code null}
* @throws ArithmeticException
* If a numeric overflow occurs
* @throws java.time.DateTimeException
* If the amount cannot be calculated
* @throws java.time.temporal.UnsupportedTemporalTypeException
* If the provided unit is not supported
*
* @return Elapsed time in the provided {@link java.time.temporal.TemporalUnit TemporalUnit} or {@code -1} if unset
*
* @see java.time.Instant#until(java.time.temporal.Temporal, java.time.temporal.TemporalUnit) Instant.until(Temporal, TemporalUnit)
* @see java.time.temporal.TemporalUnit
*/
public long getElapsedTime(TemporalUnit unit)
{
Checks.notNull(unit, "TemporalUnit");
Instant start = getStartTime();
return start != null ? start.until(Instant.now(), unit) : -1;
}
@Override
public String toString()
{
return Helpers.format("RichPresenceTimestamp(%d-%d)", start, end);
}
@Override
public boolean equals(Object obj)
{
if (!(obj instanceof Timestamps))
return false;
Timestamps t = (Timestamps) obj;
return start == t.start && end == t.end;
}
@Override
public int hashCode()
{
return Objects.hash(start, end);
}
}
/**
* Emoji for a custom status.
* <br>This can be a unicode emoji or a custom emoji (Emote).
*/
class Emoji implements ISnowflake, IMentionable
{
private final String name;
private final long id;
private final boolean animated;
public Emoji(String name, long id, boolean animated)
{
this.name = name;
this.id = id;
this.animated = animated;
}
public Emoji(String name)
{
this(name, 0, false);
}
/**
* The name of this emoji. This will be the unicode characters for a unicode emoji
* and the name of the custom emote otherwise.
*
* @return The emoji name
*
* @see #getAsCodepoints()
*/
@Nonnull
public String getName()
{
return name;
}
/**
* The codepoint notation ({@code "U+XXXX"}) for the unicode of this emoji.
* Not available for custom emotes.
*
* @throws IllegalStateException
* If {@link #isEmoji()} is false
*
* @return The codepoint notation
*
* @see #getName()
*/
@Nonnull
public String getAsCodepoints()
{
if (!isEmoji())
throw new IllegalStateException("Cannot convert custom emote to codepoints");
return EncodingUtil.encodeCodepoints(name);
}
/**
* The id for this custom emoji.
*
* @throws IllegalStateException
* If {@link #isEmote()} is false
*
* @return The emoji id
*/
@Override
public long getIdLong()
{
if (!isEmote())
throw new IllegalStateException("Cannot get id for unicode emoji");
return id;
}
/**
* Whether this emoji is animated.
* This is always false for unicode emoji.
*
* @return True, if this emoji is animated
*/
public boolean isAnimated()
{
return animated;
}
/**
* Whether this is a unicode emoji.
*
* @return True, if this is a unicode emoji
*/
public boolean isEmoji()
{
return id == 0;
}
/**
* Whether this is a custom emoji (Emote)
*
* @return True, if this is a custom emoji
*/
public boolean isEmote()
{
return id != 0;
}
@Nonnull
@Override
public String getAsMention()
{
if (isEmoji())
return name; // unicode name
// custom emoji format (for messages)
return String.format("<%s:%s:%s>", isAnimated() ? "a" : "", name, getId());
}
@Override
public int hashCode()
{
return id == 0 ? name.hashCode() : Long.hashCode(id);
}
@Override
public boolean equals(Object obj)
{
if (obj == this)
return true;
if (!(obj instanceof Emoji))
return false;
Emoji other = (Emoji) obj;
return id == 0 ? other.name.equals(this.name)
: other.id == this.id;
}
@Override
public String toString()
{
if (isEmoji())
return "ActivityEmoji(" + getAsCodepoints() + ')';
return "ActivityEmoji(" + Long.toUnsignedString(id) + " / " + name + ')';
}
}
}