Skip to content

Commit 3092875

Browse files
Merge pull request discord-jda#654 from DV8FromTheWorld/release/3.6.0
Release 3.6.0
2 parents 3b5687d + 7e4839a commit 3092875

File tree

259 files changed

+6749
-2259
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

259 files changed

+6749
-2259
lines changed

build.gradle

+4-3
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ plugins {
2828
id 'com.github.johnrengelman.shadow' version '2.0.1'
2929
}
3030

31-
def versionObj = new Version(major: 3, minor: 5, revision: 1)
31+
def versionObj = new Version(major: 3, minor: 6, revision: 0)
3232

3333
group = "net.dv8tion"
3434
archivesBaseName = "JDA"
@@ -83,12 +83,13 @@ task sourcesForRelease(type: Copy) {
8383
])
8484
}
8585

86+
8687
compileJava {
88+
options.compilerArgs += ["-Xlint:deprecation", "-Xlint:unchecked"]
89+
options.encoding = 'UTF-8'
8790
source = sourcesForRelease.destinationDir
8891
classpath = sourceSets.main.compileClasspath
8992

90-
options.encoding = 'UTF-8'
91-
9293
dependsOn sourcesForRelease
9394
}
9495

src/main/java/net/dv8tion/jda/bot/sharding/DefaultShardManager.java

+35-2
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,11 @@ public class DefaultShardManager implements ShardManager
102102
*/
103103
protected final List<Object> listeners;
104104

105+
/**
106+
* The event listener providers for new and restarted JDA instances.
107+
*/
108+
protected final List<IntFunction<Object>> listenerProviders;
109+
105110
/**
106111
* The maximum amount of time that JDA will back off to wait when attempting to reconnect the MainWebsocket.
107112
*/
@@ -201,6 +206,11 @@ public class DefaultShardManager implements ShardManager
201206
*/
202207
protected boolean enableMDC;
203208

209+
/**
210+
* Whether to enable transport compression
211+
*/
212+
protected boolean enableCompression;
213+
204214
/**
205215
* Creates a new DefaultShardManager instance.
206216
* @param shardsTotal
@@ -212,6 +222,9 @@ public class DefaultShardManager implements ShardManager
212222
* The {@link net.dv8tion.jda.core.utils.SessionController SessionController}
213223
* @param listeners
214224
* The event listeners for new JDA instances.
225+
* @param listenerProviders
226+
* Providers of event listeners for JDA instances. Each will have the shard id applied to them upon
227+
* shard creation (including shard restarts) and must return an event listener
215228
* @param token
216229
* The token
217230
* @param eventManager
@@ -251,9 +264,12 @@ public class DefaultShardManager implements ShardManager
251264
* Whether MDC should be enabled
252265
* @param contextProvider
253266
* The MDC context provider new JDA instances should use on startup
267+
* @param enableCompression
268+
* Whether to enable transport compression
254269
*/
255270
protected DefaultShardManager(final int shardsTotal, final Collection<Integer> shardIds,
256271
final SessionController controller, final List<Object> listeners,
272+
final List<IntFunction<Object>> listenerProviders,
257273
final String token, final IEventManager eventManager, final IAudioSendFactory audioSendFactory,
258274
final IntFunction<Game> gameProvider, final IntFunction<OnlineStatus> statusProvider,
259275
final OkHttpClient.Builder httpClientBuilder, final WebSocketFactory wsFactory,
@@ -262,10 +278,12 @@ protected DefaultShardManager(final int shardsTotal, final Collection<Integer> s
262278
final boolean enableShutdownHook, final boolean enableBulkDeleteSplitting,
263279
final boolean autoReconnect, final IntFunction<Boolean> idleProvider,
264280
final boolean retryOnTimeout, final boolean useShutdownNow,
265-
final boolean enableMDC, final IntFunction<ConcurrentMap<String, String>> contextProvider)
281+
final boolean enableMDC, final IntFunction<ConcurrentMap<String, String>> contextProvider,
282+
final boolean enableCompression)
266283
{
267284
this.shardsTotal = shardsTotal;
268285
this.listeners = listeners;
286+
this.listenerProviders = listenerProviders;
269287
this.token = token;
270288
this.eventManager = eventManager;
271289
this.audioSendFactory = audioSendFactory;
@@ -286,6 +304,7 @@ protected DefaultShardManager(final int shardsTotal, final Collection<Integer> s
286304
this.useShutdownNow = useShutdownNow;
287305
this.contextProvider = contextProvider;
288306
this.enableMDC = enableMDC;
307+
this.enableCompression = enableCompression;
289308

290309
synchronized (queue)
291310
{
@@ -320,6 +339,19 @@ public void removeEventListener(final Object... listeners)
320339
this.listeners.removeAll(Arrays.asList(listeners));
321340
}
322341

342+
@Override
343+
public void addEventListeners(IntFunction<Object> eventListenerProvider)
344+
{
345+
ShardManager.super.addEventListeners(eventListenerProvider);
346+
this.listenerProviders.add(eventListenerProvider);
347+
}
348+
349+
@Override
350+
public void removeEventListenerProvider(IntFunction<Object> eventListenerProvider)
351+
{
352+
this.listenerProviders.remove(eventListenerProvider);
353+
}
354+
323355
@Override
324356
public int getShardsQueued()
325357
{
@@ -557,6 +589,7 @@ protected JDAImpl buildInstance(final int shardId) throws LoginException, Interr
557589
jda.setAudioSendFactory(this.audioSendFactory);
558590

559591
this.listeners.forEach(jda::addEventListener);
592+
this.listenerProviders.forEach(provider -> jda.addEventListener(provider.apply(shardId)));
560593
jda.setStatus(JDA.Status.INITIALIZED); //This is already set by JDA internally, but this is to make sure the listeners catch it.
561594

562595
// Set the presence information before connecting to have the correct information ready when sending IDENTIFY
@@ -602,7 +635,7 @@ protected JDAImpl buildInstance(final int shardId) throws LoginException, Interr
602635

603636
final JDA.ShardInfo shardInfo = new JDA.ShardInfo(shardId, this.shardsTotal);
604637

605-
final int shardTotal = jda.login(this.gatewayURL, shardInfo);
638+
final int shardTotal = jda.login(this.gatewayURL, shardInfo, this.enableCompression);
606639
if (this.shardsTotal == -1)
607640
this.shardsTotal = shardTotal;
608641

src/main/java/net/dv8tion/jda/bot/sharding/DefaultShardManagerBuilder.java

+103-6
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,7 @@
2525
import okhttp3.OkHttpClient;
2626

2727
import javax.security.auth.login.LoginException;
28-
import java.util.ArrayList;
29-
import java.util.Arrays;
30-
import java.util.Collection;
31-
import java.util.List;
28+
import java.util.*;
3229
import java.util.concurrent.ConcurrentMap;
3330
import java.util.concurrent.ThreadFactory;
3431
import java.util.function.IntFunction;
@@ -46,6 +43,7 @@
4643
public class DefaultShardManagerBuilder
4744
{
4845
protected final List<Object> listeners = new ArrayList<>();
46+
protected final List<IntFunction<Object>> listenerProviders = new ArrayList<>();
4947
protected SessionController sessionController = null;
5048
protected IntFunction<ConcurrentMap<String, String>> contextProvider = null;
5149
protected boolean enableContext = true;
@@ -55,6 +53,7 @@ public class DefaultShardManagerBuilder
5553
protected boolean autoReconnect = true;
5654
protected boolean retryOnTimeout = true;
5755
protected boolean useShutdownNow = false;
56+
protected boolean enableCompression = true;
5857
protected int shardsTotal = -1;
5958
protected int maxReconnectDelay = 900;
6059
protected int corePoolSize = 2;
@@ -138,6 +137,28 @@ public DefaultShardManagerBuilder setContextEnabled(boolean enable)
138137
return this;
139138
}
140139

140+
/**
141+
* Enable stream-compression on the gateway connection,
142+
* this will decrease the amount of used bandwidth for the running bot instance
143+
* for the cost of a few extra cycles for decompression.
144+
* <br><b>Default: true</b>
145+
*
146+
* <p><b>We recommend to keep this enabled unless you have issues with the decompression</b>
147+
* <br>This mode might become obligatory in a future version, do not rely on this switch to stay.
148+
*
149+
* @param enable
150+
* True, if the gateway connection should use compression
151+
*
152+
* @return The {@link net.dv8tion.jda.bot.sharding.DefaultShardManagerBuilder DefaultShardManagerBuilder} instance. Useful for chaining.
153+
*
154+
* @see <a href="https://discordapp.com/developers/docs/topics/gateway#transport-compression" target="_blank">Official Discord Documentation - Transport Compression</a>
155+
*/
156+
public DefaultShardManagerBuilder setCompressionEnabled(boolean enable)
157+
{
158+
this.enableCompression = enable;
159+
return this;
160+
}
161+
141162
/**
142163
* Adds all provided listeners to the list of listeners that will be used to populate the {@link DefaultShardManager DefaultShardManager} object.
143164
* <br>This uses the {@link net.dv8tion.jda.core.hooks.InterfacedEventManager InterfacedEventListener} by default.
@@ -216,6 +237,82 @@ public DefaultShardManagerBuilder removeEventListeners(final Collection<Object>
216237
return this;
217238
}
218239

240+
/**
241+
* Adds the provided listener provider to the list of listener providers that will be used to create listeners.
242+
* On shard creation (including shard restarts) the provider will have the shard id applied and must return a listener,
243+
* which will be used, along all other listeners, to populate the listeners of the JDA object of that shard.
244+
*
245+
* <br>This uses the {@link net.dv8tion.jda.core.hooks.InterfacedEventManager InterfacedEventListener} by default.
246+
* <br>To switch to the {@link net.dv8tion.jda.core.hooks.AnnotatedEventManager AnnotatedEventManager},
247+
* use {@link #setEventManager(net.dv8tion.jda.core.hooks.IEventManager) setEventManager(new AnnotatedEventManager())}.
248+
*
249+
* <p><b>Note:</b> When using the {@link net.dv8tion.jda.core.hooks.InterfacedEventManager InterfacedEventListener} (default),
250+
* given listener(s) <b>must</b> be instance of {@link net.dv8tion.jda.core.hooks.EventListener EventListener}!
251+
*
252+
* @param listenerProvider
253+
* The listener provider to add to the list of listener providers.
254+
*
255+
* @return The {@link net.dv8tion.jda.bot.sharding.DefaultShardManagerBuilder DefaultShardManagerBuilder} instance. Useful for chaining.
256+
*/
257+
public DefaultShardManagerBuilder addEventListenerProvider(final IntFunction<Object> listenerProvider)
258+
{
259+
return this.addEventListenerProviders(Collections.singleton(listenerProvider));
260+
}
261+
262+
/**
263+
* Adds the provided listener providers to the list of listener providers that will be used to create listeners.
264+
* On shard creation (including shard restarts) each provider will have the shard id applied and must return a listener,
265+
* which will be used, along all other listeners, to populate the listeners of the JDA object of that shard.
266+
*
267+
* <br>This uses the {@link net.dv8tion.jda.core.hooks.InterfacedEventManager InterfacedEventListener} by default.
268+
* <br>To switch to the {@link net.dv8tion.jda.core.hooks.AnnotatedEventManager AnnotatedEventManager},
269+
* use {@link #setEventManager(net.dv8tion.jda.core.hooks.IEventManager) setEventManager(new AnnotatedEventManager())}.
270+
*
271+
* <p><b>Note:</b> When using the {@link net.dv8tion.jda.core.hooks.InterfacedEventManager InterfacedEventListener} (default),
272+
* given listener(s) <b>must</b> be instance of {@link net.dv8tion.jda.core.hooks.EventListener EventListener}!
273+
*
274+
* @param listenerProviders
275+
* The listener provider to add to the list of listener providers.
276+
*
277+
* @return The {@link net.dv8tion.jda.bot.sharding.DefaultShardManagerBuilder DefaultShardManagerBuilder} instance. Useful for chaining.
278+
*/
279+
public DefaultShardManagerBuilder addEventListenerProviders(final Collection<IntFunction<Object>> listenerProviders)
280+
{
281+
Checks.noneNull(listenerProviders, "listener providers");
282+
283+
this.listenerProviders.addAll(listenerProviders);
284+
return this;
285+
}
286+
287+
/**
288+
* Removes the provided listener provider from the list of listener providers.
289+
*
290+
* @param listenerProvider
291+
* The listener provider to remove from the list of listener providers.
292+
*
293+
* @return The {@link net.dv8tion.jda.bot.sharding.DefaultShardManagerBuilder DefaultShardManagerBuilder} instance. Useful for chaining.
294+
*/
295+
public DefaultShardManagerBuilder removeEventListenerProvider(final IntFunction<Object> listenerProvider)
296+
{
297+
return this.removeEventListenerProviders(Collections.singleton(listenerProvider));
298+
}
299+
300+
/**
301+
* Removes all provided listener providers from the list of listener providers.
302+
*
303+
* @param listenerProviders
304+
* The listener provider(s) to remove from the list of listener providers.
305+
*
306+
* @return The {@link net.dv8tion.jda.bot.sharding.DefaultShardManagerBuilder DefaultShardManagerBuilder} instance. Useful for chaining.
307+
*/
308+
public DefaultShardManagerBuilder removeEventListenerProviders(final Collection<IntFunction<Object>> listenerProviders)
309+
{
310+
Checks.noneNull(listenerProviders, "listener providers");
311+
312+
this.listenerProviders.removeAll(listenerProviders);
313+
return this;
314+
}
315+
219316
/**
220317
* Enables/Disables Voice functionality.
221318
* <br>This is useful, if your current system doesn't support Voice and you do not need it.
@@ -731,11 +828,11 @@ public ShardManager build() throws LoginException, IllegalArgumentException
731828
{
732829
final DefaultShardManager manager = new DefaultShardManager(
733830
this.shardsTotal, this.shards, this.sessionController,
734-
this.listeners, this.token, this.eventManager,
831+
this.listeners, this.listenerProviders, this.token, this.eventManager,
735832
this.audioSendFactory, this.gameProvider, this.statusProvider,
736833
this.httpClientBuilder, this.wsFactory, this.threadFactory,
737834
this.maxReconnectDelay, this.corePoolSize, this.enableVoice, this.enableShutdownHook, this.enableBulkDeleteSplitting,
738-
this.autoReconnect, this.idleProvider, this.retryOnTimeout, this.useShutdownNow, this.enableContext, this.contextProvider);
835+
this.autoReconnect, this.idleProvider, this.retryOnTimeout, this.useShutdownNow, this.enableContext, this.contextProvider, this.enableCompression);
739836

740837
manager.login();
741838

src/main/java/net/dv8tion/jda/bot/sharding/ShardManager.java

+60
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,66 @@ default void removeEventListener(final Object... listeners)
7676
this.getShardCache().forEach(jda -> jda.removeEventListener(listeners));
7777
}
7878

79+
/**
80+
* Adds listeners provided by the listener provider to each shard to the event-listeners that will be used to handle events.
81+
* The listener provider gets a shard id applied and is expected to return a listener.
82+
*
83+
* <p>Note: when using the {@link net.dv8tion.jda.core.hooks.InterfacedEventManager InterfacedEventListener} (default),
84+
* given listener <b>must</b> be instance of {@link net.dv8tion.jda.core.hooks.EventListener EventListener}!
85+
*
86+
* @param eventListenerProvider
87+
* The provider of listener(s) which will react to events.
88+
*
89+
* @throws java.lang.IllegalArgumentException
90+
* If the provided listener provider or any of the listeners or provides are {@code null}.
91+
*/
92+
default void addEventListeners(final IntFunction<Object> eventListenerProvider)
93+
{
94+
Checks.notNull(eventListenerProvider, "event listener provider");
95+
this.getShardCache().forEach(jda ->
96+
{
97+
Object listener = eventListenerProvider.apply(jda.getShardInfo().getShardId());
98+
if (listener != null) jda.addEventListener(listener);
99+
});
100+
}
101+
102+
/**
103+
* Remove listeners from shards by their id.
104+
* The provider takes shard ids, and returns a collection of listeners that shall be removed from the respective
105+
* shards.
106+
*
107+
* @param eventListenerProvider
108+
* gets shard ids applied and is expected to return a collection of listeners that shall be removed from
109+
* the respective shards
110+
*
111+
* @throws java.lang.IllegalArgumentException
112+
* If the provided event listeners provider is {@code null}.
113+
*/
114+
default void removeEventListeners(final IntFunction<Collection<Object>> eventListenerProvider)
115+
{
116+
Checks.notNull(eventListenerProvider, "event listener provider");
117+
this.getShardCache().forEach(jda ->
118+
jda.removeEventListener(eventListenerProvider.apply(jda.getShardInfo().getShardId()))
119+
);
120+
}
121+
122+
/**
123+
* Remove a listener provider. This will stop further created / restarted shards from getting a listener added by
124+
* that provider.
125+
*
126+
* Default is a no-op for backwards compatibility, see implementations like
127+
* {@link DefaultShardManager#removeEventListenerProvider(IntFunction)} for actual code
128+
*
129+
* @param eventListenerProvider
130+
* The provider of listeners that shall be removed.
131+
*
132+
* @throws java.lang.IllegalArgumentException
133+
* If the provided listener provider is {@code null}.
134+
*/
135+
default void removeEventListenerProvider(IntFunction<Object> eventListenerProvider)
136+
{
137+
}
138+
79139
/**
80140
* Returns the amount of shards queued for (re)connecting.
81141
*

0 commit comments

Comments
 (0)