Shimmermare / InviteRoles

A Discord bot to automatically give roles to users who joined through specific invite.
MIT License
18 stars 9 forks source link

Application not exiting on jda exception #14

Open f0o opened 3 years ago

f0o commented 3 years ago

Executing the application without any valid parameters will leave the application in an error state and not exit.

[08:02:37.186][main][DEBUG]: com.shimmermare.inviteroles.InviteRoles: Reading application properties from resource
[08:02:37.187][main][INFO]: com.shimmermare.inviteroles.InviteRoles: Starting InviteRoles Discord Bot version 1.0.1-SNAPSHOT. I'm alive!
[08:02:37.660][OkHttp https://discordapp.com/...][DEBUG]: net.dv8tion.jda.internal.requests.Requester: Received response with following cf-rays: [5f695e796c9f1bfa-OSL]
[08:02:37.974][OkHttp https://discordapp.com/...][DEBUG]: net.dv8tion.jda.internal.requests.Requester: Received response with following cf-rays: [5f695e7a2df51bfa-OSL]
[08:02:38.163][OkHttp https://discordapp.com/...][DEBUG]: net.dv8tion.jda.internal.requests.Requester: Received response with following cf-rays: [5f695e7b990a1bfa-OSL]
[08:02:38.176][main][ERROR]: com.shimmermare.inviteroles.InviteRoles: Unable to login to Discord
javax.security.auth.login.LoginException: The provided token is invalid!
    at net.dv8tion.jda.internal.JDAImpl.verifyToken(JDAImpl.java:340)
    at net.dv8tion.jda.internal.JDAImpl.verifyToken(JDAImpl.java:277)
    at net.dv8tion.jda.internal.JDAImpl.login(JDAImpl.java:220)
    at net.dv8tion.jda.internal.JDAImpl.login(JDAImpl.java:188)
    at net.dv8tion.jda.api.JDABuilder.build(JDABuilder.java:914)
    at com.shimmermare.inviteroles.InviteRoles.run(InviteRoles.java:108)
    at com.shimmermare.inviteroles.InviteRoles.main(InviteRoles.java:258)
[08:02:38.176][main][INFO]: com.shimmermare.inviteroles.InviteRoles: Application terminated from console. Goodbye!
Exception in thread "main" java.lang.NullPointerException: Cannot invoke "net.dv8tion.jda.api.JDA.shutdown()" because "this.jda" is null
    at com.shimmermare.inviteroles.InviteRoles.shutdown(InviteRoles.java:187)
    at com.shimmermare.inviteroles.InviteRoles.main(InviteRoles.java:264)

From this point forward the application still runs but isn't doing anything. It would be a lot better if it would just exit and post a non-zero exit code.

f0o commented 3 years ago
0.28s user 0.17s system 0% cpu 30:09.96 total

After 30m I decided to just ^C it..

f0o commented 3 years ago
diff --git a/src/main/java/com/shimmermare/inviteroles/InviteRoles.java b/src/main/java/com/shimmermare/inviteroles/InviteRoles.java
index e9fcd3f..6e2c906 100644
--- a/src/main/java/com/shimmermare/inviteroles/InviteRoles.java
+++ b/src/main/java/com/shimmermare/inviteroles/InviteRoles.java
@@ -110,7 +110,7 @@ public class InviteRoles
         catch (LoginException e)
         {
             LOGGER.error("Unable to login to Discord", e);
-            return;
+            System.exit(-1);
         }

         try
@@ -120,7 +120,7 @@ public class InviteRoles
         catch (InterruptedException e)
         {
             LOGGER.error("Failed to await JDA ready", e);
-            return;
+            System.exit(-1);
         }

         User selfUser = jda.getSelfUser();

fixes it

f0o commented 3 years ago

A proper fix would be to try-catch the shutdown() function. However the whole logic of the codepaths are very off in my opinion.

It should not be considered running until the login succeeded and jda returns ready which means the whole run() function should be placed somewhere else and allow for an early exit.