Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added Config class #19

Merged
merged 1 commit into from
Mar 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 7 additions & 20 deletions app/src/main/java/com/togetherjava/tjplays/Bot.java
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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<SlashCommand> commands = getCommands();
List<SlashCommand> commands = getCommands(config);
commands.forEach(command -> jda.addEventListener(command));

List<SlashCommandData> commandDatas = commands.stream()
Expand All @@ -44,7 +31,7 @@ private static JDA createJDA(String botToken) {
return jda;
}

private static List<SlashCommand> getCommands() {
private static List<SlashCommand> getCommands(Config config) {
return List.of(
new PingCommand(),
new Game2048Command()
Expand Down
26 changes: 26 additions & 0 deletions app/src/main/java/com/togetherjava/tjplays/Config.java
Original file line number Diff line number Diff line change
@@ -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")
);
}
}
Loading