Skip to content

Commit

Permalink
Invalid token exception (#2025)
Browse files Browse the repository at this point in the history
Co-authored-by: Tais993 <49957334+Tais993@users.noreply.github.com>
Co-authored-by: Austin Keener <keeneraustin@yahoo.com>
  • Loading branch information
3 people committed Aug 30, 2022
1 parent c2b439e commit 31b1bfb
Show file tree
Hide file tree
Showing 11 changed files with 92 additions and 65 deletions.
7 changes: 3 additions & 4 deletions README.md
Expand Up @@ -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")
Expand All @@ -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()
Expand Down Expand Up @@ -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!");
Expand Down Expand Up @@ -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!");
Expand Down
3 changes: 1 addition & 2 deletions src/examples/java/AudioEchoExample.java
Expand Up @@ -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;
Expand All @@ -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)
{
Expand Down
5 changes: 0 additions & 5 deletions src/examples/java/MessageLoggerExample.java
Expand Up @@ -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
Expand Down
3 changes: 1 addition & 2 deletions src/examples/java/SlashBotExample.java
Expand Up @@ -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())
Expand Down
6 changes: 3 additions & 3 deletions src/main/java/net/dv8tion/jda/api/JDABuilder.java
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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.
Expand All @@ -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;
Expand Down
@@ -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 <code>InvalidTokenException</code> with no detail message.
*/
public InvalidTokenException()
{
super();
}

/**
* Constructs an <code>InvalidTokenException</code> with the
* specified detail message.
*
* @param message
* The detail message.
*/
public InvalidTokenException(String message)
{
super(message);
}

}
47 changes: 19 additions & 28 deletions src/main/java/net/dv8tion/jda/api/sharding/DefaultShardManager.java
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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
{
Expand Down Expand Up @@ -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.
Expand All @@ -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)
Expand Down Expand Up @@ -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());
Expand Down
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -2142,7 +2142,7 @@ public DefaultShardManagerBuilder setMaxBufferSize(int bufferSize)
*
* <p>Note that this method is async and as such will <b>not</b> 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.
Expand All @@ -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);
}
Expand All @@ -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.
Expand All @@ -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);
Expand Down
7 changes: 4 additions & 3 deletions src/main/java/net/dv8tion/jda/api/sharding/ShardManager.java
Expand Up @@ -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;
Expand Down Expand Up @@ -1155,9 +1156,9 @@ default void setStatusProvider(@Nullable final IntFunction<OnlineStatus> 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();

}
Expand Up @@ -22,14 +22,14 @@
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;
import net.dv8tion.jda.internal.utils.JDALogger;
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;
Expand Down Expand Up @@ -99,7 +99,7 @@ public void handleResponse(Response response, Request<ShardedGateway> 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
{
Expand Down

0 comments on commit 31b1bfb

Please sign in to comment.