Tweak default bot token logic

This commit is contained in:
Vankka 2024-06-29 16:01:41 +03:00
parent ba3871ed77
commit 9daa138a12
No known key found for this signature in database
GPG Key ID: 62E48025ED4E7EBB
4 changed files with 50 additions and 29 deletions

View File

@ -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) {

View File

@ -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,

View File

@ -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;
}

View File

@ -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<String> 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);
}