/
JDABuilder.java
1023 lines (966 loc) · 42.1 KB
/
JDABuilder.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;
import com.neovisionaries.ws.client.WebSocketFactory;
import net.dv8tion.jda.annotations.Incubating;
import net.dv8tion.jda.api.audio.factory.IAudioSendFactory;
import net.dv8tion.jda.api.entities.Activity;
import net.dv8tion.jda.api.exceptions.AccountTypeException;
import net.dv8tion.jda.api.hooks.IEventManager;
import net.dv8tion.jda.api.hooks.VoiceDispatchInterceptor;
import net.dv8tion.jda.api.requests.RestAction;
import net.dv8tion.jda.api.utils.ChunkingFilter;
import net.dv8tion.jda.api.utils.Compression;
import net.dv8tion.jda.api.utils.SessionController;
import net.dv8tion.jda.api.utils.SessionControllerAdapter;
import net.dv8tion.jda.api.utils.cache.CacheFlag;
import net.dv8tion.jda.internal.JDAImpl;
import net.dv8tion.jda.internal.managers.PresenceImpl;
import net.dv8tion.jda.internal.utils.Checks;
import net.dv8tion.jda.internal.utils.config.AuthorizationConfig;
import net.dv8tion.jda.internal.utils.config.MetaConfig;
import net.dv8tion.jda.internal.utils.config.SessionConfig;
import net.dv8tion.jda.internal.utils.config.ThreadingConfig;
import net.dv8tion.jda.internal.utils.config.flags.ConfigFlag;
import okhttp3.OkHttpClient;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import javax.security.auth.login.LoginException;
import java.util.*;
import java.util.concurrent.*;
/**
* Used to create new {@link net.dv8tion.jda.api.JDA} instances. This is also useful for making sure all of
* your {@link net.dv8tion.jda.api.hooks.EventListener EventListeners} are registered
* before {@link net.dv8tion.jda.api.JDA} attempts to log in.
*
* <p>A single JDABuilder can be reused multiple times. Each call to
* {@link net.dv8tion.jda.api.JDABuilder#build() build()}
* creates a new {@link net.dv8tion.jda.api.JDA} instance using the same information.
* This means that you can have listeners easily registered to multiple {@link net.dv8tion.jda.api.JDA} instances.
*/
public class JDABuilder
{
protected final List<Object> listeners;
protected final AccountType accountType;
protected ScheduledExecutorService rateLimitPool = null;
protected boolean shutdownRateLimitPool = true;
protected ScheduledExecutorService mainWsPool = null;
protected boolean shutdownMainWsPool = true;
protected ExecutorService callbackPool = null;
protected boolean shutdownCallbackPool = true;
protected EnumSet<CacheFlag> cacheFlags = EnumSet.allOf(CacheFlag.class);
protected ConcurrentMap<String, String> contextMap = null;
protected SessionController controller = null;
protected VoiceDispatchInterceptor voiceDispatchInterceptor = null;
protected OkHttpClient.Builder httpClientBuilder = null;
protected OkHttpClient httpClient = null;
protected WebSocketFactory wsFactory = null;
protected String token = null;
protected IEventManager eventManager = null;
protected IAudioSendFactory audioSendFactory = null;
protected JDA.ShardInfo shardInfo = null;
protected Compression compression = Compression.ZLIB;
protected Activity activity = null;
protected OnlineStatus status = OnlineStatus.ONLINE;
protected boolean idle = false;
protected int maxReconnectDelay = 900;
protected int largeThreshold = 250;
protected int maxBufferSize = 2048;
protected EnumSet<ConfigFlag> flags = ConfigFlag.getDefault();
protected ChunkingFilter chunkingFilter = ChunkingFilter.ALL;
/**
* Creates a completely empty JDABuilder.
*
* <br>If you use this, you need to set the token using
* {@link net.dv8tion.jda.api.JDABuilder#setToken(String) setToken(String)}
* before calling {@link net.dv8tion.jda.api.JDABuilder#build() build()}
*
* @see #JDABuilder(String)
*/
public JDABuilder()
{
this(AccountType.BOT);
}
/**
* Creates a JDABuilder with the predefined token.
*
* @param token
* The bot token to use
*
* @see #setToken(String)
*/
public JDABuilder(@Nullable String token)
{
this();
setToken(token);
}
/**
* Creates a completely empty JDABuilder.
* <br>If you use this, you need to set the token using
* {@link net.dv8tion.jda.api.JDABuilder#setToken(String) setToken(String)}
* before calling {@link net.dv8tion.jda.api.JDABuilder#build() build()}
*
* @param accountType
* The {@link net.dv8tion.jda.api.AccountType AccountType}.
*
* @throws IllegalArgumentException
* If the given AccountType is {@code null}
*
* @incubating Due to policy changes for the discord API this method may not be provided in a future version
*/
@Incubating
public JDABuilder(@Nonnull AccountType accountType)
{
Checks.notNull(accountType, "accountType");
this.accountType = accountType;
this.listeners = new LinkedList<>();
}
/**
* Whether JDA should fire {@link net.dv8tion.jda.api.events.RawGatewayEvent} for every discord event.
* <br>Default: {@code false}
*
* @param enable
* True, if JDA should fire {@link net.dv8tion.jda.api.events.RawGatewayEvent}.
*
* @return The JDABuilder instance. Useful for chaining.
*
* @since 4.0.0
*/
@Nonnull
public JDABuilder setRawEventsEnabled(boolean enable)
{
return setFlag(ConfigFlag.RAW_EVENTS, enable);
}
/**
* Whether the rate-limit should be relative to the current time plus latency.
* <br>By default we use the {@code X-RateLimit-Reset-After} header to determine when
* a rate-limit is no longer imminent. This has the disadvantage that it might wait longer than needed due
* to the latency which is ignored by the reset-after relative delay.
*
* <p>When disabled, we will use the {@code X-RateLimit-Reset} absolute timestamp instead which accounts for
* latency but requires a properly NTP synchronized clock to be present.
* If your system does have this feature you might gain a little quicker rate-limit handling than the default allows.
*
* <p>Default: <b>true</b>
*
* @param enable
* True, if the relative {@code X-RateLimit-Reset-After} header should be used.
*
* @return The JDABuilder instance. Useful for chaining.
*
* @since 4.1.0
*/
@Nonnull
public JDABuilder setRelativeRateLimit(boolean enable)
{
return setFlag(ConfigFlag.USE_RELATIVE_RATELIMIT, enable);
}
/**
* Flags used to enable selective 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 JDABuilder instance. Useful for chaining.
*
* @see #setDisabledCacheFlags(EnumSet)
*/
@Nonnull
public JDABuilder setEnabledCacheFlags(@Nullable 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 JDABuilder instance. Useful for chaining.
*/
@Nonnull
public JDABuilder setDisabledCacheFlags(@Nullable EnumSet<CacheFlag> flags)
{
return setEnabledCacheFlags(flags == null ? EnumSet.allOf(CacheFlag.class) : EnumSet.complementOf(flags));
}
/**
* Sets the {@link org.slf4j.MDC MDC} mappings 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>If provided with non-null map this automatically enables MDC context using {@link #setContextEnabled(boolean) setContextEnable(true)}!
*
* @param map
* The <b>modifiable</b> context map to use in JDA, or {@code null} to reset
*
* @return The JDABuilder instance. Useful for chaining.
*
* @see <a href="https://www.slf4j.org/api/org/slf4j/MDC.html" target="_blank">MDC Javadoc</a>
* @see #setContextEnabled(boolean)
*/
@Nonnull
public JDABuilder setContextMap(@Nullable ConcurrentMap<String, String> map)
{
this.contextMap = map;
if (map != null)
setContextEnabled(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 JDABuilder 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.concurrent.ConcurrentMap)
*/
@Nonnull
public JDABuilder setContextEnabled(boolean enable)
{
return setFlag(ConfigFlag.MDC_CONTEXT, enable);
}
/**
* Sets the compression algorithm used with 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.
* Compression can be entirely disabled by setting this to {@link net.dv8tion.jda.api.utils.Compression#NONE}.
* <br><b>Default: {@link net.dv8tion.jda.api.utils.Compression#ZLIB}</b>
*
* <p><b>We recommend to keep this on the default 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 compression
* The compression algorithm to use with the gateway connection
*
* @throws java.lang.IllegalArgumentException
* If provided with null
*
* @return The JDABuilder instance. Useful for chaining
*
* @see <a href="https://discordapp.com/developers/docs/topics/gateway#transport-compression" target="_blank">Official Discord Documentation - Transport Compression</a>
*/
@Nonnull
public JDABuilder setCompression(@Nonnull Compression compression)
{
Checks.notNull(compression, "Compression");
this.compression = compression;
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.api.JDA#setRequestTimeoutRetry(boolean) JDA.setRequestTimeoutRetry(boolean)}!
*
* @param retryOnTimeout
* True, if the Request should retry once on a socket timeout
*
* @return The JDABuilder instance. Useful for chaining.
*/
@Nonnull
public JDABuilder setRequestTimeoutRetry(boolean retryOnTimeout)
{
return setFlag(ConfigFlag.RETRY_TIMEOUT, retryOnTimeout);
}
/**
* Sets the token that will be used by the {@link net.dv8tion.jda.api.JDA} instance to log in when
* {@link net.dv8tion.jda.api.JDABuilder#build() build()} is called.
*
* <h2>For {@link net.dv8tion.jda.api.AccountType#BOT}</h2>
* <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.
*
* @return The JDABuilder instance. Useful for chaining.
*/
@Nonnull
public JDABuilder setToken(@Nullable String token)
{
this.token = token;
return this;
}
/**
* Sets the {@link okhttp3.OkHttpClient.Builder Builder} that will be used by JDAs requester.
* <br>This can be used to set things such as connection timeout and proxy.
*
* @param builder
* The new {@link okhttp3.OkHttpClient.Builder Builder} to use
*
* @return The JDABuilder instance. Useful for chaining.
*/
@Nonnull
public JDABuilder setHttpClientBuilder(@Nullable 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 JDABuilder instance. Useful for chaining.
*/
@Nonnull
public JDABuilder setHttpClient(@Nullable OkHttpClient client)
{
this.httpClient = client;
return this;
}
/**
* Sets the {@link com.neovisionaries.ws.client.WebSocketFactory WebSocketFactory} that will be used by JDA's websocket client.
* This can be used to set things such as connection timeout and proxy.
*
* @param factory
* The new {@link com.neovisionaries.ws.client.WebSocketFactory WebSocketFactory} to use.
*
* @return The JDABuilder instance. Useful for chaining.
*/
@Nonnull
public JDABuilder setWebsocketFactory(@Nullable WebSocketFactory factory)
{
this.wsFactory = factory;
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><b>This automatically disables the automatic shutdown of the rate-limit pool, you can enable
* it using {@link #setRateLimitPool(ScheduledExecutorService, boolean) setRateLimitPool(executor, true)}</b>
*
* <p>This is used mostly by the Rate-Limiter to handle backoff delays by using scheduled executions.
* Besides that it is also used by planned execution for {@link net.dv8tion.jda.api.requests.RestAction#queueAfter(long, TimeUnit)}
* and similar methods.
*
* <p>Default: {@link ScheduledThreadPoolExecutor} with 5 threads.
*
* @param pool
* The thread-pool to use for rate-limit handling
*
* @return The JDABuilder instance. Useful for chaining.
*/
@Nonnull
public JDABuilder setRateLimitPool(@Nullable 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>
*
* <p>This is used mostly by the Rate-Limiter to handle backoff delays by using scheduled executions.
* Besides that it is also used by planned execution for {@link net.dv8tion.jda.api.requests.RestAction#queueAfter(long, TimeUnit)}
* and similar methods.
*
* <p>Default: {@link ScheduledThreadPoolExecutor} with 5 threads.
*
* @param pool
* The thread-pool to use for rate-limit handling
* @param automaticShutdown
* Whether {@link JDA#shutdown()} should shutdown this pool
*
* @return The JDABuilder instance. Useful for chaining.
*/
@Nonnull
public JDABuilder setRateLimitPool(@Nullable ScheduledExecutorService pool, boolean automaticShutdown)
{
this.rateLimitPool = pool;
this.shutdownRateLimitPool = automaticShutdown;
return this;
}
/**
* Sets the {@link ScheduledExecutorService ScheduledExecutorService} used by
* the main WebSocket connection for workers. These workers spend most of their lifetime
* sleeping because they only activate for sending messages over the gateway.
* <br><b>Only change this pool if you know what you're doing.
* <br>This automatically disables the automatic shutdown of the main-ws pool, you can enable
* it using {@link #setGatewayPool(ScheduledExecutorService, boolean) setGatewayPool(pool, true)}</b>
*
* <p>This is used to send various forms of session updates such as:
* <ul>
* <li>Voice States - (Dis-)Connecting from channels</li>
* <li>Presence - Changing current activity or online status</li>
* <li>Guild Setup - Requesting Members of newly joined guilds</li>
* <li>Heartbeats - Regular updates to keep the connection alive (usually once a minute)</li>
* </ul>
* When nothing has to be sent the pool will only be used every 500 milliseconds to check the queue for new payloads.
* Once a new payload is sent we switch to "rapid mode" which means more tasks will be submitted until no more payloads
* have to be sent.
*
* <p>Default: {@link ScheduledThreadPoolExecutor} with 1 thread
*
* @param pool
* The thread-pool to use for WebSocket workers
*
* @return The JDABuilder instance. Useful for chaining.
*/
@Nonnull
public JDABuilder setGatewayPool(@Nullable ScheduledExecutorService pool)
{
return setGatewayPool(pool, pool == null);
}
/**
* Sets the {@link ScheduledExecutorService ScheduledExecutorService} used by
* the main WebSocket connection for workers. These workers spend most of their lifetime
* sleeping because they only activate for sending messages over the gateway.
* <br><b>Only change this pool if you know what you're doing.</b>
*
* <p>This is used to send various forms of session updates such as:
* <ul>
* <li>Voice States - (Dis-)Connecting from channels</li>
* <li>Presence - Changing current activity or online status</li>
* <li>Guild Setup - Requesting Members of newly joined guilds</li>
* <li>Heartbeats - Regular updates to keep the connection alive (usually once a minute)</li>
* </ul>
* When nothing has to be sent the pool will only be used every 500 milliseconds to check the queue for new payloads.
* Once a new payload is sent we switch to "rapid mode" which means more tasks will be submitted until no more payloads
* have to be sent.
*
* <p>Default: {@link ScheduledThreadPoolExecutor} with 1 thread
*
* @param pool
* The thread-pool to use for WebSocket workers
* @param automaticShutdown
* Whether {@link JDA#shutdown()} should shutdown this pool
*
* @return The JDABuilder instance. Useful for chaining.
*/
@Nonnull
public JDABuilder setGatewayPool(@Nullable ScheduledExecutorService pool, boolean automaticShutdown)
{
this.mainWsPool = pool;
this.shutdownMainWsPool = automaticShutdown;
return this;
}
/**
* Sets the {@link ExecutorService ExecutorService} that should be used in
* the JDA callback handler which mostly consists of {@link net.dv8tion.jda.api.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 pool, you can enable
* it using {@link #setCallbackPool(ExecutorService, boolean) setCallbackPool(executor, true)}</b>
*
* <p>This is used to handle callbacks of {@link RestAction#queue()}, similarly it is used to
* finish {@link RestAction#submit()} and {@link RestAction#complete()} tasks which build on queue.
*
* <p>Default: {@link ForkJoinPool#commonPool()}
*
* @param executor
* The thread-pool to use for callback handling
*
* @return The JDABuilder instance. Useful for chaining.
*/
@Nonnull
public JDABuilder setCallbackPool(@Nullable 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.api.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>
*
* <p>This is used to handle callbacks of {@link RestAction#queue()}, similarly it is used to
* finish {@link RestAction#submit()} and {@link RestAction#complete()} tasks which build on queue.
*
* <p>Default: {@link ForkJoinPool#commonPool()}
*
* @param executor
* The thread-pool to use for callback handling
* @param automaticShutdown
* Whether {@link JDA#shutdown()} should shutdown this executor
*
* @return The JDABuilder instance. Useful for chaining.
*/
@Nonnull
public JDABuilder setCallbackPool(@Nullable ExecutorService executor, boolean automaticShutdown)
{
this.callbackPool = executor;
this.shutdownCallbackPool = automaticShutdown;
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.api.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 JDABuilder instance. Useful for chaining.
*/
@Nonnull
public JDABuilder setBulkDeleteSplittingEnabled(boolean enabled)
{
return setFlag(ConfigFlag.BULK_DELETE_SPLIT, enabled);
}
/**
* Enables/Disables the use of a Shutdown hook to clean up JDA.
* <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 JDA if the Java program is closed.
*
* @return Return the {@link net.dv8tion.jda.api.JDABuilder JDABuilder } instance. Useful for chaining.
*/
@Nonnull
public JDABuilder setEnableShutdownHook(boolean enable)
{
return setFlag(ConfigFlag.SHUTDOWN_HOOK, enable);
}
/**
* 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 JDABuilder instance. Useful for chaining.
*/
@Nonnull
public JDABuilder setAutoReconnect(boolean autoReconnect)
{
return setFlag(ConfigFlag.AUTO_RECONNECT, autoReconnect);
}
/**
* Changes the internally used EventManager.
* <br>There are 2 provided Implementations:
* <ul>
* <li>{@link net.dv8tion.jda.api.hooks.InterfacedEventManager InterfacedEventManager} which uses the Interface
* {@link net.dv8tion.jda.api.hooks.EventListener EventListener} (tip: use the {@link net.dv8tion.jda.api.hooks.ListenerAdapter ListenerAdapter}).
* <br>This is the default EventManager.</li>
*
* <li>{@link net.dv8tion.jda.api.hooks.AnnotatedEventManager AnnotatedEventManager} which uses the Annotation
* {@link net.dv8tion.jda.api.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.api.hooks.IEventManager}).
*
* @param manager
* The new {@link net.dv8tion.jda.api.hooks.IEventManager} to use.
*
* @return The JDABuilder instance. Useful for chaining.
*/
@Nonnull
public JDABuilder setEventManager(@Nullable IEventManager manager)
{
this.eventManager = manager;
return this;
}
/**
* Changes the factory used to create {@link net.dv8tion.jda.api.audio.factory.IAudioSendSystem IAudioSendSystem}
* objects which handle the sending loop for audio packets.
* <br>By default, JDA uses {@link net.dv8tion.jda.api.audio.factory.DefaultSendFactory DefaultSendFactory}.
*
* @param factory
* The new {@link net.dv8tion.jda.api.audio.factory.IAudioSendFactory IAudioSendFactory} to be used
* when creating new {@link net.dv8tion.jda.api.audio.factory.IAudioSendSystem} objects.
*
* @return The JDABuilder instance. Useful for chaining.
*/
@Nonnull
public JDABuilder setAudioSendFactory(@Nullable IAudioSendFactory factory)
{
this.audioSendFactory = factory;
return this;
}
/**
* Sets whether or not we should mark our session as afk
* <br>This value can be changed at any time in the {@link net.dv8tion.jda.api.managers.Presence Presence} from a JDA instance.
*
* @param idle
* boolean value that will be provided with our IDENTIFY package to mark our session as afk or not. <b>(default false)</b>
*
* @return The JDABuilder instance. Useful for chaining.
*
* @see net.dv8tion.jda.api.managers.Presence#setIdle(boolean) Presence.setIdle(boolean)
*/
@Nonnull
public JDABuilder setIdle(boolean idle)
{
this.idle = idle;
return this;
}
/**
* Sets the {@link net.dv8tion.jda.api.entities.Activity Activity} for our session.
* <br>This value can be changed at any time in the {@link net.dv8tion.jda.api.managers.Presence Presence} from a JDA instance.
*
* <p><b>Hint:</b> You can create a {@link net.dv8tion.jda.api.entities.Activity Activity} object using
* {@link net.dv8tion.jda.api.entities.Activity#playing(String)} or {@link net.dv8tion.jda.api.entities.Activity#streaming(String, String)}.
*
* @param activity
* An instance of {@link net.dv8tion.jda.api.entities.Activity Activity} (null allowed)
*
* @return The JDABuilder instance. Useful for chaining.
*
* @see net.dv8tion.jda.api.managers.Presence#setActivity(net.dv8tion.jda.api.entities.Activity) Presence.setActivity(Activity)
*/
@Nonnull
public JDABuilder setActivity(@Nullable Activity activity)
{
this.activity = activity;
return this;
}
/**
* Sets the {@link net.dv8tion.jda.api.OnlineStatus OnlineStatus} our connection will display.
* <br>This value can be changed at any time in the {@link net.dv8tion.jda.api.managers.Presence Presence} from a JDA instance.
*
* <p><b>Note:</b>This will not take affect for {@link net.dv8tion.jda.api.AccountType#CLIENT AccountType.CLIENT}
* if the status specified in the user_settings is not "online" as it is overriding our identify status.
*
* @param status
* Not-null OnlineStatus (default online)
*
* @throws IllegalArgumentException
* if the provided OnlineStatus is null or {@link net.dv8tion.jda.api.OnlineStatus#UNKNOWN UNKNOWN}
*
* @return The JDABuilder instance. Useful for chaining.
*
* @see net.dv8tion.jda.api.managers.Presence#setStatus(OnlineStatus) Presence.setStatus(OnlineStatus)
*/
@Nonnull
@SuppressWarnings("ConstantConditions") // we have to enforce the nonnull at runtime
public JDABuilder setStatus(@Nonnull OnlineStatus status)
{
if (status == null || status == OnlineStatus.UNKNOWN)
throw new IllegalArgumentException("OnlineStatus cannot be null or unknown!");
this.status = status;
return this;
}
/**
* Adds all provided listeners to the list of listeners that will be used to populate the {@link net.dv8tion.jda.api.JDA JDA} object.
* <br>This uses the {@link net.dv8tion.jda.api.hooks.InterfacedEventManager InterfacedEventListener} by default.
* <br>To switch to the {@link net.dv8tion.jda.api.hooks.AnnotatedEventManager AnnotatedEventManager},
* use {@link #setEventManager(net.dv8tion.jda.api.hooks.IEventManager) setEventManager(new AnnotatedEventManager())}.
*
* <p><b>Note:</b> When using the {@link net.dv8tion.jda.api.hooks.InterfacedEventManager InterfacedEventListener} (default),
* given listener(s) <b>must</b> be instance of {@link net.dv8tion.jda.api.hooks.EventListener EventListener}!
*
* @param listeners
* The listener(s) to add to the list.
*
* @throws java.lang.IllegalArgumentException
* If either listeners or one of it's objects is {@code null}.
*
* @return The JDABuilder instance. Useful for chaining.
*
* @see net.dv8tion.jda.api.JDA#addEventListener(Object...) JDA.addEventListener(Object...)
*/
@Nonnull
public JDABuilder addEventListeners(@Nonnull Object... listeners)
{
Checks.noneNull(listeners, "listeners");
Collections.addAll(this.listeners, listeners);
return this;
}
/**
* Removes all provided listeners from the list of listeners.
*
* @param listeners
* The listener(s) to remove from the list.
*
* @throws java.lang.IllegalArgumentException
* If either listeners or one of it's objects is {@code null}.
*
* @return The JDABuilder instance. Useful for chaining.
*
* @see net.dv8tion.jda.api.JDA#removeEventListener(Object...) JDA.removeEventListener(Object...)
*/
@Nonnull
public JDABuilder removeEventListeners(@Nonnull Object... listeners)
{
Checks.noneNull(listeners, "listeners");
this.listeners.removeAll(Arrays.asList(listeners));
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 JDABuilder instance. Useful for chaining.
*/
@Nonnull
public JDABuilder setMaxReconnectDelay(int maxReconnectDelay)
{
Checks.check(maxReconnectDelay >= 32, "Max reconnect delay must be 32 seconds or greater. You provided %d.", maxReconnectDelay);
this.maxReconnectDelay = maxReconnectDelay;
return this;
}
/**
* This will enable sharding mode for JDA.
* <br>In sharding mode, guilds are split up and assigned one of multiple shards (clients).
* <br>The shardId that receives all stuff related to given bot is calculated as follows: shardId == (guildId {@literal >>} 22) % shardTotal;
* <br><b>PMs are only sent to shard 0.</b>
*
* <p>Please note, that a shard will not know about guilds which are not assigned to it.
*
* <p><b>It is not possible to use sharding with an account for {@link net.dv8tion.jda.api.AccountType#CLIENT AccountType.CLIENT}!</b>
*
* @param shardId
* The id of this shard (starting at 0).
* @param shardTotal
* The number of overall shards.
*
* @throws net.dv8tion.jda.api.exceptions.AccountTypeException
* If this is used on a JDABuilder for {@link net.dv8tion.jda.api.AccountType#CLIENT AccountType.CLIENT}
* @throws java.lang.IllegalArgumentException
* If the provided shard configuration is invalid
* ({@code 0 <= shardId < shardTotal} with {@code shardTotal > 0})
*
* @return The JDABuilder instance. Useful for chaining.
*
* @see net.dv8tion.jda.api.JDA#getShardInfo() JDA.getShardInfo()
* @see net.dv8tion.jda.api.sharding.ShardManager ShardManager
*/
@Nonnull
public JDABuilder useSharding(int shardId, int shardTotal)
{
AccountTypeException.check(accountType, AccountType.BOT);
Checks.notNegative(shardId, "Shard ID");
Checks.positive(shardTotal, "Shard Total");
Checks.check(shardId < shardTotal,
"The shard ID must be lower than the shardTotal! Shard IDs are 0-based.");
shardInfo = new JDA.ShardInfo(shardId, shardTotal);
return this;
}
/**
* Sets the {@link net.dv8tion.jda.api.utils.SessionController SessionController}
* for this JDABuilder 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.
* <br>When {@link #useSharding(int, int)} is enabled, this is set by default.
*
* <p>When set, this allows the builder to build shards with respect to the login ratelimit automatically.
*
* @param controller
* The {@link net.dv8tion.jda.api.utils.SessionController SessionController} to use
*
* @return The JDABuilder instance. Useful for chaining.
*
* @see net.dv8tion.jda.api.utils.SessionControllerAdapter SessionControllerAdapter
*/
@Nonnull
public JDABuilder setSessionController(@Nullable SessionController controller)
{
this.controller = controller;
return this;
}
/**
* Configures a custom voice dispatch handler which handles audio connections.
*
* @param interceptor
* The new voice dispatch handler, or null to use the default
*
* @return The JDABuilder instance. Useful for chaining.
*
* @since 4.0.0
*
* @see VoiceDispatchInterceptor
*/
@Nonnull
public JDABuilder setVoiceDispatchInterceptor(@Nullable VoiceDispatchInterceptor interceptor)
{
this.voiceDispatchInterceptor = interceptor;
return this;
}
/**
* The {@link ChunkingFilter} to filter which guilds should use member chunking.
* <br>By default this uses {@link ChunkingFilter#ALL}.
*
* <p>This filter is useless when {@link #setGuildSubscriptionsEnabled(boolean)} is false.
*
* @param filter
* The filter to apply
*
* @return The JDABuilder instance. Useful for chaining.
*
* @since 4.1.0
*
* @see ChunkingFilter#NONE
* @see ChunkingFilter#include(long...)
* @see ChunkingFilter#exclude(long...)
*/
@Nonnull
public JDABuilder setChunkingFilter(@Nullable ChunkingFilter filter)
{
this.chunkingFilter = filter == null ? ChunkingFilter.ALL : filter;
return this;
}
/**
* Enable typing and presence update events.
* <br>These events cover the majority of traffic happening on the gateway and thus cause a lot
* of bandwidth usage. Disabling these events means the cache for users might become outdated since
* user properties are only updated by presence updates.
* <br>Default: true
*
* <h2>Notice</h2>
* This disables the majority of member cache and related events. If anything in your project
* relies on member state you should keep this enabled.
*
* @param enabled
* True, if guild subscriptions should be enabled
*
* @return The JDABuilder instance. Useful for chaining.
*
* @since 4.1.0
*/
@Nonnull
public JDABuilder setGuildSubscriptionsEnabled(boolean enabled)
{
return setFlag(ConfigFlag.GUILD_SUBSCRIPTIONS, enabled);
}
/**
* Decides the total number of members at which a guild should start to use lazy loading.
* <br>This is limited to a number between 50 and 250 (inclusive).
* If the {@link #setChunkingFilter(ChunkingFilter) chunking filter} is set to {@link ChunkingFilter#ALL}
* this should be set to {@code 250} (default) to minimize the amount of guilds that need to request members.
*
* @param threshold
* The threshold in {@code [50, 250]}
*
* @return The JDABuilder instance. Useful for chaining.
*
* @since 4.1.0
*/
@Nonnull
public JDABuilder setLargeThreshold(int threshold)
{
this.largeThreshold = Math.max(50, Math.min(250, threshold)); // enforce 50 <= t <= 250
return this;
}
/**
* The maximum size, in bytes, of the buffer used for decompressing discord payloads.
* <br>If the maximum buffer size is exceeded a new buffer will be allocated instead.
* <br>Setting this to {@link Integer#MAX_VALUE} would imply the buffer will never be resized unless memory starvation is imminent.
* <br>Setting this to {@code 0} would imply the buffer would need to be allocated again for every payload (not recommended).
*
* <p>Default: {@code 2048}
*
* @param bufferSize
* The maximum size the buffer should allow to retain
*
* @throws IllegalArgumentException
* If the provided buffer size is negative
*
* @return The JDABuilder instance. Useful for chaining.
*/
@Nonnull
public JDABuilder setMaxBufferSize(int bufferSize)
{
Checks.notNegative(bufferSize, "The buffer size");
this.maxBufferSize = bufferSize;
return this;
}
/**
* Builds a new {@link net.dv8tion.jda.api.JDA} instance and uses the provided token to start the login process.
* <br>The login process runs in a different thread, so while this will return immediately, {@link net.dv8tion.jda.api.JDA} has not
* finished loading, thus many {@link net.dv8tion.jda.api.JDA} methods have the chance to return incorrect information.
* For example {@link JDA#getGuilds()} might return an empty list or {@link net.dv8tion.jda.api.JDA#getUserById(long)} might return null
* for arbitrary user IDs.
*
* <p>If you wish to be sure that the {@link net.dv8tion.jda.api.JDA} information is correct, please use
* {@link net.dv8tion.jda.api.JDA#awaitReady() JDA.awaitReady()} or register an
* {@link net.dv8tion.jda.api.hooks.EventListener EventListener} to listen for the
* {@link net.dv8tion.jda.api.events.ReadyEvent ReadyEvent}.
*
* @throws LoginException
* If the provided token is invalid.
* @throws IllegalArgumentException
* If the provided token is empty or null.
*
* @return A {@link net.dv8tion.jda.api.JDA} instance that has started the login process. It is unknown as
* to whether or not loading has finished when this returns.
*
* @see net.dv8tion.jda.api.JDA#awaitReady()
*/
@Nonnull
public JDA build() throws LoginException
{
OkHttpClient httpClient = this.httpClient;
if (httpClient == null)
{
if (this.httpClientBuilder == null)
this.httpClientBuilder = new OkHttpClient.Builder();
httpClient = this.httpClientBuilder.build();
}
WebSocketFactory wsFactory = this.wsFactory == null ? new WebSocketFactory() : this.wsFactory;
if (controller == null && shardInfo != null)
controller = new SessionControllerAdapter();
AuthorizationConfig authConfig = new AuthorizationConfig(accountType, token);
ThreadingConfig threadingConfig = new ThreadingConfig();
threadingConfig.setCallbackPool(callbackPool, shutdownCallbackPool);
threadingConfig.setGatewayPool(mainWsPool, shutdownMainWsPool);
threadingConfig.setRateLimitPool(rateLimitPool, shutdownRateLimitPool);
SessionConfig sessionConfig = new SessionConfig(controller, httpClient, wsFactory, voiceDispatchInterceptor, flags, maxReconnectDelay, largeThreshold);
MetaConfig metaConfig = new MetaConfig(maxBufferSize, contextMap, cacheFlags, flags);
JDAImpl jda = new JDAImpl(authConfig, sessionConfig, threadingConfig, metaConfig);
jda.setChunkingFilter(chunkingFilter);
if (eventManager != null)
jda.setEventManager(eventManager);
if (audioSendFactory != null)