From 9daa138a124abde6018a251905cd5a66b3780d87 Mon Sep 17 00:00:00 2001 From: Vankka Date: Sat, 29 Jun 2024 16:01:41 +0300 Subject: [PATCH] Tweak default bot token logic --- .../discordsrv/common/AbstractDiscordSRV.java | 13 ++++ .../subcommand/reload/ReloadResults.java | 1 + .../common/config/connection/BotConfig.java | 4 +- .../connection/jda/JDAConnectionManager.java | 61 ++++++++++--------- 4 files changed, 50 insertions(+), 29 deletions(-) diff --git a/common/src/main/java/com/discordsrv/common/AbstractDiscordSRV.java b/common/src/main/java/com/discordsrv/common/AbstractDiscordSRV.java index dc704687..124ac486 100644 --- a/common/src/main/java/com/discordsrv/common/AbstractDiscordSRV.java +++ b/common/src/main/java/com/discordsrv/common/AbstractDiscordSRV.java @@ -38,6 +38,7 @@ import com.discordsrv.common.config.configurate.manager.ConnectionConfigManager; import com.discordsrv.common.config.configurate.manager.MainConfigManager; import com.discordsrv.common.config.configurate.manager.MessagesConfigManager; import com.discordsrv.common.config.configurate.manager.MessagesConfigSingleManager; +import com.discordsrv.common.config.connection.BotConfig; import com.discordsrv.common.config.connection.ConnectionConfig; import com.discordsrv.common.config.connection.UpdateConfig; import com.discordsrv.common.config.main.MainConfig; @@ -673,6 +674,18 @@ public abstract class AbstractDiscordSRV< results.addAll(moduleManager().reload()); } + if (connectionConfig().bot.token.equals(BotConfig.DEFAULT_TOKEN)) { + logger().info(""); + logger().info("Welcome to DiscordSRV!"); + logger().info(""); + logger().info("To get started with using DiscordSRV please configure a bot token, instructions will be listed below"); + logger().info("You can review and/or disable external services DiscordSRV uses in the " + ConnectionConfig.FILE_NAME + " before adding a bot token"); + logger().info(""); + JDAConnectionManager.invalidToken(this, true); + results.add(ReloadResults.DEFAULT_BOT_TOKEN); + return results; + } + // Update check UpdateConfig updateConfig = connectionConfig().update; if (updateConfig.security.enabled) { diff --git a/common/src/main/java/com/discordsrv/common/command/game/commands/subcommand/reload/ReloadResults.java b/common/src/main/java/com/discordsrv/common/command/game/commands/subcommand/reload/ReloadResults.java index 4733773b..a8e4c31b 100644 --- a/common/src/main/java/com/discordsrv/common/command/game/commands/subcommand/reload/ReloadResults.java +++ b/common/src/main/java/com/discordsrv/common/command/game/commands/subcommand/reload/ReloadResults.java @@ -22,6 +22,7 @@ import com.discordsrv.api.DiscordSRVApi; public enum ReloadResults implements DiscordSRVApi.ReloadResult { FAILED, + DEFAULT_BOT_TOKEN, SUCCESS, SECURITY_FAILED, STORAGE_CONNECTION_FAILED, diff --git a/common/src/main/java/com/discordsrv/common/config/connection/BotConfig.java b/common/src/main/java/com/discordsrv/common/config/connection/BotConfig.java index 417c2d05..4037b651 100644 --- a/common/src/main/java/com/discordsrv/common/config/connection/BotConfig.java +++ b/common/src/main/java/com/discordsrv/common/config/connection/BotConfig.java @@ -24,9 +24,11 @@ import org.spongepowered.configurate.objectmapping.meta.Comment; @ConfigSerializable public class BotConfig { + public static final String DEFAULT_TOKEN = "Token here"; + @Comment("The Discord bot token from https://discord.com/developers/applications\n" + "Requires a connection to: discord.com, gateway.discord.gg, cdn.discordapp.com\n" + "Privacy Policy: https://discord.com/privacy Terms: https://discord.com/developers/docs/policies-and-agreements/developer-terms-of-service") - public String token = "Token here"; + public String token = DEFAULT_TOKEN; } diff --git a/common/src/main/java/com/discordsrv/common/discord/connection/jda/JDAConnectionManager.java b/common/src/main/java/com/discordsrv/common/discord/connection/jda/JDAConnectionManager.java index f3f39dbf..1c79e92e 100644 --- a/common/src/main/java/com/discordsrv/common/discord/connection/jda/JDAConnectionManager.java +++ b/common/src/main/java/com/discordsrv/common/discord/connection/jda/JDAConnectionManager.java @@ -70,10 +70,7 @@ import org.jetbrains.annotations.NotNull; import java.io.InterruptedIOException; import java.time.Duration; -import java.util.Collections; -import java.util.HashSet; -import java.util.LinkedHashSet; -import java.util.Set; +import java.util.*; import java.util.concurrent.*; import java.util.concurrent.atomic.AtomicReference; import java.util.function.Consumer; @@ -289,8 +286,9 @@ public class JDAConnectionManager implements DiscordConnectionManager { private void connectInternal() { BotConfig botConfig = discordSRV.connectionConfig().bot; String token = botConfig.token; - if (StringUtils.isBlank(token) || token.contains(" ")) { - invalidToken(); + boolean defaultToken = false; + if (StringUtils.isBlank(token) || (defaultToken = token.equals(BotConfig.DEFAULT_TOKEN))) { + invalidToken(discordSRV, defaultToken); return; } @@ -408,7 +406,7 @@ public class JDAConnectionManager implements DiscordConnectionManager { try { instance = jdaBuilder.build(); } catch (InvalidTokenException ignored) { - invalidToken(); + invalidToken(discordSRV, false); } catch (Throwable t) { discordSRV.logger().error("Could not create JDA instance due to an unknown error", t); } @@ -567,32 +565,39 @@ public class JDAConnectionManager implements DiscordConnectionManager { discordSRV.setStatus(DiscordSRVApi.Status.FAILED_TO_CONNECT); return true; } else if (closeCode == CloseCode.AUTHENTICATION_FAILED) { - invalidToken(); + invalidToken(discordSRV, false); return true; } return false; } - private void invalidToken() { - discordSRV.logger().error("+------------------------------>"); - discordSRV.logger().error("| Failed to connect to Discord:"); - discordSRV.logger().error("|"); - discordSRV.logger().error("| The token provided in the"); - discordSRV.logger().error("| " + ConnectionConfig.FILE_NAME + " is invalid"); - discordSRV.logger().error("|"); - discordSRV.logger().error("| Haven't created a bot yet? Installing the plugin for the first time?"); - discordSRV.logger().error("| See " + DocumentationURLs.CREATE_TOKEN); - discordSRV.logger().error("|"); - discordSRV.logger().error("| Already have a bot? You can get the token for your bot from:"); - discordSRV.logger().error("| https://discord.com/developers/applications"); - discordSRV.logger().error("| by selecting the application, going to the \"Bot\" tab"); - discordSRV.logger().error("| and clicking on \"Reset Token\""); - discordSRV.logger().error("| - Keep in mind the bot is only visible to"); - discordSRV.logger().error("| the Discord user that created the bot"); - discordSRV.logger().error("|"); - discordSRV.logger().error("| Once the token is corrected in the " + ConnectionConfig.FILE_NAME); - discordSRV.logger().error("| Run the \"/discordsrv reload config discord_connection\" command"); - discordSRV.logger().error("+------------------------------>"); + public static void invalidToken(DiscordSRV discordSRV, boolean defaultToken) { + List lines = Arrays.asList( + "+------------------------------>", + "| Failed to connect to Discord:", + "|", + "| The token provided in the", + "| " + ConnectionConfig.FILE_NAME + " is invalid", + "|", + "| Haven't created a bot yet? Installing the plugin for the first time?", + "| See " + DocumentationURLs.CREATE_TOKEN, + "|", + "| Already have a bot? You can get the token for your bot from:", + "| https://discord.com/developers/applications", + "| by selecting the application, going to the \"Bot\" tab", + "| and clicking on \"Reset Token\"", + "| - Keep in mind the bot is only visible to", + "| the Discord user that created the bot", + "|", + "| Once the token is corrected in the " + ConnectionConfig.FILE_NAME, + "| Run the \"/discordsrv reload config discord_connection\" command", + "+------------------------------>" + ); + if (defaultToken) { + lines.forEach(line -> discordSRV.logger().warning(line)); + } else { + lines.forEach(line -> discordSRV.logger().error(line)); + } discordSRV.setStatus(DiscordSRVApi.Status.FAILED_TO_CONNECT); }