/
DefaultShardManagerBuilder.java
774 lines (720 loc) · 34.8 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
/*
* 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.core.OnlineStatus;
import net.dv8tion.jda.core.ShardedRateLimiter;
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 okhttp3.OkHttpClient;
import javax.security.auth.login.LoginException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import java.util.concurrent.ConcurrentMap;
import java.util.concurrent.ThreadFactory;
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 SessionController sessionController = null;
protected IntFunction<ConcurrentMap<String, String>> contextProvider = null;
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 int shardsTotal = -1;
protected int maxReconnectDelay = 900;
protected int corePoolSize = 2;
protected String token = null;
protected IntFunction<Boolean> idleProvider = null;
protected IntFunction<Game> gameProvider = null;
protected IntFunction<OnlineStatus> statusProvider = null;
protected Collection<Integer> shards = null;
protected IEventManager eventManager = null;
protected ShardedRateLimiter shardedRateLimiter = null;
protected OkHttpClient.Builder httpClientBuilder = 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() {}
/**
* 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.
*
* <p><b>Setting this disables the {@link #setShardedRateLimiter(ShardedRateLimiter)} settings.</b>
*
* @param controller
* The {@link net.dv8tion.jda.core.utils.SessionController SessionController} to use
*
* @return The {@link net.dv8tion.jda.bot.sharding.DefaultShardManagerBuilder 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 {@link net.dv8tion.jda.bot.sharding.DefaultShardManagerBuilder 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<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 {@link net.dv8tion.jda.bot.sharding.DefaultShardManagerBuilder 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;
}
/**
* 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 #setEventManager(net.dv8tion.jda.core.hooks.IEventManager) setEventManager(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 {@link net.dv8tion.jda.bot.sharding.DefaultShardManagerBuilder 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 #setEventManager(net.dv8tion.jda.core.hooks.IEventManager) setEventManager(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 {@link net.dv8tion.jda.bot.sharding.DefaultShardManagerBuilder 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 {@link net.dv8tion.jda.bot.sharding.DefaultShardManagerBuilder 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 {@link net.dv8tion.jda.bot.sharding.DefaultShardManagerBuilder 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;
}
/**
* 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 {@link net.dv8tion.jda.bot.sharding.DefaultShardManagerBuilder 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 {@link net.dv8tion.jda.bot.sharding.DefaultShardManagerBuilder 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 {@link net.dv8tion.jda.bot.sharding.DefaultShardManagerBuilder 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 {@link net.dv8tion.jda.bot.sharding.DefaultShardManagerBuilder 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: 2)
*
* @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 {@link net.dv8tion.jda.core.JDABuilder JDABuilder} 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 Return the {@link net.dv8tion.jda.bot.sharding.DefaultShardManagerBuilder 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 {@link net.dv8tion.jda.bot.sharding.DefaultShardManagerBuilder DefaultShardManagerBuilder} instance. Useful for chaining.
*/
public DefaultShardManagerBuilder setEventManager(final IEventManager manager)
{
Checks.notNull(manager, "manager");
this.eventManager = manager;
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 {@link net.dv8tion.jda.bot.sharding.DefaultShardManagerBuilder 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 {@link net.dv8tion.jda.bot.sharding.DefaultShardManagerBuilder DefaultShardManagerBuilder} instance. Useful for chaining.
*
* @see net.dv8tion.jda.core.managers.Presence#setGame(Game)
*/
public DefaultShardManagerBuilder setGameProvider(final IntFunction<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 {@link net.dv8tion.jda.bot.sharding.DefaultShardManagerBuilder 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 {@link net.dv8tion.jda.bot.sharding.DefaultShardManagerBuilder 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 {@link net.dv8tion.jda.bot.sharding.DefaultShardManagerBuilder 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 {@link net.dv8tion.jda.bot.sharding.DefaultShardManagerBuilder 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 {@link net.dv8tion.jda.bot.sharding.DefaultShardManagerBuilder 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 Returns the {@link net.dv8tion.jda.core.JDABuilder JDABuilder} instance. Useful for chaining.
*/
public DefaultShardManagerBuilder setHttpClientBuilder(OkHttpClient.Builder builder)
{
this.httpClientBuilder = builder;
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 {@link net.dv8tion.jda.bot.sharding.DefaultShardManagerBuilder 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 {@link net.dv8tion.jda.core.JDABuilder JDABuilder} instance. Useful for chaining.
*/
public DefaultShardManagerBuilder setRequestTimeoutRetry(boolean retryOnTimeout)
{
this.retryOnTimeout = retryOnTimeout;
return this;
}
/**
* Sets the {@link net.dv8tion.jda.core.ShardedRateLimiter ShardedRateLimiter} that will be used to keep
* track of rate limits across sessions.
* <br>When one shard hits the global rate limit all others will be informed by this value wrapper.
*
* <p>It is recommended to use the same ShardedRateLimiter for all shards and not one each. This is
* similar to {@link net.dv8tion.jda.core.requests.SessionReconnectQueue SessionReconnectQueue}!
*
* <p>When you construct multiple ShardManagers manage your shards it is recommended to use the same ShardedRateLimiter on
* all of them. But it is to be <u>avoided</u> to use the same ShardedRateLimiter for different accounts/tokens!
*
* <p><b>If none is provided the ShardManager will use fall back to JDA's default implementation!</b>
*
* @param shardedRateLimiter
* ShardedRateLimiter used to keep track of cross-session rate limits
*
* @return The {@link net.dv8tion.jda.bot.sharding.DefaultShardManagerBuilder DefaultShardManagerBuilder} instance. Useful for chaining.
*/
@Deprecated
public DefaultShardManagerBuilder setShardedRateLimiter(ShardedRateLimiter shardedRateLimiter)
{
Checks.notNull(shardedRateLimiter, "shardedRateLimiter");
this.shardedRateLimiter = shardedRateLimiter;
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 {@link net.dv8tion.jda.bot.sharding.DefaultShardManagerBuilder 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 {@link net.dv8tion.jda.bot.sharding.DefaultShardManagerBuilder 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 {@link net.dv8tion.jda.bot.sharding.DefaultShardManagerBuilder 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 {@link net.dv8tion.jda.bot.sharding.DefaultShardManagerBuilder 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.
*
* @throws java.lang.IllegalArgumentException
* If the token is either null or empty
*
* @return The {@link net.dv8tion.jda.bot.sharding.DefaultShardManagerBuilder DefaultShardManagerBuilder} instance. Useful for chaining.
*/
public DefaultShardManagerBuilder setToken(final String token)
{
Checks.notBlank(token, "token");
this.token = token;
return this;
}
/**
* Whether the {@link net.dv8tion.jda.bot.sharding.ShardManager ShardManager} should use
* {@link net.dv8tion.jda.core.JDA#shutdownNow() JDA#shutdownNow()} instead of
* {@link net.dv8tion.jda.core.JDA#shutdown() JDA#shutdown()} to shutdown it's shards.
* <br><b>Default</b>: {@code false}
*
* @param useShutdownNow
* Whether the ShardManager should use JDA#shutdown() or not
*
* @return The {@link net.dv8tion.jda.bot.sharding.DefaultShardManagerBuilder DefaultShardManagerBuilder} instance. Useful for chaining.
*
* @see net.dv8tion.jda.core.JDA#shutdown()
* @see net.dv8tion.jda.core.JDA#shutdownNow()
*/
public DefaultShardManagerBuilder setUseShutdownNow(final boolean useShutdownNow)
{
this.useShutdownNow = useShutdownNow;
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 Returns the {@link net.dv8tion.jda.core.JDABuilder JDABuilder} instance. Useful for chaining.
*/
public DefaultShardManagerBuilder setWebsocketFactory(WebSocketFactory factory)
{
this.wsFactory = factory;
return this;
}
/**
* Builds a new {@link net.dv8tion.jda.bot.sharding.ShardManager ShardManager} 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.bot.sharding.ShardManager ShardManager} has not
* finished loading, thus many {@link net.dv8tion.jda.bot.sharding.ShardManager ShardManager} methods have the chance to return incorrect information.
* <br>The main use of this method is to start the JDA connect process and do other things in parallel while startup is
* being performed like database connection or local resource loading.
*
* <p>Note that this method is async and as such will <b>not</b> block until all shards are started.
*
* @throws LoginException
* If the provided token is invalid.
* @throws IllegalArgumentException
* If the provided token is empty or null.
*
* @return A {@link net.dv8tion.jda.bot.sharding.ShardManager ShardManager} instance that has started the login process. It is unknown as
* to whether or not loading has finished when this returns.
*/
public ShardManager build() throws LoginException, IllegalArgumentException
{
final DefaultShardManager manager = new DefaultShardManager(
this.shardsTotal, this.shards, this.sessionController,
this.listeners, this.token, this.eventManager,
this.audioSendFactory, this.gameProvider, this.statusProvider,
this.httpClientBuilder, this.wsFactory, this.threadFactory, this.shardedRateLimiter,
this.maxReconnectDelay, this.corePoolSize, this.enableVoice, this.enableShutdownHook, this.enableBulkDeleteSplitting,
this.autoReconnect, this.idleProvider, this.retryOnTimeout, this.useShutdownNow, this.enableContext, this.contextProvider);
manager.login();
return manager;
}
}