Refactor Discord token retrieval by prioritizing environment variable and update lambda formatting in DiscordSender.

This commit is contained in:
akastijn 2025-11-23 03:45:28 +01:00
parent 1bf08fb4fc
commit 20c89a4f8e
3 changed files with 6 additions and 10 deletions

View File

@ -7,11 +7,6 @@ import lombok.extern.slf4j.Slf4j;
public class DiscordBot { public class DiscordBot {
public static void main(String[] args) { public static void main(String[] args) {
String discordToken = System.getProperty("DISCORD_TOKEN");
if (discordToken == null) {
log.error("Discord token not found, put it in the DISCORD_TOKEN environment variable");
System.exit(1);
}
DiscordBotInstance discordBotInstance = DiscordBotInstance.getInstance(); DiscordBotInstance discordBotInstance = DiscordBotInstance.getInstance();
discordBotInstance.getJda(); discordBotInstance.getJda();
} }

View File

@ -6,6 +6,8 @@ import net.dv8tion.jda.api.JDA;
import net.dv8tion.jda.api.JDABuilder; import net.dv8tion.jda.api.JDABuilder;
import net.dv8tion.jda.api.requests.GatewayIntent; import net.dv8tion.jda.api.requests.GatewayIntent;
import java.util.Optional;
@Slf4j @Slf4j
public class DiscordBotInstance { public class DiscordBotInstance {
@ -22,8 +24,8 @@ public class DiscordBotInstance {
public JDA getJda() { public JDA getJda() {
if (jda == null) { if (jda == null) {
String discordToken = System.getProperty("DISCORD_TOKEN"); String discordToken = Optional.ofNullable(System.getenv("DISCORD_TOKEN"))
log.info("env:\n{}", System.getenv()); .orElse(System.getProperty("DISCORD_TOKEN"));
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);

View File

@ -69,9 +69,8 @@ public class DiscordSender {
result.stream() result.stream()
.filter(Optional::isPresent) .filter(Optional::isPresent)
.map(Optional::get) .map(Optional::get)
.forEach(message -> { .forEach(message ->
message.createThreadChannel(threadName).queue(); message.createThreadChannel(threadName).queue());
});
}); });
} }