Skip to content

Commit

Permalink
Use of slash commands (#3)
Browse files Browse the repository at this point in the history
  • Loading branch information
Tosox authored Jul 4, 2023
1 parent 701bbbe commit 6f80c94
Show file tree
Hide file tree
Showing 24 changed files with 222 additions and 411 deletions.
21 changes: 0 additions & 21 deletions src/main/java/de/tosoxdev/tosoxjr/GenericCommandBase.java

This file was deleted.

32 changes: 0 additions & 32 deletions src/main/java/de/tosoxdev/tosoxjr/GenericManagerBase.java

This file was deleted.

15 changes: 3 additions & 12 deletions src/main/java/de/tosoxdev/tosoxjr/Main.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
package de.tosoxdev.tosoxjr;

import de.tosoxdev.tosoxjr.commands.CommandManager;
import de.tosoxdev.tosoxjr.games.GameManager;
import de.tosoxdev.tosoxjr.listener.StatusListener;
import de.tosoxdev.tosoxjr.listener.UserInputListener;
import de.tosoxdev.tosoxjr.slashcommands.SlashCommandManager;
import de.tosoxdev.tosoxjr.utils.Constants;
import net.dv8tion.jda.api.JDABuilder;
import net.dv8tion.jda.api.OnlineStatus;
Expand All @@ -23,12 +21,13 @@ public class Main {
// Permissions:
// - Send Messages
// - Manage Messages
//
// Prod: https://discord.com/api/oauth2/authorize?client_id=853752473365250089&permissions=10240&scope=bot%20applications.commands
// Dev : https://discord.com/api/oauth2/authorize?client_id=1125333842186752091&permissions=10240&scope=bot%20applications.commands
///////////////////////////////////////////////////////////////

private static final Logger LOGGER = LoggerFactory.getLogger(Main.class);
private static final CommandManager COMMAND_MANAGER = new CommandManager();
private static final SlashCommandManager SLASH_COMMAND_MANAGER = new SlashCommandManager();
private static final GameManager GAME_MANAGER = new GameManager();

public static void main(String[] args) throws InterruptedException {
JDABuilder.createDefault(Constants.BOT_TOKEN)
Expand All @@ -48,12 +47,4 @@ public static Logger getLogger() {
public static CommandManager getCommandManager() {
return COMMAND_MANAGER;
}

public static SlashCommandManager getSlashCommandManager() {
return SLASH_COMMAND_MANAGER;
}

public static GameManager getGameManager() {
return GAME_MANAGER;
}
}
32 changes: 27 additions & 5 deletions src/main/java/de/tosoxdev/tosoxjr/commands/CommandBase.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,32 @@
package de.tosoxdev.tosoxjr.commands;

import de.tosoxdev.tosoxjr.GenericCommandBase;
import net.dv8tion.jda.api.events.message.MessageReceivedEvent;
import net.dv8tion.jda.api.events.interaction.command.SlashCommandInteractionEvent;
import net.dv8tion.jda.api.interactions.commands.build.OptionData;

public abstract class CommandBase extends GenericCommandBase<MessageReceivedEvent> {
public CommandBase(String name, String description) {
super(name, description);
import java.util.List;

public abstract class CommandBase {
private final String name;
private final String description;
private final List<OptionData> options;

public CommandBase(String name, String description, List<OptionData> options) {
this.name = name;
this.description = description;
this.options = options;
}

public String getName() {
return name;
}

public String getDescription() {
return description;
}

public List<OptionData> getOptions() {
return options;
}

public abstract void handle(SlashCommandInteractionEvent event);
}
76 changes: 35 additions & 41 deletions src/main/java/de/tosoxdev/tosoxjr/commands/CommandManager.java
Original file line number Diff line number Diff line change
@@ -1,56 +1,50 @@
package de.tosoxdev.tosoxjr.commands;

import de.tosoxdev.tosoxjr.GenericManagerBase;
import de.tosoxdev.tosoxjr.Main;
import de.tosoxdev.tosoxjr.commands.cat.CatCmd;
import de.tosoxdev.tosoxjr.commands.csstats.CSStatsCmd;
import de.tosoxdev.tosoxjr.commands.help.HelpCmd;
import de.tosoxdev.tosoxjr.commands.list.ListCmd;
import de.tosoxdev.tosoxjr.commands.ping.PingCmd;
import de.tosoxdev.tosoxjr.commands.hangman.HangmanCmd;
import de.tosoxdev.tosoxjr.commands.quote.QuoteCmd;
import de.tosoxdev.tosoxjr.commands.say.SayCmd;
import de.tosoxdev.tosoxjr.games.GameBase;
import de.tosoxdev.tosoxjr.utils.Constants;
import net.dv8tion.jda.api.events.message.MessageReceivedEvent;
import net.dv8tion.jda.api.events.interaction.command.SlashCommandInteractionEvent;
import org.jetbrains.annotations.NotNull;

import java.util.ArrayList;
import java.util.List;

public class CommandManager {
private final List<CommandBase> commands = new ArrayList<>();

public class CommandManager extends GenericManagerBase<CommandBase, MessageReceivedEvent> {
public CommandManager() {
addElement(new PingCmd());
addElement(new SayCmd());
addElement(new HelpCmd());
addElement(new ListCmd());
addElement(new QuoteCmd());
addElement(new CSStatsCmd());
addElement(new CatCmd());
addCommand(new SayCmd());
addCommand(new CatCmd());
addCommand(new QuoteCmd());
addCommand(new CSStatsCmd());
addCommand(new HangmanCmd());
}

public List<CommandBase> getCommands() {
return commands;
}

@Override
public void handle(MessageReceivedEvent event) {
// Check if message is valid
if (!event.isFromGuild()) return;
if (!event.getMessage().getContentDisplay().startsWith(Constants.BOT_PREFIX)) return;
if (event.getAuthor().isBot()) return;
if (event.isWebhookMessage()) return;

// Remove prefix, split arguments
String[] split = event.getMessage().getContentDisplay().substring(Constants.BOT_PREFIX.length()).split(" ");
String command = split[0].toLowerCase();
CommandBase cmd = getElement(command);

event.getChannel().sendTyping().queue();

if (cmd == null) {
// Check for games
GameBase game = Main.getGameManager().getElement(command);
if (game == null) {
event.getChannel().sendMessage("Seems like I don't know this command").queue();
return;
}

game.run(event);
return;
public void addCommand(CommandBase cmd) throws IllegalArgumentException {
boolean cmdExists = commands.stream().anyMatch(i -> i.getName().equalsIgnoreCase(cmd.getName()));
if (cmdExists) {
throw new IllegalArgumentException("Found a duplicate item in the list");
}
commands.add(cmd);
}

@NotNull
public CommandBase getCommand(String name) {
return commands.stream()
.filter(cmd -> cmd.getName().equalsIgnoreCase(name))
.findFirst()
.orElseThrow();
}

public void handle(SlashCommandInteractionEvent event) {
String command = event.getName();
CommandBase cmd = getCommand(command);
cmd.handle(event);
}
}
14 changes: 14 additions & 0 deletions src/main/java/de/tosoxdev/tosoxjr/commands/GameBase.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package de.tosoxdev.tosoxjr.commands;

import net.dv8tion.jda.api.events.Event;
import net.dv8tion.jda.api.interactions.commands.build.OptionData;

import java.util.List;

public abstract class GameBase extends CommandBase {
public GameBase(String name, String description, List<OptionData> options) {
super(name, description, options);
}

public abstract void handleEvent(Event event);
}
10 changes: 5 additions & 5 deletions src/main/java/de/tosoxdev/tosoxjr/commands/cat/CatCmd.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,25 @@

import de.tosoxdev.tosoxjr.commands.CommandBase;
import de.tosoxdev.tosoxjr.utils.APIRequest;
import net.dv8tion.jda.api.events.message.MessageReceivedEvent;
import net.dv8tion.jda.api.events.interaction.command.SlashCommandInteractionEvent;
import org.json.JSONArray;

public class CatCmd extends CommandBase {
private static final String CAT_API = "https://api.thecatapi.com/v1/images/search";

public CatCmd() {
super("cat", "Get a random picture of a cat");
super("cat", "Get a random picture of a cat", null);
}

@Override
public void handle(MessageReceivedEvent event) {
public void handle(SlashCommandInteractionEvent event) {
JSONArray response = (JSONArray) APIRequest.getJson(CAT_API);
if ((response == null) || (response.isEmpty())) {
event.getChannel().sendMessage("Unable to get a cat image :(").queue();
event.reply("Unable to get a cat image :(").queue();
return;
}

String url = response.getJSONObject(0).getString("url");
event.getChannel().sendMessage(url).queue();
event.reply(url).queue();
}
}
38 changes: 16 additions & 22 deletions src/main/java/de/tosoxdev/tosoxjr/commands/csstats/CSStatsCmd.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@

import de.tosoxdev.tosoxjr.commands.CommandBase;
import de.tosoxdev.tosoxjr.utils.ArgumentParser;
import de.tosoxdev.tosoxjr.utils.Constants;
import net.dv8tion.jda.api.EmbedBuilder;
import net.dv8tion.jda.api.events.message.MessageReceivedEvent;
import net.dv8tion.jda.api.events.interaction.command.SlashCommandInteractionEvent;
import net.dv8tion.jda.api.interactions.commands.OptionType;
import net.dv8tion.jda.api.interactions.commands.build.OptionData;
import org.json.JSONObject;

import java.awt.*;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

Expand All @@ -19,54 +19,48 @@ public class CSStatsCmd extends CommandBase {
private final CSStats csStats = new CSStats();

public CSStatsCmd() {
super("cs-stats", "Get some CS:GO statistics about the given player");
super("cs-stats", "Get some CS:GO statistics about the given player", List.of(
new OptionData(OptionType.STRING, "user", "The user id64 or the url of the steam profile", true),
new OptionData(OptionType.STRING, "stat", "A statistic to retrieve from the user statistics", false)
));
}

@Override
public void handle(MessageReceivedEvent event) {
String[] split = event.getMessage().getContentDisplay().substring(Constants.BOT_PREFIX.length()).split(" ");
List<String> args = Arrays.asList(split).subList(1, split.length);

// Check if user parameter is provided
String user = ArgumentParser.get(args, 0);
if (user == null) {
String msg = String.format("Please use the correct syntax: %scsstats <userid/userurl> <optional: stat>", Constants.BOT_PREFIX);
event.getChannel().sendMessage(msg).queue();
return;
}
public void handle(SlashCommandInteractionEvent event) {
String user = ArgumentParser.getStringForced(event.getOption("user"));

// Check and get steamid64
String userid = csStats.getID64(user);
if (userid == null) {
String msg = String.format("Couldn't find user id from user url: %s", user);
event.getChannel().sendMessage(msg).queue();
event.reply(msg).queue();
return;
}

// Get user statistics
JSONObject userStats = csStats.getStatistics(userid);
if (userStats == null) {
String msg = String.format("Couldn't find user: %s", user);
event.getChannel().sendMessage(msg).queue();
event.reply(msg).queue();
return;
} else if (userStats.isEmpty()) {
String msg = String.format("User '%s' set 'Game Details' to private or friends only", user);
event.getChannel().sendMessage(msg).queue();
event.reply(msg).queue();
return;
}

// Check if a statistic parameter is provided
String stat = ArgumentParser.get(args, 1);
String stat = ArgumentParser.getString(event.getOption("stat"));
if (stat != null) {
// Check if statistic exists
String statistic = csStats.getStatistic(userStats, stat);
if (statistic == null) {
String msg = String.format("Couldn't find statistic: %s", stat);
event.getChannel().sendMessage(msg).queue();
event.reply(msg).queue();
return;
}

event.getChannel().sendMessage(statistic).queue();
event.reply(statistic).queue();
return;
}

Expand Down Expand Up @@ -111,6 +105,6 @@ public void handle(MessageReceivedEvent event) {
statsEmbed.addField("**Winrate**", wr + "%", true);
statsEmbed.setFooter("Request made @ " + formatter.format(new Date()), null);

event.getChannel().sendMessageEmbeds(statsEmbed.build()).queue();
event.replyEmbeds(statsEmbed.build()).queue();
}
}
Loading

0 comments on commit 6f80c94

Please sign in to comment.