diff --git a/.gitignore b/.gitignore index 81b6fd5..839c825 100644 --- a/.gitignore +++ b/.gitignore @@ -44,5 +44,5 @@ nbdist/ # Ignore Gradle build output directory build -### Vim ### -.vim/ +# Credentials +bot-config.properties diff --git a/app/bot-config.properties.template b/app/bot-config.properties.template new file mode 100644 index 0000000..0b77bed --- /dev/null +++ b/app/bot-config.properties.template @@ -0,0 +1 @@ +BOT_TOKEN= diff --git a/app/build.gradle b/app/build.gradle index 3e19b48..49dc925 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -45,7 +45,7 @@ dependencies { } application { - mainClass = 'com.togetherjava.tjplays.App' + mainClass = 'com.togetherjava.tjplays.Bot' } tasks.named('test') { diff --git a/app/src/main/java/com/togetherjava/tjplays/App.java b/app/src/main/java/com/togetherjava/tjplays/App.java deleted file mode 100644 index 375492d..0000000 --- a/app/src/main/java/com/togetherjava/tjplays/App.java +++ /dev/null @@ -1,24 +0,0 @@ -package com.togetherjava.tjplays; - -import java.util.List; - -import com.togetherjava.tjplays.listeners.commands.*; - -import net.dv8tion.jda.api.JDA; -import net.dv8tion.jda.api.JDABuilder; -import net.dv8tion.jda.api.interactions.commands.build.SlashCommandData; - -public final class App { - public static void main(String[] args) { - JDA jda = JDABuilder.createDefault(args[0]).build(); - - List commands = List.of(new PingCommand(), new Game2048Command()); - commands.forEach(command -> jda.addEventListener(command)); - - List commandDatas = commands.stream() - .map(SlashCommand::getData) - .toList(); - - jda.updateCommands().addCommands(commandDatas).queue(); - } -} diff --git a/app/src/main/java/com/togetherjava/tjplays/Bot.java b/app/src/main/java/com/togetherjava/tjplays/Bot.java new file mode 100644 index 0000000..e893874 --- /dev/null +++ b/app/src/main/java/com/togetherjava/tjplays/Bot.java @@ -0,0 +1,53 @@ +package com.togetherjava.tjplays; + +import java.io.FileInputStream; +import java.io.IOException; +import java.util.List; +import java.util.Properties; + +import com.togetherjava.tjplays.listeners.commands.*; + +import net.dv8tion.jda.api.JDA; +import net.dv8tion.jda.api.JDABuilder; +import net.dv8tion.jda.api.interactions.commands.build.SlashCommandData; + +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)); + + return properties; + } + + private static JDA createJDA(String botToken) { + JDA jda = JDABuilder.createDefault(botToken).build(); + + List commands = getCommands(); + commands.forEach(command -> jda.addEventListener(command)); + + List commandDatas = commands.stream() + .map(SlashCommand::getData) + .toList(); + + jda.updateCommands().addCommands(commandDatas).queue(); + + return jda; + } + + private static List getCommands() { + return List.of( + new PingCommand(), + new Game2048Command() + ); + } +} diff --git a/app/src/main/java/com/togetherjava/tjplays/listeners/commands/Game2048Command.java b/app/src/main/java/com/togetherjava/tjplays/listeners/commands/Game2048Command.java index 3ba3b3c..39b2e24 100644 --- a/app/src/main/java/com/togetherjava/tjplays/listeners/commands/Game2048Command.java +++ b/app/src/main/java/com/togetherjava/tjplays/listeners/commands/Game2048Command.java @@ -28,8 +28,7 @@ public Game2048Command() { } @Override - public void onSlashCommandInteraction(SlashCommandInteractionEvent event) { - if (!event.getName().equals(COMMAND_NAME)) return; + public void onSlashCommand(SlashCommandInteractionEvent event) { Renderer2048 gameRenderer = new Renderer2048(new Game2048()); event.reply(gameMessage(gameRenderer, event.getUser().getId())) diff --git a/app/src/main/java/com/togetherjava/tjplays/listeners/commands/PingCommand.java b/app/src/main/java/com/togetherjava/tjplays/listeners/commands/PingCommand.java index 39a0900..00b958f 100644 --- a/app/src/main/java/com/togetherjava/tjplays/listeners/commands/PingCommand.java +++ b/app/src/main/java/com/togetherjava/tjplays/listeners/commands/PingCommand.java @@ -11,9 +11,7 @@ public PingCommand() { } @Override - public void onSlashCommandInteraction(SlashCommandInteractionEvent event) { - if (!event.getName().equals(COMMAND_NAME)) return; - + public void onSlashCommand(SlashCommandInteractionEvent event) { event.reply("Pong!").queue(); } } diff --git a/app/src/main/java/com/togetherjava/tjplays/listeners/commands/SlashCommand.java b/app/src/main/java/com/togetherjava/tjplays/listeners/commands/SlashCommand.java index 0c9ceda..bd48c54 100644 --- a/app/src/main/java/com/togetherjava/tjplays/listeners/commands/SlashCommand.java +++ b/app/src/main/java/com/togetherjava/tjplays/listeners/commands/SlashCommand.java @@ -17,8 +17,12 @@ public SlashCommandData getData() { return data; } + public abstract void onSlashCommand(SlashCommandInteractionEvent event); + @Override - abstract public void onSlashCommandInteraction(SlashCommandInteractionEvent event); + public final void onSlashCommandInteraction(SlashCommandInteractionEvent event) { + if (event.getName().equals(data.getName())) onSlashCommand(event); + } // Should not be implemented @Override