diff --git a/README.md b/README.md index e54cdd8092..a98c1be934 100644 --- a/README.md +++ b/README.md @@ -144,7 +144,7 @@ configure it on the `JDABuilder` with `setEventManager(...)`. public class ReadyListener implements EventListener { public static void main(String[] args) - throws LoginException, InterruptedException + throws InterruptedException { // Note: It is important to register your ReadyListener before building JDA jda = JDABuilder.createDefault("token") @@ -170,7 +170,6 @@ public class ReadyListener implements EventListener public class MessageListener extends ListenerAdapter { public static void main(String[] args) - throws LoginException { JDA jda = JDABuilder.createDefault("token") .enableIntents(GatewayIntent.MESSAGE_CONTENT) // enables explicit access to message.getContentDisplay() @@ -204,7 +203,7 @@ public class MessageListener extends ListenerAdapter ```java public class Bot extends ListenerAdapter { - public static void main(String[] args) throws LoginException + public static void main(String[] args) { if (args.length < 1) { System.out.println("You have to provide a token as first argument!"); @@ -241,7 +240,7 @@ public class Bot extends ListenerAdapter ```java public class Bot extends ListenerAdapter { - public static void main(String[] args) throws LoginException + public static void main(String[] args) { if (args.length < 1) { System.out.println("You have to provide a token as first argument!"); diff --git a/src/examples/java/AudioEchoExample.java b/src/examples/java/AudioEchoExample.java index 557fea7d54..a3a6c97a20 100644 --- a/src/examples/java/AudioEchoExample.java +++ b/src/examples/java/AudioEchoExample.java @@ -26,7 +26,6 @@ import net.dv8tion.jda.api.requests.GatewayIntent; import net.dv8tion.jda.api.utils.cache.CacheFlag; -import javax.security.auth.login.LoginException; import java.nio.ByteBuffer; import java.util.EnumSet; import java.util.List; @@ -35,7 +34,7 @@ public class AudioEchoExample extends ListenerAdapter { - public static void main(String[] args) throws LoginException + public static void main(String[] args) { if (args.length == 0) { diff --git a/src/examples/java/MessageLoggerExample.java b/src/examples/java/MessageLoggerExample.java index fb9360499e..089a421b69 100644 --- a/src/examples/java/MessageLoggerExample.java +++ b/src/examples/java/MessageLoggerExample.java @@ -120,11 +120,6 @@ public static void main(String[] args) throws IOException // Now we can access the fully loaded cache and show some statistics or do other cache dependent things System.out.println("Guilds: " + jda.getGuildCache().size()); } - catch (LoginException e) - { - // This is thrown if the token is invalid or incorrectly formatted - e.printStackTrace(); - } catch (InterruptedException e) { // Thrown if the awaitReady() call is interrupted diff --git a/src/examples/java/SlashBotExample.java b/src/examples/java/SlashBotExample.java index a7014d0e8f..fec4195a4c 100644 --- a/src/examples/java/SlashBotExample.java +++ b/src/examples/java/SlashBotExample.java @@ -32,14 +32,13 @@ import net.dv8tion.jda.api.requests.GatewayIntent; import net.dv8tion.jda.api.requests.restaction.CommandListUpdateAction; -import javax.security.auth.login.LoginException; import java.util.EnumSet; import static net.dv8tion.jda.api.interactions.commands.OptionType.*; public class SlashBotExample extends ListenerAdapter { - public static void main(String[] args) throws LoginException + public static void main(String[] args) { JDA jda = JDABuilder.createLight("BOT_TOKEN_HERE", EnumSet.noneOf(GatewayIntent.class)) // slash commands don't need any intents .addEventListeners(new SlashBotExample()) diff --git a/src/main/java/net/dv8tion/jda/api/JDABuilder.java b/src/main/java/net/dv8tion/jda/api/JDABuilder.java index fb266625b3..f0c7c20ad3 100644 --- a/src/main/java/net/dv8tion/jda/api/JDABuilder.java +++ b/src/main/java/net/dv8tion/jda/api/JDABuilder.java @@ -29,6 +29,7 @@ import net.dv8tion.jda.internal.managers.PresenceImpl; import net.dv8tion.jda.internal.utils.Checks; import net.dv8tion.jda.internal.utils.IOUtil; +import net.dv8tion.jda.api.exceptions.InvalidTokenException; import net.dv8tion.jda.internal.utils.config.AuthorizationConfig; import net.dv8tion.jda.internal.utils.config.MetaConfig; import net.dv8tion.jda.internal.utils.config.SessionConfig; @@ -39,7 +40,6 @@ import javax.annotation.CheckReturnValue; import javax.annotation.Nonnull; import javax.annotation.Nullable; -import javax.security.auth.login.LoginException; import java.util.*; import java.util.concurrent.*; import java.util.stream.Collectors; @@ -1740,7 +1740,7 @@ public JDABuilder setMaxBufferSize(int bufferSize) * {@link net.dv8tion.jda.api.hooks.EventListener EventListener} to listen for the * {@link net.dv8tion.jda.api.events.ReadyEvent ReadyEvent}. * - * @throws LoginException + * @throws InvalidTokenException * If the provided token is invalid. * @throws IllegalArgumentException * If the provided token is empty or null. Or the provided intents/cache configuration is not possible. @@ -1751,7 +1751,7 @@ public JDABuilder setMaxBufferSize(int bufferSize) * @see net.dv8tion.jda.api.JDA#awaitReady() */ @Nonnull - public JDA build() throws LoginException + public JDA build() { checkIntents(); OkHttpClient httpClient = this.httpClient; diff --git a/src/main/java/net/dv8tion/jda/api/exceptions/InvalidTokenException.java b/src/main/java/net/dv8tion/jda/api/exceptions/InvalidTokenException.java new file mode 100644 index 0000000000..854c8a2d60 --- /dev/null +++ b/src/main/java/net/dv8tion/jda/api/exceptions/InvalidTokenException.java @@ -0,0 +1,46 @@ +/* + * Copyright 2015 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.exceptions; + +/** + * Indicates that an invalid token was given when trying to login the Discord API + * + * */ +public class InvalidTokenException extends RuntimeException +{ + + /** + * Constructs an InvalidTokenException with no detail message. + */ + public InvalidTokenException() + { + super(); + } + + /** + * Constructs an InvalidTokenException with the + * specified detail message. + * + * @param message + * The detail message. + */ + public InvalidTokenException(String message) + { + super(message); + } + +} diff --git a/src/main/java/net/dv8tion/jda/api/sharding/DefaultShardManager.java b/src/main/java/net/dv8tion/jda/api/sharding/DefaultShardManager.java index 5296d03795..3df7d61bd9 100644 --- a/src/main/java/net/dv8tion/jda/api/sharding/DefaultShardManager.java +++ b/src/main/java/net/dv8tion/jda/api/sharding/DefaultShardManager.java @@ -32,6 +32,7 @@ import net.dv8tion.jda.internal.requests.RestActionImpl; import net.dv8tion.jda.internal.requests.Route; import net.dv8tion.jda.internal.utils.Checks; +import net.dv8tion.jda.api.exceptions.InvalidTokenException; import net.dv8tion.jda.internal.utils.JDALogger; import net.dv8tion.jda.internal.utils.UnlockHook; import net.dv8tion.jda.internal.utils.cache.ShardCacheViewImpl; @@ -45,7 +46,6 @@ import javax.annotation.Nonnull; import javax.annotation.Nullable; -import javax.security.auth.login.LoginException; import java.util.Arrays; import java.util.Collection; import java.util.EnumSet; @@ -257,9 +257,9 @@ public ShardCacheView getShardCache() } @Override - public void login() throws LoginException + public void login() { - // building the first one in the current thread ensures that LoginException and IllegalArgumentException can be thrown on login + // building the first one in the current thread ensures that InvalidTokenException and IllegalArgumentException can be thrown on login JDAImpl jda = null; try { @@ -448,7 +448,7 @@ protected void processQueue() LOG.error("Caught an exception in queue processing thread", e); return; } - catch (LoginException e) + catch (InvalidTokenException e) { // this can only happen if the token has been changed // in this case the ShardManager will just shutdown itself as there currently is no way of hot-swapping the token on a running JDA instance. @@ -472,7 +472,7 @@ protected void processQueue() } } - protected JDAImpl buildInstance(final int shardId) throws LoginException + protected JDAImpl buildInstance(final int shardId) { OkHttpClient httpClient = sessionConfig.getHttpClient(); if (httpClient == null) @@ -543,34 +543,25 @@ protected JDAImpl buildInstance(final int shardId) throws LoginException if (this.gatewayURL == null) { - try + SessionController.ShardedGateway gateway = jda.getShardedGateway(); + this.sessionConfig.getSessionController().setConcurrency(gateway.getConcurrency()); + this.gatewayURL = gateway.getUrl(); + if (this.gatewayURL == null) + LOG.error("Acquired null gateway url from SessionController"); + else + LOG.info("Login Successful!"); + + if (getShardsTotal() == -1) { - SessionController.ShardedGateway gateway = jda.getShardedGateway(); - this.sessionConfig.getSessionController().setConcurrency(gateway.getConcurrency()); - this.gatewayURL = gateway.getUrl(); - if (this.gatewayURL == null) - LOG.error("Acquired null gateway url from SessionController"); - else - LOG.info("Login Successful!"); + shardingConfig.setShardsTotal(gateway.getShardTotal()); + this.shards = new ShardCacheViewImpl(getShardsTotal()); - if (getShardsTotal() == -1) + synchronized (queue) { - shardingConfig.setShardsTotal(gateway.getShardTotal()); - this.shards = new ShardCacheViewImpl(getShardsTotal()); - - synchronized (queue) - { - for (int i = 0; i < getShardsTotal(); i++) - queue.add(i); - } + for (int i = 0; i < getShardsTotal(); i++) + queue.add(i); } } - catch (CompletionException e) - { - if (e.getCause() instanceof LoginException) - throw (LoginException) e.getCause(); // complete() can't throw this because its a checked-exception so we have to unwrap it first - throw e; - } } final JDA.ShardInfo shardInfo = new JDA.ShardInfo(shardId, getShardsTotal()); diff --git a/src/main/java/net/dv8tion/jda/api/sharding/DefaultShardManagerBuilder.java b/src/main/java/net/dv8tion/jda/api/sharding/DefaultShardManagerBuilder.java index 44a9385a72..f131f6ccea 100644 --- a/src/main/java/net/dv8tion/jda/api/sharding/DefaultShardManagerBuilder.java +++ b/src/main/java/net/dv8tion/jda/api/sharding/DefaultShardManagerBuilder.java @@ -25,6 +25,7 @@ import net.dv8tion.jda.api.hooks.VoiceDispatchInterceptor; import net.dv8tion.jda.api.requests.GatewayIntent; import net.dv8tion.jda.api.requests.RestAction; +import net.dv8tion.jda.api.exceptions.InvalidTokenException; import net.dv8tion.jda.api.utils.ChunkingFilter; import net.dv8tion.jda.api.utils.Compression; import net.dv8tion.jda.api.utils.MemberCachePolicy; @@ -40,7 +41,6 @@ import javax.annotation.CheckReturnValue; import javax.annotation.Nonnull; import javax.annotation.Nullable; -import javax.security.auth.login.LoginException; import java.util.*; import java.util.concurrent.*; import java.util.function.IntFunction; @@ -2142,7 +2142,7 @@ public DefaultShardManagerBuilder setMaxBufferSize(int bufferSize) * *

Note that this method is async and as such will not block until all shards are started. * - * @throws LoginException + * @throws InvalidTokenException * If the provided token is invalid. * @throws IllegalArgumentException * If the provided token is empty or null. Or the provided intents/cache configuration is not possible. @@ -2153,7 +2153,7 @@ public DefaultShardManagerBuilder setMaxBufferSize(int bufferSize) * to whether or not loading has finished when this returns. */ @Nonnull - public ShardManager build() throws LoginException, IllegalArgumentException + public ShardManager build() throws IllegalArgumentException { return build(true); } @@ -2171,7 +2171,7 @@ public ShardManager build() throws LoginException, IllegalArgumentException * Whether the login process will be started. If this is false, then you will need to manually call * {@link net.dv8tion.jda.api.sharding.ShardManager#login()} to start it. * - * @throws LoginException + * @throws InvalidTokenException * If the provided token is invalid and {@code login} is true * @throws IllegalArgumentException * If the provided token is empty or null. Or the provided intents/cache configuration is not possible. @@ -2181,7 +2181,7 @@ public ShardManager build() throws LoginException, IllegalArgumentException * finished when this returns. */ @Nonnull - public ShardManager build(boolean login) throws LoginException, IllegalArgumentException + public ShardManager build(boolean login) throws IllegalArgumentException { checkIntents(); boolean useShutdownNow = shardingFlags.contains(ShardingConfigFlag.SHUTDOWN_NOW); diff --git a/src/main/java/net/dv8tion/jda/api/sharding/ShardManager.java b/src/main/java/net/dv8tion/jda/api/sharding/ShardManager.java index cb5a4f63f1..17b924e36b 100644 --- a/src/main/java/net/dv8tion/jda/api/sharding/ShardManager.java +++ b/src/main/java/net/dv8tion/jda/api/sharding/ShardManager.java @@ -32,11 +32,12 @@ import net.dv8tion.jda.internal.requests.RestActionImpl; import net.dv8tion.jda.internal.requests.Route; import net.dv8tion.jda.internal.utils.Checks; +import net.dv8tion.jda.api.exceptions.InvalidTokenException; import javax.annotation.CheckReturnValue; import javax.annotation.Nonnull; import javax.annotation.Nullable; -import javax.security.auth.login.LoginException; + import java.util.*; import java.util.function.Function; import java.util.function.IntFunction; @@ -1155,9 +1156,9 @@ default void setStatusProvider(@Nullable final IntFunction statusP /** * Initializes and starts all shards. This should only be called once. * - * @throws LoginException + * @throws InvalidTokenException * If the provided token is invalid. */ - void login() throws LoginException; + void login(); } diff --git a/src/main/java/net/dv8tion/jda/api/utils/SessionControllerAdapter.java b/src/main/java/net/dv8tion/jda/api/utils/SessionControllerAdapter.java index 7ab6d3f711..172616eed0 100644 --- a/src/main/java/net/dv8tion/jda/api/utils/SessionControllerAdapter.java +++ b/src/main/java/net/dv8tion/jda/api/utils/SessionControllerAdapter.java @@ -22,6 +22,7 @@ import net.dv8tion.jda.api.exceptions.AccountTypeException; import net.dv8tion.jda.api.requests.Request; import net.dv8tion.jda.api.requests.Response; +import net.dv8tion.jda.api.exceptions.InvalidTokenException; import net.dv8tion.jda.api.utils.data.DataObject; import net.dv8tion.jda.internal.requests.RestActionImpl; import net.dv8tion.jda.internal.requests.Route; @@ -29,7 +30,6 @@ import org.slf4j.Logger; import javax.annotation.Nonnull; -import javax.security.auth.login.LoginException; import java.util.Queue; import java.util.concurrent.ConcurrentLinkedQueue; import java.util.concurrent.TimeUnit; @@ -99,7 +99,7 @@ public void handleResponse(Response response, Request request) else if (response.code == 401) { api.shutdownNow(); - request.onFailure(new LoginException("The provided token is invalid!")); + request.onFailure(new InvalidTokenException("The provided token is invalid!")); } else { diff --git a/src/main/java/net/dv8tion/jda/internal/JDAImpl.java b/src/main/java/net/dv8tion/jda/internal/JDAImpl.java index 798dc7a7ed..060c21552b 100644 --- a/src/main/java/net/dv8tion/jda/internal/JDAImpl.java +++ b/src/main/java/net/dv8tion/jda/internal/JDAImpl.java @@ -34,6 +34,7 @@ import net.dv8tion.jda.api.events.GenericEvent; import net.dv8tion.jda.api.events.StatusChangeEvent; import net.dv8tion.jda.api.exceptions.AccountTypeException; +import net.dv8tion.jda.api.exceptions.InvalidTokenException; import net.dv8tion.jda.api.exceptions.ParsingException; import net.dv8tion.jda.api.exceptions.RateLimitedException; import net.dv8tion.jda.api.hooks.IEventManager; @@ -73,10 +74,7 @@ import net.dv8tion.jda.internal.requests.restaction.CommandEditActionImpl; import net.dv8tion.jda.internal.requests.restaction.CommandListUpdateActionImpl; import net.dv8tion.jda.internal.requests.restaction.GuildActionImpl; -import net.dv8tion.jda.internal.utils.Checks; -import net.dv8tion.jda.internal.utils.Helpers; -import net.dv8tion.jda.internal.utils.JDALogger; -import net.dv8tion.jda.internal.utils.UnlockHook; +import net.dv8tion.jda.internal.utils.*; import net.dv8tion.jda.internal.utils.cache.AbstractCacheView; import net.dv8tion.jda.internal.utils.cache.SnowflakeCacheViewImpl; import net.dv8tion.jda.internal.utils.config.AuthorizationConfig; @@ -88,7 +86,6 @@ import org.slf4j.MDC; import javax.annotation.Nonnull; -import javax.security.auth.login.LoginException; import java.util.*; import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ConcurrentMap; @@ -272,17 +269,17 @@ public void usedPrivateChannel(long id) } } - public int login() throws LoginException + public int login() { return login(null, null, Compression.ZLIB, true, GatewayIntent.ALL_INTENTS, GatewayEncoding.JSON); } - public int login(ShardInfo shardInfo, Compression compression, boolean validateToken, int intents, GatewayEncoding encoding) throws LoginException + public int login(ShardInfo shardInfo, Compression compression, boolean validateToken, int intents, GatewayEncoding encoding) { return login(null, shardInfo, compression, validateToken, intents, encoding); } - public int login(String gatewayUrl, ShardInfo shardInfo, Compression compression, boolean validateToken, int intents, GatewayEncoding encoding) throws LoginException + public int login(String gatewayUrl, ShardInfo shardInfo, Compression compression, boolean validateToken, int intents, GatewayEncoding encoding) { this.shardInfo = shardInfo; threadConfig.init(this::getIdentifierString); @@ -293,7 +290,7 @@ public int login(String gatewayUrl, ShardInfo shardInfo, Compression compression String token = authConfig.getToken(); setStatus(Status.LOGGING_IN); if (token == null || token.isEmpty()) - throw new LoginException("Provided token was null or empty!"); + throw new InvalidTokenException("Provided token was null or empty!"); Map previousContext = null; ConcurrentMap contextMap = metaConfig.getMdcContextMap(); @@ -367,7 +364,7 @@ public void setStatus(Status status) } } - public void verifyToken() throws LoginException + public void verifyToken() { RestActionImpl login = new RestActionImpl(this, Route.Self.GET_SELF.compile()) { @@ -392,7 +389,7 @@ else if (response.code == 401) return; } shutdownNow(); - throw new LoginException("The provided token is invalid!"); + throw new InvalidTokenException("The provided token is invalid!"); } public AuthorizationConfig getAuthorizationConfig()