Skip to content

Commit

Permalink
added config and onSlashCommand method (#12)
Browse files Browse the repository at this point in the history
* added config and onSlashCommand method

* load config from path

* split main method

* removed redundent check
  • Loading branch information
Taz03 authored Mar 14, 2024
1 parent 24d7ff2 commit 1ae5b7a
Show file tree
Hide file tree
Showing 8 changed files with 64 additions and 33 deletions.
4 changes: 2 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -44,5 +44,5 @@ nbdist/
# Ignore Gradle build output directory
build

### Vim ###
.vim/
# Credentials
bot-config.properties
1 change: 1 addition & 0 deletions app/bot-config.properties.template
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
BOT_TOKEN=<put your token here>
2 changes: 1 addition & 1 deletion app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ dependencies {
}

application {
mainClass = 'com.togetherjava.tjplays.App'
mainClass = 'com.togetherjava.tjplays.Bot'
}

tasks.named('test') {
Expand Down
24 changes: 0 additions & 24 deletions app/src/main/java/com/togetherjava/tjplays/App.java

This file was deleted.

53 changes: 53 additions & 0 deletions app/src/main/java/com/togetherjava/tjplays/Bot.java
Original file line number Diff line number Diff line change
@@ -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<SlashCommand> commands = getCommands();
commands.forEach(command -> jda.addEventListener(command));

List<SlashCommandData> commandDatas = commands.stream()
.map(SlashCommand::getData)
.toList();

jda.updateCommands().addCommands(commandDatas).queue();

return jda;
}

private static List<SlashCommand> getCommands() {
return List.of(
new PingCommand(),
new Game2048Command()
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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()))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 1ae5b7a

Please sign in to comment.