Refactor DiscordBotInstance to remove start method from public API, initialize JDA with lazy loading, and clean up unused token validation logic.

This commit is contained in:
akastijn 2025-11-23 03:40:53 +01:00
parent af9e1e627f
commit 1bf08fb4fc
3 changed files with 3 additions and 15 deletions

View File

@ -13,6 +13,6 @@ public class DiscordBot {
System.exit(1); System.exit(1);
} }
DiscordBotInstance discordBotInstance = DiscordBotInstance.getInstance(); DiscordBotInstance discordBotInstance = DiscordBotInstance.getInstance();
discordBotInstance.start(discordToken); discordBotInstance.getJda();
} }
} }

View File

@ -23,6 +23,7 @@ public class DiscordBotInstance {
public JDA getJda() { public JDA getJda() {
if (jda == null) { if (jda == null) {
String discordToken = System.getProperty("DISCORD_TOKEN"); String discordToken = System.getProperty("DISCORD_TOKEN");
log.info("env:\n{}", System.getenv());
if (discordToken == null) { if (discordToken == null) {
log.error("Discord token not found, put it in the DISCORD_TOKEN environment variable"); log.error("Discord token not found, put it in the DISCORD_TOKEN environment variable");
System.exit(1); System.exit(1);
@ -37,7 +38,7 @@ public class DiscordBotInstance {
return jda; return jda;
} }
public synchronized void start(String token) { private synchronized void start(String token) {
if (jda != null) { if (jda != null) {
return; return;
} }

View File

@ -30,19 +30,7 @@ public class DiscordSender {
return INSTANCE; return INSTANCE;
} }
private void ensureStarted() {
if (botInstance.getJda() != null) return;
String token = Optional.ofNullable(System.getenv("DISCORD_TOKEN"))
.orElse(System.getProperty("DISCORD_TOKEN"));
if (token == null || token.isBlank()) {
log.error("Discord token not found. Set DISCORD_TOKEN as an environment variable or system property.");
return;
}
botInstance.start(token);
}
public void sendMessageToChannels(List<Long> channelIds, String message) { public void sendMessageToChannels(List<Long> channelIds, String message) {
ensureStarted();
if (botInstance.getJda() == null) { if (botInstance.getJda() == null) {
log.error("JDA not initialized; cannot send Discord message."); log.error("JDA not initialized; cannot send Discord message.");
return; return;
@ -100,7 +88,6 @@ public class DiscordSender {
} }
public CompletableFuture<Optional<Message>> sendEmbedToChannel(Long channelId, MessageForEmbed messageForEmbed) { public CompletableFuture<Optional<Message>> sendEmbedToChannel(Long channelId, MessageForEmbed messageForEmbed) {
ensureStarted();
if (botInstance.getJda() == null) { if (botInstance.getJda() == null) {
log.error("JDA not initialized; cannot send Discord embed."); log.error("JDA not initialized; cannot send Discord embed.");
return CompletableFuture.completedFuture(Optional.empty()); return CompletableFuture.completedFuture(Optional.empty());