-
-
Notifications
You must be signed in to change notification settings - Fork 722
/
DefaultShardManagerBuilder.java
1107 lines (1031 loc) · 48.7 KB
/
DefaultShardManagerBuilder.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-2018 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.bot.sharding;
import com.neovisionaries.ws.client.WebSocketFactory;
import net.dv8tion.jda.annotations.DeprecatedSince;
import net.dv8tion.jda.annotations.ReplaceWith;
import net.dv8tion.jda.core.OnlineStatus;
import net.dv8tion.jda.core.audio.factory.IAudioSendFactory;
import net.dv8tion.jda.core.entities.Game;
import net.dv8tion.jda.core.hooks.IEventManager;
import net.dv8tion.jda.core.utils.Checks;
import net.dv8tion.jda.core.utils.SessionController;
import net.dv8tion.jda.core.utils.cache.CacheFlag;
import okhttp3.OkHttpClient;
import javax.security.auth.login.LoginException;
import java.util.*;
import java.util.concurrent.*;
import java.util.function.IntFunction;
import java.util.stream.Collectors;
/**
* Used to create new instances of JDA's default {@link net.dv8tion.jda.bot.sharding.ShardManager ShardManager} implementation.
*
* <p>A single DefaultShardManagerBuilder can be reused multiple times. Each call to {@link #build()}
* creates a new {@link net.dv8tion.jda.bot.sharding.ShardManager ShardManager} instance using the same information.
*
* @since 3.4
* @author Aljoscha Grebe
*/
public class DefaultShardManagerBuilder
{
protected final List<Object> listeners = new ArrayList<>();
protected final List<IntFunction<Object>> listenerProviders = new ArrayList<>();
protected SessionController sessionController = null;
protected EnumSet<CacheFlag> cacheFlags = EnumSet.allOf(CacheFlag.class);
protected boolean enableContext = true;
protected boolean enableBulkDeleteSplitting = true;
protected boolean enableShutdownHook = true;
protected boolean enableVoice = true;
protected boolean autoReconnect = true;
protected boolean retryOnTimeout = true;
protected boolean useShutdownNow = false;
protected boolean enableCompression = true;
protected int shardsTotal = -1;
protected int maxReconnectDelay = 900;
protected int corePoolSize = 5;
protected String token = null;
protected IntFunction<Boolean> idleProvider = null;
protected IntFunction<OnlineStatus> statusProvider = null;
protected IntFunction<? extends Game> gameProvider = null;
protected IntFunction<? extends ConcurrentMap<String, String>> contextProvider = null;
protected IntFunction<? extends IEventManager> eventManagerProvider = null;
protected ThreadPoolProvider<? extends ScheduledExecutorService> rateLimitPoolProvider = null;
protected ThreadPoolProvider<? extends ScheduledExecutorService> gatewayPoolProvider = null;
protected ThreadPoolProvider<? extends ExecutorService> callbackPoolProvider = null;
protected Collection<Integer> shards = null;
protected OkHttpClient.Builder httpClientBuilder = null;
protected OkHttpClient httpClient = null;
protected WebSocketFactory wsFactory = null;
protected IAudioSendFactory audioSendFactory = null;
protected ThreadFactory threadFactory = null;
/**
* Creates a completely empty DefaultShardManagerBuilder.
* <br>You need to set the token using
* {@link net.dv8tion.jda.bot.sharding.DefaultShardManagerBuilder#setToken(String) setToken(String)}
* before calling {@link net.dv8tion.jda.bot.sharding.DefaultShardManagerBuilder#build() build()}.
*/
public DefaultShardManagerBuilder() {}
/**
* Flags used to enable parts of the JDA cache to reduce the runtime memory footprint.
* <br><b>It is highly recommended to use {@link #setDisabledCacheFlags(EnumSet)} instead
* for backwards compatibility</b>. We might add more flags in the future which you then effectively disable
* when updating and not changing your setting here.
*
* @param flags
* EnumSet containing the flags for cache services that should be <b>enabled</b>
*
* @return The DefaultShardManagerBuilder instance. Useful for chaining.
*/
public DefaultShardManagerBuilder setEnabledCacheFlags(EnumSet<CacheFlag> flags)
{
this.cacheFlags = flags == null ? EnumSet.noneOf(CacheFlag.class) : EnumSet.copyOf(flags);
return this;
}
/**
* Flags used to disable parts of the JDA cache to reduce the runtime memory footprint.
* <br>Shortcut for {@code setEnabledCacheFlags(EnumSet.complementOf(flags))}
*
* @param flags
* EnumSet containing the flags for cache services that should be <b>disabled</b>
*
* @return The DefaultShardManagerBuilder instance. Useful for chaining.
*/
public DefaultShardManagerBuilder setDisabledCacheFlags(EnumSet<CacheFlag> flags)
{
return setEnabledCacheFlags(EnumSet.complementOf(flags));
}
/**
* Sets the {@link net.dv8tion.jda.core.utils.SessionController SessionController}
* for the resulting ShardManager instance. This can be used to sync behaviour and state between shards
* of a bot and should be one and the same instance on all builders for the shards.
*
* @param controller
* The {@link net.dv8tion.jda.core.utils.SessionController SessionController} to use
*
* @return The DefaultShardManagerBuilder instance. Useful for chaining.
*
* @see net.dv8tion.jda.core.utils.SessionControllerAdapter SessionControllerAdapter
*/
public DefaultShardManagerBuilder setSessionController(SessionController controller)
{
this.sessionController = controller;
return this;
}
/**
* Sets the {@link org.slf4j.MDC MDC} mappings provider to use in JDA.
* <br>If sharding is enabled JDA will automatically add a {@code jda.shard} context with the format {@code [SHARD_ID / TOTAL]}
* where {@code SHARD_ID} and {@code TOTAL} are the shard configuration.
* Additionally it will provide context for the id via {@code jda.shard.id} and the total via {@code jda.shard.total}.
*
* <p><b>The manager will call this with a shardId and it is recommended to provide a different context map for each shard!</b>
* <br>This automatically switches {@link #setContextEnabled(boolean)} to true if the provided function is not null!
*
* @param provider
* The provider for <b>modifiable</b> context maps to use in JDA, or {@code null} to reset
*
* @return The DefaultShardManagerBuilder instance. Useful for chaining.
*
* @see <a href="https://www.slf4j.org/api/org/slf4j/MDC.html" target="_blank">MDC Javadoc</a>
*/
public DefaultShardManagerBuilder setContextMap(IntFunction<? extends ConcurrentMap<String, String>> provider)
{
this.contextProvider = provider;
if (provider != null)
this.enableContext = true;
return this;
}
/**
* Whether JDA should use a synchronized MDC context for all of its controlled threads.
* <br>Default: {@code true}
*
* @param enable
* True, if JDA should provide an MDC context map
*
* @return The DefaultShardManagerBuilder instance. Useful for chaining.
*
* @see <a href="https://www.slf4j.org/api/org/slf4j/MDC.html" target="_blank">MDC Javadoc</a>
* @see #setContextMap(java.util.function.IntFunction)
*/
public DefaultShardManagerBuilder setContextEnabled(boolean enable)
{
this.enableContext = enable;
return this;
}
/**
* Enable stream-compression on the gateway connection,
* this will decrease the amount of used bandwidth for the running bot instance
* for the cost of a few extra cycles for decompression.
* <br><b>Default: true</b>
*
* <p><b>We recommend to keep this enabled unless you have issues with the decompression</b>
* <br>This mode might become obligatory in a future version, do not rely on this switch to stay.
*
* @param enable
* True, if the gateway connection should use compression
*
* @return The DefaultShardManagerBuilder instance. Useful for chaining.
*
* @see <a href="https://discordapp.com/developers/docs/topics/gateway#transport-compression" target="_blank">Official Discord Documentation - Transport Compression</a>
*/
public DefaultShardManagerBuilder setCompressionEnabled(boolean enable)
{
this.enableCompression = enable;
return this;
}
/**
* Adds all provided listeners to the list of listeners that will be used to populate the {@link DefaultShardManager DefaultShardManager} object.
* <br>This uses the {@link net.dv8tion.jda.core.hooks.InterfacedEventManager InterfacedEventListener} by default.
* <br>To switch to the {@link net.dv8tion.jda.core.hooks.AnnotatedEventManager AnnotatedEventManager},
* use {@link #setEventManagerProvider(IntFunction) setEventManagerProvider(id -> new AnnotatedEventManager())}.
*
* <p><b>Note:</b> When using the {@link net.dv8tion.jda.core.hooks.InterfacedEventManager InterfacedEventListener} (default),
* given listener(s) <b>must</b> be instance of {@link net.dv8tion.jda.core.hooks.EventListener EventListener}!
*
* @param listeners
* The listener(s) to add to the list.
*
* @return The DefaultShardManagerBuilder instance. Useful for chaining.
*
* @see DefaultShardManager#addEventListener(Object...) JDA.addEventListener(Object...)
*/
public DefaultShardManagerBuilder addEventListeners(final Object... listeners)
{
return this.addEventListeners(Arrays.asList(listeners));
}
/**
* Adds all provided listeners to the list of listeners that will be used to populate the {@link DefaultShardManager DefaultShardManager} object.
* <br>This uses the {@link net.dv8tion.jda.core.hooks.InterfacedEventManager InterfacedEventListener} by default.
* <br>To switch to the {@link net.dv8tion.jda.core.hooks.AnnotatedEventManager AnnotatedEventManager},
* use {@link #setEventManagerProvider(IntFunction) setEventManager(id -> new AnnotatedEventManager())}.
*
* <p><b>Note:</b> When using the {@link net.dv8tion.jda.core.hooks.InterfacedEventManager InterfacedEventListener} (default),
* given listener(s) <b>must</b> be instance of {@link net.dv8tion.jda.core.hooks.EventListener EventListener}!
*
* @param listeners
* The listener(s) to add to the list.
*
* @return The DefaultShardManagerBuilder instance. Useful for chaining.
*
* @see DefaultShardManager#addEventListener(Object...) JDA.addEventListener(Object...)
*/
public DefaultShardManagerBuilder addEventListeners(final Collection<Object> listeners)
{
Checks.noneNull(listeners, "listeners");
this.listeners.addAll(listeners);
return this;
}
/**
* Removes all provided listeners from the list of listeners.
*
* @param listeners
* The listener(s) to remove from the list.
*
* @return The DefaultShardManagerBuilder instance. Useful for chaining.
*
* @see net.dv8tion.jda.core.JDA#removeEventListener(Object...) JDA.removeEventListener(Object...)
*/
public DefaultShardManagerBuilder removeEventListeners(final Object... listeners)
{
return this.removeEventListeners(Arrays.asList(listeners));
}
/**
* Removes all provided listeners from the list of listeners.
*
* @param listeners
* The listener(s) to remove from the list.
*
* @return The DefaultShardManagerBuilder instance. Useful for chaining.
*
* @see net.dv8tion.jda.core.JDA#removeEventListener(Object...) JDA.removeEventListener(Object...)
*/
public DefaultShardManagerBuilder removeEventListeners(final Collection<Object> listeners)
{
Checks.noneNull(listeners, "listeners");
this.listeners.removeAll(listeners);
return this;
}
/**
* Adds the provided listener provider to the list of listener providers that will be used to create listeners.
* On shard creation (including shard restarts) the provider will have the shard id applied and must return a listener,
* which will be used, along all other listeners, to populate the listeners of the JDA object of that shard.
*
* <br>This uses the {@link net.dv8tion.jda.core.hooks.InterfacedEventManager InterfacedEventListener} by default.
* <br>To switch to the {@link net.dv8tion.jda.core.hooks.AnnotatedEventManager AnnotatedEventManager},
* use {@link #setEventManagerProvider(IntFunction) setEventManager(id -> new AnnotatedEventManager())}.
*
* <p><b>Note:</b> When using the {@link net.dv8tion.jda.core.hooks.InterfacedEventManager InterfacedEventListener} (default),
* given listener(s) <b>must</b> be instance of {@link net.dv8tion.jda.core.hooks.EventListener EventListener}!
*
* @param listenerProvider
* The listener provider to add to the list of listener providers.
*
* @return The DefaultShardManagerBuilder instance. Useful for chaining.
*/
public DefaultShardManagerBuilder addEventListenerProvider(final IntFunction<Object> listenerProvider)
{
return this.addEventListenerProviders(Collections.singleton(listenerProvider));
}
/**
* Adds the provided listener providers to the list of listener providers that will be used to create listeners.
* On shard creation (including shard restarts) each provider will have the shard id applied and must return a listener,
* which will be used, along all other listeners, to populate the listeners of the JDA object of that shard.
*
* <br>This uses the {@link net.dv8tion.jda.core.hooks.InterfacedEventManager InterfacedEventListener} by default.
* <br>To switch to the {@link net.dv8tion.jda.core.hooks.AnnotatedEventManager AnnotatedEventManager},
* use {@link #setEventManagerProvider(IntFunction) setEventManager(id -> new AnnotatedEventManager())}.
*
* <p><b>Note:</b> When using the {@link net.dv8tion.jda.core.hooks.InterfacedEventManager InterfacedEventListener} (default),
* given listener(s) <b>must</b> be instance of {@link net.dv8tion.jda.core.hooks.EventListener EventListener}!
*
* @param listenerProviders
* The listener provider to add to the list of listener providers.
*
* @return The DefaultShardManagerBuilder instance. Useful for chaining.
*/
public DefaultShardManagerBuilder addEventListenerProviders(final Collection<IntFunction<Object>> listenerProviders)
{
Checks.noneNull(listenerProviders, "listener providers");
this.listenerProviders.addAll(listenerProviders);
return this;
}
/**
* Removes the provided listener provider from the list of listener providers.
*
* @param listenerProvider
* The listener provider to remove from the list of listener providers.
*
* @return The DefaultShardManagerBuilder instance. Useful for chaining.
*/
public DefaultShardManagerBuilder removeEventListenerProvider(final IntFunction<Object> listenerProvider)
{
return this.removeEventListenerProviders(Collections.singleton(listenerProvider));
}
/**
* Removes all provided listener providers from the list of listener providers.
*
* @param listenerProviders
* The listener provider(s) to remove from the list of listener providers.
*
* @return The DefaultShardManagerBuilder instance. Useful for chaining.
*/
public DefaultShardManagerBuilder removeEventListenerProviders(final Collection<IntFunction<Object>> listenerProviders)
{
Checks.noneNull(listenerProviders, "listener providers");
this.listenerProviders.removeAll(listenerProviders);
return this;
}
/**
* Enables/Disables Voice functionality.
* <br>This is useful, if your current system doesn't support Voice and you do not need it.
*
* <p>Default: <b>true (enabled)</b>
*
* @param enabled
* True - enables voice support.
*
* @return The DefaultShardManagerBuilder instance. Useful for chaining.
*/
public DefaultShardManagerBuilder setAudioEnabled(final boolean enabled)
{
this.enableVoice = enabled;
return this;
}
/**
* Changes the factory used to create {@link net.dv8tion.jda.core.audio.factory.IAudioSendSystem IAudioSendSystem}
* objects which handle the sending loop for audio packets.
* <br>By default, JDA uses {@link net.dv8tion.jda.core.audio.factory.DefaultSendFactory DefaultSendFactory}.
*
* @param factory
* The new {@link net.dv8tion.jda.core.audio.factory.IAudioSendFactory IAudioSendFactory} to be used
* when creating new {@link net.dv8tion.jda.core.audio.factory.IAudioSendSystem} objects.
*
* @return The DefaultShardManagerBuilder instance. Useful for chaining.
*/
public DefaultShardManagerBuilder setAudioSendFactory(final IAudioSendFactory factory)
{
this.audioSendFactory = factory;
return this;
}
/**
* Sets whether or not JDA should try to reconnect if a connection-error is encountered.
* <br>This will use an incremental reconnect (timeouts are increased each time an attempt fails).
*
* Default: <b>true (enabled)</b>
*
* @param autoReconnect
* If true - enables autoReconnect
*
* @return The DefaultShardManagerBuilder instance. Useful for chaining.
*/
public DefaultShardManagerBuilder setAutoReconnect(final boolean autoReconnect)
{
this.autoReconnect = autoReconnect;
return this;
}
/**
* If enabled, JDA will separate the bulk delete event into individual delete events, but this isn't as efficient as
* handling a single event would be. It is recommended that BulkDelete Splitting be disabled and that the developer
* should instead handle the {@link net.dv8tion.jda.core.events.message.MessageBulkDeleteEvent MessageBulkDeleteEvent}.
*
* <p>Default: <b>true (enabled)</b>
*
* @param enabled
* True - The MESSAGE_DELETE_BULK will be split into multiple individual MessageDeleteEvents.
*
* @return The DefaultShardManagerBuilder instance. Useful for chaining.
*/
public DefaultShardManagerBuilder setBulkDeleteSplittingEnabled(final boolean enabled)
{
this.enableBulkDeleteSplitting = enabled;
return this;
}
/**
* Sets the core pool size for the global JDA {@link java.util.concurrent.ScheduledExecutorService ScheduledExecutorService}
* which is used in various locations throughout the JDA instance created by this ShardManager. (Default: 5)
* <br>Note: This has no effect if you set a pool using
* {@link #setRateLimitPool(ScheduledExecutorService)} or {@link #setRateLimitPoolProvider(ThreadPoolProvider)}.
*
* @param size
* The core pool size for the global JDA executor
*
* @throws java.lang.IllegalArgumentException
* If the specified core pool size is not positive
*
* @return The DefaultShardManagerBuilder instance. Useful for chaining.
*/
public DefaultShardManagerBuilder setCorePoolSize(int size)
{
Checks.positive(size, "Core pool size");
this.corePoolSize = size;
return this;
}
/**
* Enables/Disables the use of a Shutdown hook to clean up the ShardManager and it's JDA instances.
* <br>When the Java program closes shutdown hooks are run. This is used as a last-second cleanup
* attempt by JDA to properly close connections.
*
* <p>Default: <b>true (enabled)</b>
*
* @param enable
* True (default) - use shutdown hook to clean up the ShardManager and it's JDA instances if the Java program is closed.
*
* @return The DefaultShardManagerBuilder instance. Useful for chaining.
*/
public DefaultShardManagerBuilder setEnableShutdownHook(final boolean enable)
{
this.enableShutdownHook = enable;
return this;
}
/**
* Changes the internally used EventManager.
* <br>There are 2 provided Implementations:
* <ul>
* <li>{@link net.dv8tion.jda.core.hooks.InterfacedEventManager InterfacedEventManager} which uses the Interface
* {@link net.dv8tion.jda.core.hooks.EventListener EventListener} (tip: use the {@link net.dv8tion.jda.core.hooks.ListenerAdapter ListenerAdapter}).
* <br>This is the default EventManager.</li>
*
* <li>{@link net.dv8tion.jda.core.hooks.AnnotatedEventManager AnnotatedEventManager} which uses the Annotation
* {@link net.dv8tion.jda.core.hooks.SubscribeEvent @SubscribeEvent} to mark the methods that listen for events.</li>
* </ul>
* <br>You can also create your own EventManager (See {@link net.dv8tion.jda.core.hooks.IEventManager}).
*
* @param manager
* The new {@link net.dv8tion.jda.core.hooks.IEventManager} to use.
*
* @return The DefaultShardManagerBuilder instance. Useful for chaining.
*
* @deprecated Use {@link #setEventManagerProvider(IntFunction)} instead
*/
@Deprecated
@DeprecatedSince("3.8.1")
@ReplaceWith("setEventManagerProvider((id) -> manager)")
public DefaultShardManagerBuilder setEventManager(final IEventManager manager)
{
Checks.notNull(manager, "manager");
return setEventManagerProvider((id) -> manager);
}
/**
* Sets a provider to change the internally used EventManager.
* <br>There are 2 provided Implementations:
* <ul>
* <li>{@link net.dv8tion.jda.core.hooks.InterfacedEventManager InterfacedEventManager} which uses the Interface
* {@link net.dv8tion.jda.core.hooks.EventListener EventListener} (tip: use the {@link net.dv8tion.jda.core.hooks.ListenerAdapter ListenerAdapter}).
* <br>This is the default EventManager.</li>
*
* <li>{@link net.dv8tion.jda.core.hooks.AnnotatedEventManager AnnotatedEventManager} which uses the Annotation
* {@link net.dv8tion.jda.core.hooks.SubscribeEvent @SubscribeEvent} to mark the methods that listen for events.</li>
* </ul>
* <br>You can also create your own EventManager (See {@link net.dv8tion.jda.core.hooks.IEventManager}).
*
* @param eventManagerProvider
* A supplier for the new {@link net.dv8tion.jda.core.hooks.IEventManager} to use.
*
* @return The DefaultShardManagerBuilder instance. Useful for chaining.
*/
public DefaultShardManagerBuilder setEventManagerProvider(final IntFunction<? extends IEventManager> eventManagerProvider)
{
Checks.notNull(eventManagerProvider, "eventManagerProvider");
this.eventManagerProvider = eventManagerProvider;
return this;
}
/**
* Sets the {@link net.dv8tion.jda.core.entities.Game Game} for our session.
* <br>This value can be changed at any time in the {@link net.dv8tion.jda.core.managers.Presence Presence} from a JDA instance.
*
* <p><b>Hint:</b> You can create a {@link net.dv8tion.jda.core.entities.Game Game} object using
* {@link net.dv8tion.jda.core.entities.Game#playing(String) Game.playing(String)} or
* {@link net.dv8tion.jda.core.entities.Game#streaming(String, String)} Game.streaming(String, String)}.
*
* @param game
* An instance of {@link net.dv8tion.jda.core.entities.Game Game} (null allowed)
*
* @return The DefaultShardManagerBuilder instance. Useful for chaining.
*
* @see net.dv8tion.jda.core.managers.Presence#setGame(Game)
*/
public DefaultShardManagerBuilder setGame(final Game game)
{
return this.setGameProvider(id -> game);
}
/**
* Sets the {@link net.dv8tion.jda.core.entities.Game Game} for our session.
* <br>This value can be changed at any time in the {@link net.dv8tion.jda.core.managers.Presence Presence} from a JDA instance.
*
* <p><b>Hint:</b> You can create a {@link net.dv8tion.jda.core.entities.Game Game} object using
* {@link net.dv8tion.jda.core.entities.Game#playing(String) Game.playing(String)} or
* {@link net.dv8tion.jda.core.entities.Game#streaming(String, String) Game.streaming(String, String)}.
*
* @param gameProvider
* An instance of {@link net.dv8tion.jda.core.entities.Game Game} (null allowed)
*
* @return The DefaultShardManagerBuilder instance. Useful for chaining.
*
* @see net.dv8tion.jda.core.managers.Presence#setGame(Game)
*/
public DefaultShardManagerBuilder setGameProvider(final IntFunction<? extends Game> gameProvider)
{
this.gameProvider = gameProvider;
return this;
}
/**
* Sets whether or not we should mark our sessions as afk
* <br>This value can be changed at any time using
* {@link DefaultShardManager#setIdle(boolean) DefaultShardManager#setIdleProvider(boolean)}.
*
* @param idle
* boolean value that will be provided with our IDENTIFY packages to mark our sessions as afk or not. <b>(default false)</b>
*
* @return The DefaultShardManagerBuilder instance. Useful for chaining.
*
* @see net.dv8tion.jda.core.managers.Presence#setIdle(boolean)
*/
public DefaultShardManagerBuilder setIdle(final boolean idle)
{
return this.setIdleProvider(id -> idle);
}
/**
* Sets whether or not we should mark our sessions as afk
* <br>This value can be changed at any time using
* {@link DefaultShardManager#setIdle(boolean) DefaultShardManager#setIdleProvider(boolean)}.
*
* @param idleProvider
* boolean value that will be provided with our IDENTIFY packages to mark our sessions as afk or not. <b>(default false)</b>
*
* @return The DefaultShardManagerBuilder instance. Useful for chaining.
*
* @see net.dv8tion.jda.core.managers.Presence#setIdle(boolean)
*/
public DefaultShardManagerBuilder setIdleProvider(final IntFunction<Boolean> idleProvider)
{
this.idleProvider = idleProvider;
return this;
}
/**
* Sets the {@link net.dv8tion.jda.core.OnlineStatus OnlineStatus} our connection will display.
* <br>This value can be changed at any time in the {@link net.dv8tion.jda.core.managers.Presence Presence} from a JDA instance.
*
* <p><b>Note:</b>This will not take affect for {@link net.dv8tion.jda.core.AccountType#CLIENT AccountType.CLIENT}
* if the statusProvider specified in the user_settings is not "online" as it is overriding our identify statusProvider.
*
* @param status
* Not-null OnlineStatus (default online)
*
* @throws IllegalArgumentException
* if the provided OnlineStatus is null or {@link net.dv8tion.jda.core.OnlineStatus#UNKNOWN UNKNOWN}
*
* @return The DefaultShardManagerBuilder instance. Useful for chaining.
*
* @see net.dv8tion.jda.core.managers.Presence#setStatus(OnlineStatus) Presence.setStatusProvider(OnlineStatus)
*/
public DefaultShardManagerBuilder setStatus(final OnlineStatus status)
{
Checks.notNull(status, "status");
Checks.check(status != OnlineStatus.UNKNOWN, "OnlineStatus cannot be unknown!");
return this.setStatusProvider(id -> status);
}
/**
* Sets the {@link net.dv8tion.jda.core.OnlineStatus OnlineStatus} our connection will display.
* <br>This value can be changed at any time in the {@link net.dv8tion.jda.core.managers.Presence Presence} from a JDA instance.
*
* <p><b>Note:</b>This will not take affect for {@link net.dv8tion.jda.core.AccountType#CLIENT AccountType.CLIENT}
* if the statusProvider specified in the user_settings is not "online" as it is overriding our identify statusProvider.
*
* @param statusProvider
* Not-null OnlineStatus (default online)
*
* @throws IllegalArgumentException
* if the provided OnlineStatus is null or {@link net.dv8tion.jda.core.OnlineStatus#UNKNOWN UNKNOWN}
*
* @return The DefaultShardManagerBuilder instance. Useful for chaining.
*
* @see net.dv8tion.jda.core.managers.Presence#setStatus(OnlineStatus) Presence.setStatusProvider(OnlineStatus)
*/
public DefaultShardManagerBuilder setStatusProvider(final IntFunction<OnlineStatus> statusProvider)
{
this.statusProvider = statusProvider;
return this;
}
/**
* Sets the {@link java.util.concurrent.ThreadFactory ThreadFactory} that will be used by the internal executor
* of the ShardManager.
* <p>Note: This will not affect Threads created by any JDA instance.
*
* @param threadFactory
* The ThreadFactory or {@code null} to reset to the default value.
*
* @return The DefaultShardManagerBuilder instance. Useful for chaining.
*/
public DefaultShardManagerBuilder setThreadFactory(final ThreadFactory threadFactory)
{
this.threadFactory = threadFactory;
return this;
}
/**
* Sets the {@link okhttp3.OkHttpClient.Builder Builder} that will be used by JDA's requester.
* This can be used to set things such as connection timeout and proxy.
*
* @param builder
* The new {@link okhttp3.OkHttpClient.Builder OkHttpClient.Builder} to use.
*
* @return The DefaultShardManagerBuilder instance. Useful for chaining.
*/
public DefaultShardManagerBuilder setHttpClientBuilder(OkHttpClient.Builder builder)
{
this.httpClientBuilder = builder;
return this;
}
/**
* Sets the {@link okhttp3.OkHttpClient OkHttpClient} that will be used by JDAs requester.
* <br>This can be used to set things such as connection timeout and proxy.
*
* @param client
* The new {@link okhttp3.OkHttpClient OkHttpClient} to use
*
* @return The DefaultShardManagerBuilder instance. Useful for chaining.
*/
public DefaultShardManagerBuilder setHttpClient(OkHttpClient client)
{
this.httpClient = client;
return this;
}
/**
* Sets the {@link ScheduledExecutorService ScheduledExecutorService} that should be used in
* the JDA rate-limit handler. Changing this can drastically change the JDA behavior for RestAction execution
* and should be handled carefully. <b>Only change this pool if you know what you're doing.</b>
* <br>This will override the rate-limit pool provider set from {@link #setRateLimitPoolProvider(ThreadPoolProvider)}.
* <br><b>This automatically disables the automatic shutdown of the rate-limit pool, you can enable
* it using {@link #setRateLimitPool(ScheduledExecutorService, boolean) setRateLimiPool(executor, true)}</b>
*
* @param pool
* The thread-pool to use for rate-limit handling
*
* @return The DefaultShardManagerBuilder instance. Useful for chaining.
*/
public DefaultShardManagerBuilder setRateLimitPool(ScheduledExecutorService pool)
{
return setRateLimitPool(pool, pool == null);
}
/**
* Sets the {@link ScheduledExecutorService ScheduledExecutorService} that should be used in
* the JDA rate-limit handler. Changing this can drastically change the JDA behavior for RestAction execution
* and should be handled carefully. <b>Only change this pool if you know what you're doing.</b>
* <br>This will override the rate-limit pool provider set from {@link #setRateLimitPoolProvider(ThreadPoolProvider)}.
*
* @param pool
* The thread-pool to use for rate-limit handling
* @param automaticShutdown
* Whether {@link net.dv8tion.jda.core.JDA#shutdown()} should automatically shutdown this pool
*
* @return The DefaultShardManagerBuilder instance. Useful for chaining.
*/
public DefaultShardManagerBuilder setRateLimitPool(ScheduledExecutorService pool, boolean automaticShutdown)
{
return setRateLimitPoolProvider(pool == null ? null : new ThreadPoolProviderImpl<>(pool, automaticShutdown));
}
/**
* Sets the {@link ScheduledExecutorService ScheduledExecutorService} provider that should be used in
* the JDA rate-limit handler. Changing this can drastically change the JDA behavior for RestAction execution
* and should be handled carefully. <b>Only change this pool if you know what you're doing.</b>
*
* @param provider
* The thread-pool provider to use for rate-limit handling
*
* @return The DefaultShardManagerBuilder instance. Useful for chaining.
*/
public DefaultShardManagerBuilder setRateLimitPoolProvider(ThreadPoolProvider<? extends ScheduledExecutorService> provider)
{
this.rateLimitPoolProvider = provider;
return this;
}
/**
* Sets the {@link ScheduledExecutorService ScheduledExecutorService} that should be used for
* the JDA main WebSocket workers.
* <br><b>Only change this pool if you know what you're doing.</b>
* <br>This will override the worker pool provider set from {@link #setGatewayPoolProvider(ThreadPoolProvider)}.
* <br><b>This automatically disables the automatic shutdown of the main-ws pools, you can enable
* it using {@link #setGatewayPool(ScheduledExecutorService, boolean) setGatewayPoolProvider(pool, true)}</b>
*
* @param pool
* The thread-pool to use for main WebSocket workers
*
* @return The DefaultShardManagerBuilder instance. Useful for chaining.
*/
public DefaultShardManagerBuilder setGatewayPool(ScheduledExecutorService pool)
{
return setGatewayPool(pool, pool == null);
}
/**
* Sets the {@link ScheduledExecutorService ScheduledExecutorService} that should be used for
* the JDA main WebSocket workers.
* <br><b>Only change this pool if you know what you're doing.</b>
* <br>This will override the worker pool provider set from {@link #setGatewayPoolProvider(ThreadPoolProvider)}.
*
* @param pool
* The thread-pool to use for main WebSocket workers
* @param automaticShutdown
* Whether {@link net.dv8tion.jda.core.JDA#shutdown()} should automatically shutdown this pool
*
* @return The DefaultShardManagerBuilder instance. Useful for chaining.
*/
public DefaultShardManagerBuilder setGatewayPool(ScheduledExecutorService pool, boolean automaticShutdown)
{
return setGatewayPoolProvider(pool == null ? null : new ThreadPoolProviderImpl<>(pool, automaticShutdown));
}
/**
* Sets the {@link ScheduledExecutorService ScheduledExecutorService} that should be used for
* the JDA main WebSocket workers.
* <br><b>Only change this pool if you know what you're doing.</b>
*
* @param provider
* The thread-pool provider to use for main WebSocket workers
*
* @return The DefaultShardManagerBuilder instance. Useful for chaining.
*/
public DefaultShardManagerBuilder setGatewayPoolProvider(ThreadPoolProvider<? extends ScheduledExecutorService> provider)
{
this.gatewayPoolProvider = provider;
return this;
}
/**
* Sets the {@link ExecutorService ExecutorService} that should be used in
* the JDA callback handler which mostly consists of {@link net.dv8tion.jda.core.requests.RestAction RestAction} callbacks.
* By default JDA will use {@link ForkJoinPool#commonPool()}
* <br><b>Only change this pool if you know what you're doing.
* <br>This automatically disables the automatic shutdown of the callback pools, you can enable
* it using {@link #setCallbackPool(ExecutorService, boolean) setCallbackPool(executor, true)}</b>
*
* @param executor
* The thread-pool to use for callback handling
*
* @return The DefaultShardManagerBuilder instance. Useful for chaining.
*/
public DefaultShardManagerBuilder setCallbackPool(ExecutorService executor)
{
return setCallbackPool(executor, executor == null);
}
/**
* Sets the {@link ExecutorService ExecutorService} that should be used in
* the JDA callback handler which mostly consists of {@link net.dv8tion.jda.core.requests.RestAction RestAction} callbacks.
* By default JDA will use {@link ForkJoinPool#commonPool()}
* <br><b>Only change this pool if you know what you're doing.</b>
*
* @param executor
* The thread-pool to use for callback handling
* @param automaticShutdown
* Whether {@link net.dv8tion.jda.core.JDA#shutdown()} should automatically shutdown this pool
*
* @return The DefaultShardManagerBuilder instance. Useful for chaining.
*/
public DefaultShardManagerBuilder setCallbackPool(ExecutorService executor, boolean automaticShutdown)
{
return setCallbackPoolProvider(executor == null ? null : new ThreadPoolProviderImpl<>(executor, automaticShutdown));
}
/**
* Sets the {@link ExecutorService ExecutorService} that should be used in
* the JDA callback handler which mostly consists of {@link net.dv8tion.jda.core.requests.RestAction RestAction} callbacks.
* By default JDA will use {@link ForkJoinPool#commonPool()}
* <br><b>Only change this pool if you know what you're doing.</b>
*
* @param provider
* The thread-pool provider to use for callback handling
*
* @return The DefaultShardManagerBuilder instance. Useful for chaining.
*/
public DefaultShardManagerBuilder setCallbackPoolProvider(ThreadPoolProvider<? extends ExecutorService> provider)
{
this.callbackPoolProvider = provider;
return this;
}
/**
* Sets the maximum amount of time that JDA will back off to wait when attempting to reconnect the MainWebsocket.
* <br>Provided value must be 32 or greater.
*
* @param maxReconnectDelay
* The maximum amount of time that JDA will wait between reconnect attempts in seconds.
*
* @throws java.lang.IllegalArgumentException
* Thrown if the provided {@code maxReconnectDelay} is less than 32.
*
* @return The DefaultShardManagerBuilder instance. Useful for chaining.
*/
public DefaultShardManagerBuilder setMaxReconnectDelay(final int maxReconnectDelay)
{
Checks.check(maxReconnectDelay >= 32, "Max reconnect delay must be 32 seconds or greater. You provided %d.", maxReconnectDelay);
this.maxReconnectDelay = maxReconnectDelay;
return this;
}
/**
* Whether the Requester should retry when
* a {@link java.net.SocketTimeoutException SocketTimeoutException} occurs.
* <br><b>Default</b>: {@code true}
*
* <p>This value can be changed at any time with {@link net.dv8tion.jda.core.JDA#setRequestTimeoutRetry(boolean) JDA.setRequestTimeoutRetry(boolean)}!
*
* @param retryOnTimeout
* True, if the Request should retry once on a socket timeout
*
* @return The DefaultShardManagerBuilder instance. Useful for chaining.
*/
public DefaultShardManagerBuilder setRequestTimeoutRetry(boolean retryOnTimeout)
{
this.retryOnTimeout = retryOnTimeout;
return this;
}
/**
* Sets the list of shards the {@link DefaultShardManager DefaultShardManager} should contain.
*
* <p><b>This does not have any effect if the total shard count is set to {@code -1} (get recommended shards from discord).</b>
*
* @param shardIds
* The list of shard ids
*
* @return The DefaultShardManagerBuilder instance. Useful for chaining.
*/
public DefaultShardManagerBuilder setShards(final int... shardIds)
{
Checks.notNull(shardIds, "shardIds");
for (int id : shardIds)
{
Checks.notNegative(id, "minShardId");
Checks.check(id < this.shardsTotal, "maxShardId must be lower than shardsTotal");
}
this.shards = Arrays.stream(shardIds).boxed().collect(Collectors.toSet());
return this;
}
/**
* Sets the range of shards the {@link DefaultShardManager DefaultShardManager} should contain.
* This is useful if you want to split your shards between multiple JVMs or servers.
*
* <p><b>This does not have any effect if the total shard count is set to {@code -1} (get recommended shards from discord).</b>
*
* @param minShardId
* The lowest shard id the DefaultShardManager should contain
*
* @param maxShardId
* The highest shard id the DefaultShardManager should contain
*
* @throws IllegalArgumentException
* If either minShardId is negative, maxShardId is lower than shardsTotal or
* minShardId is lower than or equal to maxShardId
*
* @return The DefaultShardManagerBuilder instance. Useful for chaining.
*/
public DefaultShardManagerBuilder setShards(final int minShardId, final int maxShardId)
{
Checks.notNegative(minShardId, "minShardId");
Checks.check(maxShardId < this.shardsTotal, "maxShardId must be lower than shardsTotal");
Checks.check(minShardId <= maxShardId, "minShardId must be lower than or equal to maxShardId");
List<Integer> shards = new ArrayList<>(maxShardId - minShardId + 1);
for (int i = minShardId; i <= maxShardId; i++)
shards.add(i);
this.shards = shards;
return this;
}
/**
* Sets the range of shards the {@link DefaultShardManager DefaultShardManager} should contain.
* This is useful if you want to split your shards between multiple JVMs or servers.
*
* <p><b>This does not have any effect if the total shard count is set to {@code -1} (get recommended shards from discord).</b>
*
* @param shardIds
* The list of shard ids
*
* @throws IllegalArgumentException
* If either minShardId is negative, maxShardId is lower than shardsTotal or
* minShardId is lower than or equal to maxShardId
*
* @return The DefaultShardManagerBuilder instance. Useful for chaining.
*/
public DefaultShardManagerBuilder setShards(Collection<Integer> shardIds)
{
Checks.notNull(shardIds, "shardIds");
for (Integer id : shardIds)
{
Checks.notNegative(id, "minShardId");
Checks.check(id < this.shardsTotal, "maxShardId must be lower than shardsTotal");
}
this.shards = new ArrayList<>(shardIds);
return this;
}
/**
* This will set the total amount of shards the {@link DefaultShardManager DefaultShardManager} should use.
* <p> If this is set to {@code -1} JDA will automatically retrieve the recommended amount of shards from discord (default behavior).
*
* @param shardsTotal
* The number of overall shards or {@code -1} if JDA should use the recommended amount from discord.
*
* @return The DefaultShardManagerBuilder instance. Useful for chaining.
*
* @see #setShards(int, int)
*/
public DefaultShardManagerBuilder setShardsTotal(final int shardsTotal)
{
Checks.check(shardsTotal == -1 || shardsTotal > 0, "shardsTotal must either be -1 or greater than 0");
this.shardsTotal = shardsTotal;
return this;
}
/**
* Sets the token that will be used by the {@link net.dv8tion.jda.bot.sharding.ShardManager ShardManager} instance to log in when
* {@link net.dv8tion.jda.bot.sharding.DefaultShardManagerBuilder#build() build()} is called.
*
* <p>To get a bot token:
* <ol>
* <li>Go to your <a href="https://discordapp.com/developers/applications/me">Discord Applications</a></li>
* <li>Create or select an already existing application</li>
* <li>Verify that it has already been turned into a Bot. If you see the "Create a Bot User" button, click it.</li>
* <li>Click the <i>click to reveal</i> link beside the <b>Token</b> label to show your Bot's {@code token}</li>
* </ol>
*
* @param token
* The token of the account that you would like to login with.
*