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:
parent
af9e1e627f
commit
1bf08fb4fc
|
|
@ -13,6 +13,6 @@ public class DiscordBot {
|
|||
System.exit(1);
|
||||
}
|
||||
DiscordBotInstance discordBotInstance = DiscordBotInstance.getInstance();
|
||||
discordBotInstance.start(discordToken);
|
||||
discordBotInstance.getJda();
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -23,6 +23,7 @@ public class DiscordBotInstance {
|
|||
public JDA getJda() {
|
||||
if (jda == null) {
|
||||
String discordToken = System.getProperty("DISCORD_TOKEN");
|
||||
log.info("env:\n{}", System.getenv());
|
||||
if (discordToken == null) {
|
||||
log.error("Discord token not found, put it in the DISCORD_TOKEN environment variable");
|
||||
System.exit(1);
|
||||
|
|
@ -37,7 +38,7 @@ public class DiscordBotInstance {
|
|||
return jda;
|
||||
}
|
||||
|
||||
public synchronized void start(String token) {
|
||||
private synchronized void start(String token) {
|
||||
if (jda != null) {
|
||||
return;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -30,19 +30,7 @@ public class DiscordSender {
|
|||
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) {
|
||||
ensureStarted();
|
||||
if (botInstance.getJda() == null) {
|
||||
log.error("JDA not initialized; cannot send Discord message.");
|
||||
return;
|
||||
|
|
@ -100,7 +88,6 @@ public class DiscordSender {
|
|||
}
|
||||
|
||||
public CompletableFuture<Optional<Message>> sendEmbedToChannel(Long channelId, MessageForEmbed messageForEmbed) {
|
||||
ensureStarted();
|
||||
if (botInstance.getJda() == null) {
|
||||
log.error("JDA not initialized; cannot send Discord embed.");
|
||||
return CompletableFuture.completedFuture(Optional.empty());
|
||||
|
|
|
|||
Loading…
Reference in New Issue
Block a user