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 and onSlashCommand method #12

Merged
merged 4 commits into from
Mar 14, 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
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
Loading