From 3f0bd350a8b0ab502b1c9df51016e971b3b11f63 Mon Sep 17 00:00:00 2001 From: Tanish Azad Date: Sun, 17 Mar 2024 01:50:54 +0530 Subject: [PATCH] added Config class --- .../java/com/togetherjava/tjplays/Bot.java | 27 +++++-------------- .../java/com/togetherjava/tjplays/Config.java | 26 ++++++++++++++++++ 2 files changed, 33 insertions(+), 20 deletions(-) create mode 100644 app/src/main/java/com/togetherjava/tjplays/Config.java diff --git a/app/src/main/java/com/togetherjava/tjplays/Bot.java b/app/src/main/java/com/togetherjava/tjplays/Bot.java index e893874..292172d 100644 --- a/app/src/main/java/com/togetherjava/tjplays/Bot.java +++ b/app/src/main/java/com/togetherjava/tjplays/Bot.java @@ -1,10 +1,8 @@ package com.togetherjava.tjplays; -import java.io.FileInputStream; import java.io.IOException; +import java.nio.file.Path; import java.util.List; -import java.util.Properties; - import com.togetherjava.tjplays.listeners.commands.*; import net.dv8tion.jda.api.JDA; @@ -13,26 +11,15 @@ public final class Bot { public static void main(String[] args) throws IOException { - Properties properties = readProperties(args); - - String botToken = properties.getProperty("BOT_TOKEN"); - - createJDA(botToken); - } - - private static Properties readProperties(String... args) throws IOException { - Properties properties = new Properties(); - - String configPath = args.length == 0 ? "bot-config.properties" : args[0]; - properties.load(new FileInputStream(configPath)); + Config config = Config.readConfig(args.length == 0 ? null : Path.of(args[0])); - return properties; + createBot(config); } - private static JDA createJDA(String botToken) { - JDA jda = JDABuilder.createDefault(botToken).build(); + private static JDA createBot(Config config) { + JDA jda = JDABuilder.createDefault(config.botToken()).build(); - List commands = getCommands(); + List commands = getCommands(config); commands.forEach(command -> jda.addEventListener(command)); List commandDatas = commands.stream() @@ -44,7 +31,7 @@ private static JDA createJDA(String botToken) { return jda; } - private static List getCommands() { + private static List getCommands(Config config) { return List.of( new PingCommand(), new Game2048Command() diff --git a/app/src/main/java/com/togetherjava/tjplays/Config.java b/app/src/main/java/com/togetherjava/tjplays/Config.java new file mode 100644 index 0000000..0dd39db --- /dev/null +++ b/app/src/main/java/com/togetherjava/tjplays/Config.java @@ -0,0 +1,26 @@ +package com.togetherjava.tjplays; + +import java.util.Properties; +import java.io.FileInputStream; +import java.io.IOException; +import java.nio.file.Path; + +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; + +public final record Config( + @NotNull String botToken +) { + private static final String DEFAULT_PATH = "bot-config.properties"; + + public static Config readConfig(@Nullable Path configPath) throws IOException { + Properties properties = new Properties(); + + configPath = configPath == null ? Path.of(DEFAULT_PATH) : configPath; + properties.load(new FileInputStream(configPath.toString())); + + return new Config( + properties.getProperty("BOT_TOKEN") + ); + } +}