/
ShardManager.java
1349 lines (1268 loc) · 56.3 KB
/
ShardManager.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
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright 2015-2019 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.sharding;
import net.dv8tion.jda.annotations.DeprecatedSince;
import net.dv8tion.jda.annotations.ReplaceWith;
import net.dv8tion.jda.api.JDA;
import net.dv8tion.jda.api.JDA.Status;
import net.dv8tion.jda.api.OnlineStatus;
import net.dv8tion.jda.api.entities.*;
import net.dv8tion.jda.api.requests.RestAction;
import net.dv8tion.jda.api.utils.MiscUtil;
import net.dv8tion.jda.api.utils.cache.CacheView;
import net.dv8tion.jda.api.utils.cache.ShardCacheView;
import net.dv8tion.jda.api.utils.cache.SnowflakeCacheView;
import net.dv8tion.jda.internal.JDAImpl;
import net.dv8tion.jda.internal.requests.EmptyRestAction;
import net.dv8tion.jda.internal.requests.RestActionImpl;
import net.dv8tion.jda.internal.requests.Route;
import net.dv8tion.jda.internal.utils.Checks;
import javax.annotation.CheckReturnValue;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import java.util.*;
import java.util.function.Function;
import java.util.function.IntFunction;
import java.util.stream.Collectors;
/**
* This class acts as a manager for multiple shards.
* It contains several methods to make your life with sharding easier.
*
* <br>Custom implementations may not support all methods and throw
* {@link java.lang.UnsupportedOperationException UnsupportedOperationExceptions} instead.
*
* @since 3.4
* @author Aljoscha Grebe
*/
public interface ShardManager
{
/**
* Adds all provided listeners to the event-listeners that will be used to handle events.
*
* <p>Note: when using the {@link net.dv8tion.jda.api.hooks.InterfacedEventManager InterfacedEventListener} (default),
* given listener <b>must</b> be instance of {@link net.dv8tion.jda.api.hooks.EventListener EventListener}!
*
* @param listeners
* The listener(s) which will react to events.
*
* @throws java.lang.IllegalArgumentException
* If either listeners or one of it's objects is {@code null}.
*/
default void addEventListener(@Nonnull final Object... listeners)
{
Checks.noneNull(listeners, "listeners");
this.getShardCache().forEach(jda -> jda.addEventListener(listeners));
}
/**
* Removes all provided listeners from the event-listeners and no longer uses them to handle events.
*
* @param listeners
* The listener(s) to be removed.
*
* @throws java.lang.IllegalArgumentException
* If either listeners or one of it's objects is {@code null}.
*/
default void removeEventListener(@Nonnull final Object... listeners)
{
Checks.noneNull(listeners, "listeners");
this.getShardCache().forEach(jda -> jda.removeEventListener(listeners));
}
/**
* Adds listeners provided by the listener provider to each shard to the event-listeners that will be used to handle events.
* The listener provider gets a shard id applied and is expected to return a listener.
*
* <p>Note: when using the {@link net.dv8tion.jda.api.hooks.InterfacedEventManager InterfacedEventListener} (default),
* given listener <b>must</b> be instance of {@link net.dv8tion.jda.api.hooks.EventListener EventListener}!
*
* @param eventListenerProvider
* The provider of listener(s) which will react to events.
*
* @throws java.lang.IllegalArgumentException
* If the provided listener provider or any of the listeners or provides are {@code null}.
*/
default void addEventListeners(@Nonnull final IntFunction<Object> eventListenerProvider)
{
Checks.notNull(eventListenerProvider, "event listener provider");
this.getShardCache().forEach(jda ->
{
Object listener = eventListenerProvider.apply(jda.getShardInfo().getShardId());
if (listener != null) jda.addEventListener(listener);
});
}
/**
* Remove listeners from shards by their id.
* The provider takes shard ids, and returns a collection of listeners that shall be removed from the respective
* shards.
*
* @param eventListenerProvider
* Gets shard ids applied and is expected to return a collection of listeners that shall be removed from
* the respective shards
*
* @throws java.lang.IllegalArgumentException
* If the provided event listeners provider is {@code null}.
*/
default void removeEventListeners(@Nonnull final IntFunction<Collection<Object>> eventListenerProvider)
{
Checks.notNull(eventListenerProvider, "event listener provider");
this.getShardCache().forEach(jda ->
jda.removeEventListener(eventListenerProvider.apply(jda.getShardInfo().getShardId()))
);
}
/**
* Remove a listener provider. This will stop further created / restarted shards from getting a listener added by
* that provider.
*
* Default is a no-op for backwards compatibility, see implementations like
* {@link DefaultShardManager#removeEventListenerProvider(IntFunction)} for actual code
*
* @param eventListenerProvider
* The provider of listeners that shall be removed.
*
* @throws java.lang.IllegalArgumentException
* If the provided listener provider is {@code null}.
*/
default void removeEventListenerProvider(@Nonnull IntFunction<Object> eventListenerProvider)
{
}
/**
* Returns the amount of shards queued for (re)connecting.
*
* @return The amount of shards queued for (re)connecting.
*/
int getShardsQueued();
/**
* Returns the amount of running shards.
*
* @return The amount of running shards.
*/
default int getShardsRunning()
{
return (int) this.getShardCache().size();
}
/**
* Returns the amount of shards managed by this {@link net.dv8tion.jda.api.sharding.ShardManager ShardManager}.
* This includes shards currently queued for a restart.
*
* @return The managed amount of shards.
*/
default int getShardsTotal()
{
return this.getShardsQueued() + this.getShardsRunning();
}
/**
* Used to access application details of this bot.
* <br>Since this is the same for every shard it picks {@link JDA#retrieveApplicationInfo()} from any shard.
*
* @throws java.lang.IllegalStateException
* If there is no running shard
*
* @return The Application registry for this bot.
*/
@Nonnull
default RestAction<ApplicationInfo> retrieveApplicationInfo()
{
return this.getShardCache().stream()
.findAny()
.orElseThrow(() -> new IllegalStateException("no active shards"))
.retrieveApplicationInfo();
}
/**
* The average time in milliseconds between all shards that discord took to respond to our last heartbeat.
* This roughly represents the WebSocket ping of this session. If there is no shard running this wil return {@code -1}.
*
* <p><b>{@link net.dv8tion.jda.api.requests.RestAction RestAction} request times do not
* correlate to this value!</b>
*
* @return The average time in milliseconds between heartbeat and the heartbeat ack response
*/
default double getAverageGatewayPing()
{
return this.getShardCache()
.stream()
.mapToLong(JDA::getGatewayPing)
.filter(ping -> ping != -1)
.average()
.orElse(-1D);
}
/**
* Gets all {@link net.dv8tion.jda.api.entities.Category Categories} visible to the currently logged in account.
*
* <p>This copies the backing store into a list. This means every call
* creates a new list with O(n) complexity. It is recommended to store this into
* a local variable or use {@link #getCategoryCache()} and use its more efficient
* versions of handling these values.
*
* @return An immutable list of all visible {@link net.dv8tion.jda.api.entities.Category Categories}.
*/
@Nonnull
default List<Category> getCategories()
{
return this.getCategoryCache().asList();
}
/**
* Gets a list of all {@link net.dv8tion.jda.api.entities.Category Categories} that have the same name as the one
* provided. <br>If there are no matching categories this will return an empty list.
*
* @param name
* The name to check
* @param ignoreCase
* Whether to ignore case on name checking
*
* @throws java.lang.IllegalArgumentException
* If the provided name is {@code null}
*
* @return Immutable list of all categories matching the provided name
*/
@Nonnull
default List<Category> getCategoriesByName(@Nonnull final String name, final boolean ignoreCase)
{
return this.getCategoryCache().getElementsByName(name, ignoreCase);
}
/**
* Gets the {@link net.dv8tion.jda.api.entities.Category Category} that matches the provided id. <br>If there is no
* matching {@link net.dv8tion.jda.api.entities.Category Category} this returns {@code null}.
*
* @param id
* The snowflake ID of the wanted Category
* @return Possibly-null {@link net.dv8tion.jda.api.entities.Category Category} for the provided ID.
*/
@Nullable
default Category getCategoryById(final long id)
{
return this.getCategoryCache().getElementById(id);
}
/**
* Gets the {@link net.dv8tion.jda.api.entities.Category Category} that matches the provided id. <br>If there is no
* matching {@link net.dv8tion.jda.api.entities.Category Category} this returns {@code null}.
*
* @param id
* The snowflake ID of the wanted Category
*
* @throws java.lang.IllegalArgumentException
* If the provided ID is not a valid {@code long}
*
* @return Possibly-null {@link net.dv8tion.jda.api.entities.Category Category} for the provided ID.
*/
@Nullable
default Category getCategoryById(@Nonnull final String id)
{
return this.getCategoryCache().getElementById(id);
}
/**
* {@link net.dv8tion.jda.api.utils.cache.SnowflakeCacheView SnowflakeCacheView} of
* all cached {@link net.dv8tion.jda.api.entities.Category Categories} visible to this ShardManager instance.
*
* @return {@link net.dv8tion.jda.api.utils.cache.SnowflakeCacheView SnowflakeCacheView}
*/
@Nonnull
default SnowflakeCacheView<Category> getCategoryCache()
{
return CacheView.allSnowflakes(() -> this.getShardCache().stream().map(JDA::getCategoryCache));
}
/**
* Retrieves an emote matching the specified {@code id} if one is available in our cache.
*
* <p><b>Unicode emojis are not included as {@link net.dv8tion.jda.api.entities.Emote Emote}!</b>
*
* @param id
* The id of the requested {@link net.dv8tion.jda.api.entities.Emote}.
*
* @return An {@link net.dv8tion.jda.api.entities.Emote Emote} represented by this id or null if none is found in
* our cache.
*/
@Nullable
default Emote getEmoteById(final long id)
{
return this.getEmoteCache().getElementById(id);
}
/**
* Retrieves an emote matching the specified {@code id} if one is available in our cache.
*
* <p><b>Unicode emojis are not included as {@link net.dv8tion.jda.api.entities.Emote Emote}!</b>
*
* @param id
* The id of the requested {@link net.dv8tion.jda.api.entities.Emote}.
*
* @throws java.lang.NumberFormatException
* If the provided {@code id} cannot be parsed by {@link Long#parseLong(String)}
*
* @return An {@link net.dv8tion.jda.api.entities.Emote Emote} represented by this id or null if none is found in
* our cache.
*/
@Nullable
default Emote getEmoteById(@Nonnull final String id)
{
return this.getEmoteCache().getElementById(id);
}
/**
* Unified {@link net.dv8tion.jda.api.utils.cache.SnowflakeCacheView SnowflakeCacheView} of
* all cached {@link net.dv8tion.jda.api.entities.Emote Emotes} visible to this ShardManager instance.
*
*
* @return Unified {@link net.dv8tion.jda.api.utils.cache.SnowflakeCacheView SnowflakeCacheView}
*/
@Nonnull
default SnowflakeCacheView<Emote> getEmoteCache()
{
return CacheView.allSnowflakes(() -> this.getShardCache().stream().map(JDA::getEmoteCache));
}
/**
* A collection of all to us known emotes (managed/restricted included).
*
* <p><b>Hint</b>: To check whether you can use an {@link net.dv8tion.jda.api.entities.Emote Emote} in a specific
* context you can use {@link Emote#canInteract(net.dv8tion.jda.api.entities.Member)} or {@link
* Emote#canInteract(net.dv8tion.jda.api.entities.User, net.dv8tion.jda.api.entities.MessageChannel)}
*
* <p><b>Unicode emojis are not included as {@link net.dv8tion.jda.api.entities.Emote Emote}!</b>
*
* <p>This copies the backing store into a list. This means every call
* creates a new list with O(n) complexity. It is recommended to store this into
* a local variable or use {@link #getEmoteCache()} and use its more efficient
* versions of handling these values.
*
* @return An immutable list of Emotes (which may or may not be available to usage).
*/
@Nonnull
default List<Emote> getEmotes()
{
return this.getEmoteCache().asList();
}
/**
* An unmodifiable list of all {@link net.dv8tion.jda.api.entities.Emote Emotes} that have the same name as the one
* provided. <br>If there are no {@link net.dv8tion.jda.api.entities.Emote Emotes} with the provided name, then
* this returns an empty list.
*
* <p><b>Unicode emojis are not included as {@link net.dv8tion.jda.api.entities.Emote Emote}!</b>
*
* @param name
* The name of the requested {@link net.dv8tion.jda.api.entities.Emote Emotes}.
* @param ignoreCase
* Whether to ignore case or not when comparing the provided name to each {@link
* net.dv8tion.jda.api.entities.Emote#getName()}.
*
* @return Possibly-empty list of all the {@link net.dv8tion.jda.api.entities.Emote Emotes} that all have the same
* name as the provided name.
*/
@Nonnull
default List<Emote> getEmotesByName(@Nonnull final String name, final boolean ignoreCase)
{
return this.getEmoteCache().getElementsByName(name, ignoreCase);
}
/**
* This returns the {@link net.dv8tion.jda.api.entities.Guild Guild} which has the same id as the one provided.
* <br>If there is no connected guild with an id that matches the provided one, then this returns {@code null}.
*
* @param id
* The id of the {@link net.dv8tion.jda.api.entities.Guild Guild}.
*
* @return Possibly-null {@link net.dv8tion.jda.api.entities.Guild Guild} with matching id.
*/
@Nullable
default Guild getGuildById(final long id)
{
return getGuildCache().getElementById(id);
}
/**
* This returns the {@link net.dv8tion.jda.api.entities.Guild Guild} which has the same id as the one provided.
* <br>If there is no connected guild with an id that matches the provided one, then this returns {@code null}.
*
* @param id
* The id of the {@link net.dv8tion.jda.api.entities.Guild Guild}.
*
* @return Possibly-null {@link net.dv8tion.jda.api.entities.Guild Guild} with matching id.
*/
@Nullable
default Guild getGuildById(@Nonnull final String id)
{
return getGuildById(MiscUtil.parseSnowflake(id));
}
/**
* An unmodifiable list of all {@link net.dv8tion.jda.api.entities.Guild Guilds} that have the same name as the one provided.
* <br>If there are no {@link net.dv8tion.jda.api.entities.Guild Guilds} with the provided name, then this returns an empty list.
*
* @param name
* The name of the requested {@link net.dv8tion.jda.api.entities.Guild Guilds}.
* @param ignoreCase
* Whether to ignore case or not when comparing the provided name to each {@link net.dv8tion.jda.api.entities.Guild#getName()}.
*
* @return Possibly-empty list of all the {@link net.dv8tion.jda.api.entities.Guild Guilds} that all have the same name as the provided name.
*/
@Nonnull
default List<Guild> getGuildsByName(@Nonnull final String name, final boolean ignoreCase)
{
return this.getGuildCache().getElementsByName(name, ignoreCase);
}
/**
* {@link net.dv8tion.jda.api.utils.cache.SnowflakeCacheView SnowflakeCacheView} of
* all cached {@link net.dv8tion.jda.api.entities.Guild Guilds} visible to this ShardManager instance.
*
* @return {@link net.dv8tion.jda.api.utils.cache.SnowflakeCacheView SnowflakeCacheView}
*/
@Nonnull
default SnowflakeCacheView<Guild> getGuildCache()
{
return CacheView.allSnowflakes(() -> this.getShardCache().stream().map(JDA::getGuildCache));
}
/**
* An unmodifiable List of all {@link net.dv8tion.jda.api.entities.Guild Guilds} that the logged account is connected to.
* <br>If this account is not connected to any {@link net.dv8tion.jda.api.entities.Guild Guilds}, this will return
* an empty list.
*
* <p>This copies the backing store into a list. This means every call
* creates a new list with O(n) complexity. It is recommended to store this into
* a local variable or use {@link #getGuildCache()} and use its more efficient
* versions of handling these values.
*
* @return Possibly-empty list of all the {@link net.dv8tion.jda.api.entities.Guild Guilds} that this account is connected to.
*/
@Nonnull
default List<Guild> getGuilds()
{
return this.getGuildCache().asList();
}
/**
* Gets all {@link net.dv8tion.jda.api.entities.Guild Guilds} that contain all given users as their members.
*
* @param users
* The users which all the returned {@link net.dv8tion.jda.api.entities.Guild Guilds} must contain.
*
* @return Unmodifiable list of all {@link net.dv8tion.jda.api.entities.Guild Guild} instances which have all {@link net.dv8tion.jda.api.entities.User Users} in them.
*/
@Nonnull
default List<Guild> getMutualGuilds(@Nonnull final Collection<User> users)
{
Checks.noneNull(users, "users");
return Collections.unmodifiableList(
this.getGuildCache().stream()
.filter(guild -> users.stream()
.allMatch(guild::isMember))
.collect(Collectors.toList()));
}
/**
* Gets all {@link net.dv8tion.jda.api.entities.Guild Guilds} that contain all given users as their members.
*
* @param users
* The users which all the returned {@link net.dv8tion.jda.api.entities.Guild Guilds} must contain.
*
* @return Unmodifiable list of all {@link net.dv8tion.jda.api.entities.Guild Guild} instances which have all {@link net.dv8tion.jda.api.entities.User Users} in them.
*/
@Nonnull
default List<Guild> getMutualGuilds(@Nonnull final User... users)
{
Checks.notNull(users, "users");
return this.getMutualGuilds(Arrays.asList(users));
}
/**
* Attempts to retrieve a {@link net.dv8tion.jda.api.entities.User User} object based on the provided id.
* <br>This first calls {@link #getUserById(long)}, and if the return is {@code null} then a request
* is made to the Discord servers.
*
* <p>The returned {@link net.dv8tion.jda.api.requests.RestAction RestAction} can encounter the following Discord errors:
* <ul>
* <li>{@link net.dv8tion.jda.api.requests.ErrorResponse#UNKNOWN_USER ErrorResponse.UNKNOWN_USER}
* <br>Occurs when the provided id does not refer to a {@link net.dv8tion.jda.api.entities.User User}
* known by Discord. Typically occurs when developers provide an incomplete id (cut short).</li>
* </ul>
*
* @param id
* The id of the requested {@link net.dv8tion.jda.api.entities.User User}.
*
* @throws java.lang.IllegalArgumentException
* If the provided id String is not a valid snowflake.
* @throws java.lang.IllegalStateException
* If there isn't any active shards.
*
* @return {@link net.dv8tion.jda.api.requests.RestAction RestAction} - Type: {@link net.dv8tion.jda.api.entities.User User}
* <br>On request, gets the User with id matching provided id from Discord.
*/
@Nonnull
@CheckReturnValue
default RestAction<User> retrieveUserById(@Nonnull String id)
{
return retrieveUserById(MiscUtil.parseSnowflake(id));
}
/**
* Attempts to retrieve a {@link net.dv8tion.jda.api.entities.User User} object based on the provided id.
* <br>This first calls {@link #getUserById(long)}, and if the return is {@code null} then a request
* is made to the Discord servers.
*
* <p>The returned {@link net.dv8tion.jda.api.requests.RestAction RestAction} can encounter the following Discord errors:
* <ul>
* <li>{@link net.dv8tion.jda.api.requests.ErrorResponse#UNKNOWN_USER ErrorResponse.UNKNOWN_USER}
* <br>Occurs when the provided id does not refer to a {@link net.dv8tion.jda.api.entities.User User}
* known by Discord. Typically occurs when developers provide an incomplete id (cut short).</li>
* </ul>
*
* @param id
* The id of the requested {@link net.dv8tion.jda.api.entities.User User}.
*
* @throws java.lang.IllegalStateException
* If there isn't any active shards.
*
* @return {@link net.dv8tion.jda.api.requests.RestAction RestAction} - Type: {@link net.dv8tion.jda.api.entities.User User}
* <br>On request, gets the User with id matching provided id from Discord.
*/
@Nonnull
@CheckReturnValue
default RestAction<User> retrieveUserById(long id)
{
JDA api = null;
for (JDA shard : getShardCache())
{
api = shard;
User user = shard.getUserById(id);
if (user != null)
return new EmptyRestAction<>(shard, user);
}
if (api == null)
throw new IllegalStateException("no shards active");
JDAImpl jda = (JDAImpl) api;
Route.CompiledRoute route = Route.Users.GET_USER.compile(Long.toUnsignedString(id));
return new RestActionImpl<>(jda, route, (response, request) -> jda.getEntityBuilder().createFakeUser(response.getObject(), false));
}
/**
* Searches for the first user that has the matching Discord Tag.
* <br>Format has to be in the form {@code Username#Discriminator} where the
* username must be between 2 and 32 characters (inclusive) matching the exact casing and the discriminator
* must be exactly 4 digits.
*
* <p>This only checks users that are known to the currently logged in account (shards). If a user exists
* with the tag that is not available in the {@link #getUserCache() User-Cache} it will not be detected.
* <br>Currently Discord does not offer a way to retrieve a user by their discord tag.
*
* @param tag
* The Discord Tag in the format {@code Username#Discriminator}
*
* @throws java.lang.IllegalArgumentException
* If the provided tag is null or not in the described format
*
* @return The {@link net.dv8tion.jda.api.entities.User} for the discord tag or null if no user has the provided tag
*/
@Nullable
default User getUserByTag(@Nonnull String tag)
{
return getShardCache().applyStream(stream ->
stream.map(jda -> jda.getUserByTag(tag))
.filter(Objects::nonNull)
.findFirst()
.orElse(null)
);
}
/**
* Searches for the first user that has the matching Discord Tag.
* <br>Format has to be in the form {@code Username#Discriminator} where the
* username must be between 2 and 32 characters (inclusive) matching the exact casing and the discriminator
* must be exactly 4 digits.
*
* <p>This only checks users that are known to the currently logged in account (shards). If a user exists
* with the tag that is not available in the {@link #getUserCache() User-Cache} it will not be detected.
* <br>Currently Discord does not offer a way to retrieve a user by their discord tag.
*
* @param username
* The name of the user
* @param discriminator
* The discriminator of the user
*
* @throws java.lang.IllegalArgumentException
* If the provided arguments are null or not in the described format
*
* @return The {@link net.dv8tion.jda.api.entities.User} for the discord tag or null if no user has the provided tag
*/
@Nullable
default User getUserByTag(@Nonnull String username, @Nonnull String discriminator)
{
return getShardCache().applyStream(stream ->
stream.map(jda -> jda.getUserByTag(username, discriminator))
.filter(Objects::nonNull)
.findFirst()
.orElse(null)
);
}
/**
* This returns the {@link net.dv8tion.jda.api.entities.PrivateChannel PrivateChannel} which has the same id as the one provided.
* <br>If there is no known {@link net.dv8tion.jda.api.entities.PrivateChannel PrivateChannel} with an id that matches the provided
* one, then this returns {@code null}.
*
* @param id
* The id of the {@link net.dv8tion.jda.api.entities.PrivateChannel PrivateChannel}.
*
* @return Possibly-null {@link net.dv8tion.jda.api.entities.PrivateChannel PrivateChannel} with matching id.
*/
@Nullable
default PrivateChannel getPrivateChannelById(final long id)
{
return this.getPrivateChannelCache().getElementById(id);
}
/**
* This returns the {@link net.dv8tion.jda.api.entities.PrivateChannel PrivateChannel} which has the same id as the one provided.
* <br>If there is no known {@link net.dv8tion.jda.api.entities.PrivateChannel PrivateChannel} with an id that matches the provided
* one, then this returns {@code null}.
*
* @param id
* The id of the {@link net.dv8tion.jda.api.entities.PrivateChannel PrivateChannel}.
*
* @throws java.lang.NumberFormatException
* If the provided {@code id} cannot be parsed by {@link Long#parseLong(String)}
*
* @return Possibly-null {@link net.dv8tion.jda.api.entities.PrivateChannel PrivateChannel} with matching id.
*/
@Nullable
default PrivateChannel getPrivateChannelById(@Nonnull final String id)
{
return this.getPrivateChannelCache().getElementById(id);
}
/**
* {@link net.dv8tion.jda.api.utils.cache.SnowflakeCacheView SnowflakeCacheView} of
* all cached {@link net.dv8tion.jda.api.entities.PrivateChannel PrivateChannels} visible to this ShardManager instance.
*
* @return {@link net.dv8tion.jda.api.utils.cache.SnowflakeCacheView SnowflakeCacheView}
*/
@Nonnull
default SnowflakeCacheView<PrivateChannel> getPrivateChannelCache()
{
return CacheView.allSnowflakes(() -> this.getShardCache().stream().map(JDA::getPrivateChannelCache));
}
/**
* An unmodifiable list of all known {@link net.dv8tion.jda.api.entities.PrivateChannel PrivateChannels}.
*
* <p>This copies the backing store into a list. This means every call
* creates a new list with O(n) complexity. It is recommended to store this into
* a local variable or use {@link #getPrivateChannelCache()} and use its more efficient
* versions of handling these values.
*
* @return Possibly-empty list of all {@link net.dv8tion.jda.api.entities.PrivateChannel PrivateChannels}.
*/
@Nonnull
default List<PrivateChannel> getPrivateChannels()
{
return this.getPrivateChannelCache().asList();
}
/**
* Retrieves the {@link net.dv8tion.jda.api.entities.Role Role} associated to the provided id. <br>This iterates
* over all {@link net.dv8tion.jda.api.entities.Guild Guilds} and check whether a Role from that Guild is assigned
* to the specified ID and will return the first that can be found.
*
* @param id
* The id of the searched Role
*
* @return Possibly-null {@link net.dv8tion.jda.api.entities.Role Role} for the specified ID
*/
@Nullable
default Role getRoleById(final long id)
{
return this.getRoleCache().getElementById(id);
}
/**
* Retrieves the {@link net.dv8tion.jda.api.entities.Role Role} associated to the provided id. <br>This iterates
* over all {@link net.dv8tion.jda.api.entities.Guild Guilds} and check whether a Role from that Guild is assigned
* to the specified ID and will return the first that can be found.
*
* @param id
* The id of the searched Role
*
* @throws java.lang.NumberFormatException
* If the provided {@code id} cannot be parsed by {@link Long#parseLong(String)}
*
* @return Possibly-null {@link net.dv8tion.jda.api.entities.Role Role} for the specified ID
*/
@Nullable
default Role getRoleById(@Nonnull final String id)
{
return this.getRoleCache().getElementById(id);
}
/**
* Unified {@link net.dv8tion.jda.api.utils.cache.SnowflakeCacheView SnowflakeCacheView} of
* all cached {@link net.dv8tion.jda.api.entities.Role Roles} visible to this ShardManager instance.
*
* @return Unified {@link net.dv8tion.jda.api.utils.cache.SnowflakeCacheView SnowflakeCacheView}
*/
@Nonnull
default SnowflakeCacheView<Role> getRoleCache()
{
return CacheView.allSnowflakes(() -> this.getShardCache().stream().map(JDA::getRoleCache));
}
/**
* All {@link net.dv8tion.jda.api.entities.Role Roles} this ShardManager instance can see. <br>This will iterate over each
* {@link net.dv8tion.jda.api.entities.Guild Guild} retrieved from {@link #getGuilds()} and collect its {@link
* net.dv8tion.jda.api.entities.Guild#getRoles() Guild.getRoles()}.
*
* <p>This copies the backing store into a list. This means every call
* creates a new list with O(n) complexity. It is recommended to store this into
* a local variable or use {@link #getRoleCache()} and use its more efficient
* versions of handling these values.
*
* @return Immutable List of all visible Roles
*/
@Nonnull
default List<Role> getRoles()
{
return this.getRoleCache().asList();
}
/**
* Retrieves all {@link net.dv8tion.jda.api.entities.Role Roles} visible to this ShardManager instance.
* <br>This simply filters the Roles returned by {@link #getRoles()} with the provided name, either using
* {@link String#equals(Object)} or {@link String#equalsIgnoreCase(String)} on {@link net.dv8tion.jda.api.entities.Role#getName()}.
*
* @param name
* The name for the Roles
* @param ignoreCase
* Whether to use {@link String#equalsIgnoreCase(String)}
*
* @return Immutable List of all Roles matching the parameters provided.
*/
@Nonnull
default List<Role> getRolesByName(@Nonnull final String name, final boolean ignoreCase)
{
return this.getRoleCache().getElementsByName(name, ignoreCase);
}
/**
* This returns the {@link net.dv8tion.jda.api.JDA JDA} instance which has the same id as the one provided.
* <br>If there is no shard with an id that matches the provided one, then this returns {@code null}.
*
* @param id
* The id of the shard.
*
* @return The {@link net.dv8tion.jda.api.JDA JDA} instance with the given shardId or
* {@code null} if no shard has the given id
*/
@Nullable
default JDA getShardById(final int id)
{
return this.getShardCache().getElementById(id);
}
/**
* This returns the {@link net.dv8tion.jda.api.JDA JDA} instance which has the same id as the one provided.
* <br>If there is no shard with an id that matches the provided one, then this returns {@code null}.
*
* @param id
* The id of the shard.
*
* @return The {@link net.dv8tion.jda.api.JDA JDA} instance with the given shardId or
* {@code null} if no shard has the given id
*/
@Nullable
default JDA getShardById(@Nonnull final String id)
{
return this.getShardCache().getElementById(id);
}
/**
* Unified {@link ShardCacheView ShardCacheView} of
* all cached {@link net.dv8tion.jda.api.JDA JDA} bound to this ShardManager instance.
*
* @return Unified {@link ShardCacheView ShardCacheView}
*/
@Nonnull
ShardCacheView getShardCache();
/**
* Gets all {@link net.dv8tion.jda.api.JDA JDA} instances bound to this ShardManager.
*
* <p>This copies the backing store into a list. This means every call
* creates a new list with O(n) complexity. It is recommended to store this into
* a local variable or use {@link #getShardCache()} and use its more efficient
* versions of handling these values.
*
* @return An immutable list of all managed {@link net.dv8tion.jda.api.JDA JDA} instances.
*/
@Nonnull
default List<JDA> getShards()
{
return this.getShardCache().asList();
}
/**
* This returns the {@link net.dv8tion.jda.api.JDA.Status JDA.Status} of the shard which has the same id as the one provided.
* <br>If there is no shard with an id that matches the provided one, then this returns {@code null}.
*
* @param shardId
* The id of the shard.
*
* @return The {@link net.dv8tion.jda.api.JDA.Status JDA.Status} of the shard with the given shardId or
* {@code null} if no shard has the given id
*/
@Nullable
default JDA.Status getStatus(final int shardId)
{
final JDA jda = this.getShardCache().getElementById(shardId);
return jda == null ? null : jda.getStatus();
}
/**
* Gets the current {@link net.dv8tion.jda.api.JDA.Status Status} of all shards.
*
* @return All current shard statuses.
*/
@Nonnull
default Map<JDA, Status> getStatuses()
{
return Collections.unmodifiableMap(this.getShardCache().stream()
.collect(Collectors.toMap(Function.identity(), JDA::getStatus)));
}
/**
* This returns the {@link net.dv8tion.jda.api.entities.TextChannel TextChannel} which has the same id as the one provided.
* <br>If there is no known {@link net.dv8tion.jda.api.entities.TextChannel TextChannel} with an id that matches the provided
* one, then this returns {@code null}.
*
* <p><b>Note:</b> just because a {@link net.dv8tion.jda.api.entities.TextChannel TextChannel} is present does
* not mean that you will be able to send messages to it. Furthermore, if you log into this account on the discord
* client, it is you will not see the channel that this returns. This is because the discord client
* hides any {@link net.dv8tion.jda.api.entities.TextChannel TextChannel} that you don't have the
* {@link net.dv8tion.jda.api.Permission#MESSAGE_READ Permission.MESSAGE_READ} permission in.
*
* @param id
* The id of the {@link net.dv8tion.jda.api.entities.TextChannel TextChannel}.
*
* @return Possibly-null {@link net.dv8tion.jda.api.entities.TextChannel TextChannel} with matching id.
*/
@Nullable
default TextChannel getTextChannelById(final long id)
{
return this.getTextChannelCache().getElementById(id);
}
/**
* This returns the {@link net.dv8tion.jda.api.entities.TextChannel TextChannel} which has the same id as the one provided.
* <br>If there is no known {@link net.dv8tion.jda.api.entities.TextChannel TextChannel} with an id that matches the provided
* one, then this returns {@code null}.
*
* <p><b>Note:</b> just because a {@link net.dv8tion.jda.api.entities.TextChannel TextChannel} is present does
* not mean that you will be able to send messages to it. Furthermore, if you log into this account on the discord
* client, it is you will not see the channel that this returns. This is because the discord client
* hides any {@link net.dv8tion.jda.api.entities.TextChannel TextChannel} that you don't have the
* {@link net.dv8tion.jda.api.Permission#MESSAGE_READ Permission.MESSAGE_READ} permission in.
*
* @param id
* The id of the {@link net.dv8tion.jda.api.entities.TextChannel TextChannel}.
*
* @return Possibly-null {@link net.dv8tion.jda.api.entities.TextChannel TextChannel} with matching id.
*/
@Nullable
default TextChannel getTextChannelById(@Nonnull final String id)
{
return this.getTextChannelCache().getElementById(id);
}
/**
* {@link net.dv8tion.jda.api.utils.cache.SnowflakeCacheView SnowflakeCacheView} of
* all cached {@link net.dv8tion.jda.api.entities.TextChannel TextChannels} visible to this ShardManager instance.
*
* @return {@link net.dv8tion.jda.api.utils.cache.SnowflakeCacheView SnowflakeCacheView}
*/
@Nonnull
default SnowflakeCacheView<TextChannel> getTextChannelCache()
{
return CacheView.allSnowflakes(() -> this.getShardCache().stream().map(JDA::getTextChannelCache));
}
/**
* An unmodifiable List of all {@link net.dv8tion.jda.api.entities.TextChannel TextChannels} of all connected
* {@link net.dv8tion.jda.api.entities.Guild Guilds}.
*
* <p><b>Note:</b> just because a {@link net.dv8tion.jda.api.entities.TextChannel TextChannel} is present in this list does
* not mean that you will be able to send messages to it. Furthermore, if you log into this account on the discord
* client, it is possible that you will see fewer channels than this returns. This is because the discord client
* hides any {@link net.dv8tion.jda.api.entities.TextChannel TextChannel} that you don't have the
* {@link net.dv8tion.jda.api.Permission#MESSAGE_READ Permission.MESSAGE_READ} permission in.
*
* <p>This copies the backing store into a list. This means every call
* creates a new list with O(n) complexity. It is recommended to store this into
* a local variable or use {@link #getTextChannelCache()} and use its more efficient
* versions of handling these values.
*
* @return Possibly-empty list of all known {@link net.dv8tion.jda.api.entities.TextChannel TextChannels}.
*/
@Nonnull
default List<TextChannel> getTextChannels()
{
return this.getTextChannelCache().asList();
}
/**
* This returns the {@link net.dv8tion.jda.api.entities.StoreChannel StoreChannel} which has the same id as the one provided.
* <br>If there is no known {@link net.dv8tion.jda.api.entities.StoreChannel StoreChannel} with an id that matches the provided
* one, then this returns {@code null}.
*
* @param id
* The id of the {@link net.dv8tion.jda.api.entities.StoreChannel StoreChannel}.
*
* @return Possibly-null {@link net.dv8tion.jda.api.entities.StoreChannel StoreChannel} with matching id.
*/
@Nullable
default StoreChannel getStoreChannelById(final long id)
{
return this.getStoreChannelCache().getElementById(id);
}
/**
* This returns the {@link net.dv8tion.jda.api.entities.StoreChannel StoreChannel} which has the same id as the one provided.
* <br>If there is no known {@link net.dv8tion.jda.api.entities.StoreChannel StoreChannel} with an id that matches the provided
* one, then this returns {@code null}.
*
* @param id
* The id of the {@link net.dv8tion.jda.api.entities.StoreChannel StoreChannel}.
*
* @return Possibly-null {@link net.dv8tion.jda.api.entities.StoreChannel StoreChannel} with matching id.
*/
@Nullable
default StoreChannel getStoreChannelById(@Nonnull final String id)
{
return this.getStoreChannelCache().getElementById(id);
}
/**
* {@link net.dv8tion.jda.api.utils.cache.SnowflakeCacheView SnowflakeCacheView} of
* all cached {@link net.dv8tion.jda.api.entities.StoreChannel StoreChannels} visible to this ShardManager instance.
*
* @return {@link net.dv8tion.jda.api.utils.cache.SnowflakeCacheView SnowflakeCacheView}
*/
@Nonnull
default SnowflakeCacheView<StoreChannel> getStoreChannelCache()
{
return CacheView.allSnowflakes(() -> this.getShardCache().stream().map(JDA::getStoreChannelCache));
}
/**
* An unmodifiable List of all {@link net.dv8tion.jda.api.entities.StoreChannel StoreChannels} of all connected
* {@link net.dv8tion.jda.api.entities.Guild Guilds}.
*
* <p>This copies the backing store into a list. This means every call
* creates a new list with O(n) complexity. It is recommended to store this into
* a local variable or use {@link #getStoreChannelCache()} and use its more efficient
* versions of handling these values.
*
* @return Possibly-empty list of all known {@link net.dv8tion.jda.api.entities.StoreChannel StoreChannels}.
*/
@Nonnull
default List<StoreChannel> getStoreChannels()
{
return this.getStoreChannelCache().asList();
}