diff --git a/src/main/java/wtf/beatrice/hidekobot/HidekoBot.java b/src/main/java/wtf/beatrice/hidekobot/HidekoBot.java index 6a9f666..fe9f6fe 100644 --- a/src/main/java/wtf/beatrice/hidekobot/HidekoBot.java +++ b/src/main/java/wtf/beatrice/hidekobot/HidekoBot.java @@ -6,6 +6,7 @@ import net.dv8tion.jda.api.OnlineStatus; import net.dv8tion.jda.api.entities.Activity; import net.dv8tion.jda.api.requests.GatewayIntent; import sun.misc.Signal; +import wtf.beatrice.hidekobot.commands.slash.*; import wtf.beatrice.hidekobot.datasource.ConfigurationSource; import wtf.beatrice.hidekobot.datasource.DatabaseSource; import wtf.beatrice.hidekobot.listeners.ButtonInteractionListener; @@ -97,9 +98,21 @@ public class HidekoBot } + // register commands + SlashCommandListener slashCommandListener = new SlashCommandListener(); + slashCommandListener.registerCommand(new AvatarCommand()); + slashCommandListener.registerCommand(new BotInfoCommand()); + slashCommandListener.registerCommand(new ClearCommand()); + slashCommandListener.registerCommand(new CoinFlipCommand()); + slashCommandListener.registerCommand(new DieCommand()); + slashCommandListener.registerCommand(new HelpCommand()); + slashCommandListener.registerCommand(new InviteCommand()); + slashCommandListener.registerCommand(new PingCommand()); + slashCommandListener.registerCommand(new SayCommand()); + // register listeners jda.addEventListener(new MessageListener()); - jda.addEventListener(new SlashCommandListener()); + jda.addEventListener(slashCommandListener); jda.addEventListener(new SlashCommandCompleter()); jda.addEventListener(new ButtonInteractionListener()); diff --git a/src/main/java/wtf/beatrice/hidekobot/commands/slash/AvatarCommand.java b/src/main/java/wtf/beatrice/hidekobot/commands/slash/AvatarCommand.java index 58bd6b7..5d81853 100644 --- a/src/main/java/wtf/beatrice/hidekobot/commands/slash/AvatarCommand.java +++ b/src/main/java/wtf/beatrice/hidekobot/commands/slash/AvatarCommand.java @@ -6,10 +6,17 @@ import net.dv8tion.jda.api.events.interaction.command.SlashCommandInteractionEve import net.dv8tion.jda.api.interactions.commands.OptionMapping; import org.jetbrains.annotations.NotNull; import wtf.beatrice.hidekobot.Cache; +import wtf.beatrice.hidekobot.objects.SlashCommand; -public class AvatarCommand +public class AvatarCommand implements SlashCommand { + @Override + public String getCommandName() { + return "avatar"; + } + + @Override public void runSlashCommand(@NotNull SlashCommandInteractionEvent event) { // defer reply because this might take a moment diff --git a/src/main/java/wtf/beatrice/hidekobot/commands/slash/BotInfoCommand.java b/src/main/java/wtf/beatrice/hidekobot/commands/slash/BotInfoCommand.java index 72eeb18..91dc1a6 100644 --- a/src/main/java/wtf/beatrice/hidekobot/commands/slash/BotInfoCommand.java +++ b/src/main/java/wtf/beatrice/hidekobot/commands/slash/BotInfoCommand.java @@ -6,15 +6,22 @@ import net.dv8tion.jda.api.interactions.commands.Command; import org.jetbrains.annotations.NotNull; import wtf.beatrice.hidekobot.Cache; import wtf.beatrice.hidekobot.HidekoBot; +import wtf.beatrice.hidekobot.objects.SlashCommand; import wtf.beatrice.hidekobot.util.FormatUtil; import java.lang.management.ManagementFactory; import java.text.DecimalFormat; import java.util.List; -public class BotInfoCommand +public class BotInfoCommand implements SlashCommand { + @Override + public String getCommandName() { + return "botinfo"; + } + + @Override public void runSlashCommand(@NotNull SlashCommandInteractionEvent event) { // defer reply because this might take a moment diff --git a/src/main/java/wtf/beatrice/hidekobot/commands/slash/ClearCommand.java b/src/main/java/wtf/beatrice/hidekobot/commands/slash/ClearCommand.java index 43c4792..6841382 100644 --- a/src/main/java/wtf/beatrice/hidekobot/commands/slash/ClearCommand.java +++ b/src/main/java/wtf/beatrice/hidekobot/commands/slash/ClearCommand.java @@ -13,13 +13,21 @@ import net.dv8tion.jda.api.interactions.components.buttons.Button; import net.dv8tion.jda.api.requests.restaction.WebhookMessageEditAction; import org.jetbrains.annotations.NotNull; import wtf.beatrice.hidekobot.Cache; +import wtf.beatrice.hidekobot.objects.SlashCommand; import java.util.ArrayList; import java.util.List; -public class ClearCommand +public class ClearCommand implements SlashCommand { + @Override + public String getCommandName() { + return "clear"; + } + + @Override + public void runSlashCommand(@NotNull SlashCommandInteractionEvent event) { diff --git a/src/main/java/wtf/beatrice/hidekobot/commands/slash/CoinFlipCommand.java b/src/main/java/wtf/beatrice/hidekobot/commands/slash/CoinFlipCommand.java index e629ccc..2dc7310 100644 --- a/src/main/java/wtf/beatrice/hidekobot/commands/slash/CoinFlipCommand.java +++ b/src/main/java/wtf/beatrice/hidekobot/commands/slash/CoinFlipCommand.java @@ -9,16 +9,23 @@ import net.dv8tion.jda.api.interactions.components.ActionRow; import net.dv8tion.jda.api.interactions.components.buttons.Button; import org.jetbrains.annotations.NotNull; import wtf.beatrice.hidekobot.Cache; +import wtf.beatrice.hidekobot.objects.SlashCommand; import wtf.beatrice.hidekobot.util.RandomUtil; import java.util.List; -public class CoinFlipCommand +public class CoinFlipCommand implements SlashCommand { + @Override + public String getCommandName() { + return "coinflip"; + } + private final Button reflipButton = Button.primary("coinflip_reflip", "Flip again") .withEmoji(Emoji.fromFormatted("\uD83E\uDE99")); + @Override public void runSlashCommand(@NotNull SlashCommandInteractionEvent event) { // perform coin flip diff --git a/src/main/java/wtf/beatrice/hidekobot/commands/slash/DieCommand.java b/src/main/java/wtf/beatrice/hidekobot/commands/slash/DieCommand.java index c8d888d..e9b6a4e 100644 --- a/src/main/java/wtf/beatrice/hidekobot/commands/slash/DieCommand.java +++ b/src/main/java/wtf/beatrice/hidekobot/commands/slash/DieCommand.java @@ -4,13 +4,20 @@ import net.dv8tion.jda.api.events.interaction.command.SlashCommandInteractionEve import org.jetbrains.annotations.NotNull; import wtf.beatrice.hidekobot.Cache; import wtf.beatrice.hidekobot.HidekoBot; +import wtf.beatrice.hidekobot.objects.SlashCommand; import java.util.concurrent.Executors; import java.util.concurrent.TimeUnit; -public class DieCommand +public class DieCommand implements SlashCommand { + @Override + public String getCommandName() { + return "die"; + } + + @Override public void runSlashCommand(@NotNull SlashCommandInteractionEvent event) { if(Cache.getBotOwnerId() != event.getUser().getIdLong()) diff --git a/src/main/java/wtf/beatrice/hidekobot/commands/slash/HelpCommand.java b/src/main/java/wtf/beatrice/hidekobot/commands/slash/HelpCommand.java index 36f07dc..834dde2 100644 --- a/src/main/java/wtf/beatrice/hidekobot/commands/slash/HelpCommand.java +++ b/src/main/java/wtf/beatrice/hidekobot/commands/slash/HelpCommand.java @@ -4,10 +4,17 @@ import net.dv8tion.jda.api.EmbedBuilder; import net.dv8tion.jda.api.events.interaction.command.SlashCommandInteractionEvent; import org.jetbrains.annotations.NotNull; import wtf.beatrice.hidekobot.Cache; +import wtf.beatrice.hidekobot.objects.SlashCommand; -public class HelpCommand +public class HelpCommand implements SlashCommand { + @Override + public String getCommandName() { + return "help"; + } + + @Override public void runSlashCommand(@NotNull SlashCommandInteractionEvent event) { // defer reply because replying might take a while diff --git a/src/main/java/wtf/beatrice/hidekobot/commands/slash/InviteCommand.java b/src/main/java/wtf/beatrice/hidekobot/commands/slash/InviteCommand.java index b04cf30..f58aa52 100644 --- a/src/main/java/wtf/beatrice/hidekobot/commands/slash/InviteCommand.java +++ b/src/main/java/wtf/beatrice/hidekobot/commands/slash/InviteCommand.java @@ -11,9 +11,17 @@ import net.dv8tion.jda.api.requests.restaction.interactions.ReplyCallbackAction; import org.jetbrains.annotations.NotNull; import wtf.beatrice.hidekobot.Cache; import wtf.beatrice.hidekobot.HidekoBot; +import wtf.beatrice.hidekobot.objects.SlashCommand; -public class InviteCommand +public class InviteCommand implements SlashCommand { + + @Override + public String getCommandName() { + return "invite"; + } + + @Override public void runSlashCommand(@NotNull SlashCommandInteractionEvent event) { // defer reply because this might take a moment diff --git a/src/main/java/wtf/beatrice/hidekobot/commands/slash/PingCommand.java b/src/main/java/wtf/beatrice/hidekobot/commands/slash/PingCommand.java index bc4a31e..035fa57 100644 --- a/src/main/java/wtf/beatrice/hidekobot/commands/slash/PingCommand.java +++ b/src/main/java/wtf/beatrice/hidekobot/commands/slash/PingCommand.java @@ -2,9 +2,16 @@ package wtf.beatrice.hidekobot.commands.slash; import net.dv8tion.jda.api.events.interaction.command.SlashCommandInteractionEvent; import org.jetbrains.annotations.NotNull; +import wtf.beatrice.hidekobot.objects.SlashCommand; -public class PingCommand +public class PingCommand implements SlashCommand { + @Override + public String getCommandName() { + return "ping"; + } + + @Override public void runSlashCommand(@NotNull SlashCommandInteractionEvent event) { event.reply("Pong!").queue(); diff --git a/src/main/java/wtf/beatrice/hidekobot/commands/slash/SayCommand.java b/src/main/java/wtf/beatrice/hidekobot/commands/slash/SayCommand.java index 2011dae..ebc2a83 100644 --- a/src/main/java/wtf/beatrice/hidekobot/commands/slash/SayCommand.java +++ b/src/main/java/wtf/beatrice/hidekobot/commands/slash/SayCommand.java @@ -4,10 +4,18 @@ import net.dv8tion.jda.api.entities.channel.middleman.MessageChannel; import net.dv8tion.jda.api.events.interaction.command.SlashCommandInteractionEvent; import net.dv8tion.jda.api.interactions.commands.OptionMapping; import org.jetbrains.annotations.NotNull; +import wtf.beatrice.hidekobot.objects.SlashCommand; -public class SayCommand +public class SayCommand implements SlashCommand { + + @Override + public String getCommandName() { + return "say"; + } + + @Override public void runSlashCommand(@NotNull SlashCommandInteractionEvent event) { MessageChannel channel = event.getChannel(); diff --git a/src/main/java/wtf/beatrice/hidekobot/listeners/MessageListener.java b/src/main/java/wtf/beatrice/hidekobot/listeners/MessageListener.java index 8720aa2..38c041c 100644 --- a/src/main/java/wtf/beatrice/hidekobot/listeners/MessageListener.java +++ b/src/main/java/wtf/beatrice/hidekobot/listeners/MessageListener.java @@ -10,6 +10,12 @@ import wtf.beatrice.hidekobot.util.Logger; public class MessageListener extends ListenerAdapter { + private final String commandRegex = "(?i)^(hideko|hde)\\b"; + // (?i) -> case insensitive flag + // ^ -> start of string (not in middle of a sentence) + // \b -> the word has to end here + // .* -> there can be anything else after this word + private final Logger logger = new Logger(MessageListener.class); @Override @@ -17,24 +23,31 @@ public class MessageListener extends ListenerAdapter { String eventMessage = event.getMessage().getContentDisplay(); + if(!eventMessage.toLowerCase().matches(commandRegex + ".*")) + return; + + MessageChannel channel = event.getChannel(); + // generate args from the string + String argsString = eventMessage.replaceAll(commandRegex + "\\s*", ""); + String[] args = argsString.split("\\s+"); + + event.getMessage().reply("Hi").queue(); + + if(eventMessage.equalsIgnoreCase("hideko")) { - MessageChannel channel = event.getChannel(); channel.sendMessage("Hello there! ✨").queue(); return; } if(eventMessage.equalsIgnoreCase("ping")) { - MessageChannel channel = event.getChannel(); channel.sendMessage("Pong!").queue(); return; } if(eventMessage.equalsIgnoreCase("hideko verbose")) { - MessageChannel channel = event.getChannel(); - boolean verbose = Cache.isVerbose(); String msg = verbose ? "off" : "on"; diff --git a/src/main/java/wtf/beatrice/hidekobot/listeners/SlashCommandListener.java b/src/main/java/wtf/beatrice/hidekobot/listeners/SlashCommandListener.java index d915d60..dd16e35 100644 --- a/src/main/java/wtf/beatrice/hidekobot/listeners/SlashCommandListener.java +++ b/src/main/java/wtf/beatrice/hidekobot/listeners/SlashCommandListener.java @@ -3,24 +3,28 @@ package wtf.beatrice.hidekobot.listeners; import net.dv8tion.jda.api.events.interaction.command.SlashCommandInteractionEvent; import net.dv8tion.jda.api.hooks.ListenerAdapter; import org.jetbrains.annotations.NotNull; -import wtf.beatrice.hidekobot.commands.slash.*; +import wtf.beatrice.hidekobot.objects.SlashCommand; + +import java.util.HashMap; public class SlashCommandListener extends ListenerAdapter { + HashMap registeredCommands = new HashMap<>(); + + public void registerCommand(SlashCommand command) + { + registeredCommands.remove(command.getCommandName()); + registeredCommands.put(command.getCommandName(), command); + } + @Override public void onSlashCommandInteraction(@NotNull SlashCommandInteractionEvent event) { - switch (event.getName().toLowerCase()) { - case "avatar" -> new AvatarCommand().runSlashCommand(event); - case "botinfo" -> new BotInfoCommand().runSlashCommand(event); - case "clear" -> new ClearCommand().runSlashCommand(event); - case "coinflip" -> new CoinFlipCommand().runSlashCommand(event); - case "die" -> new DieCommand().runSlashCommand(event); - case "help" -> new HelpCommand().runSlashCommand(event); - case "invite" -> new InviteCommand().runSlashCommand(event); - case "ping" -> new PingCommand().runSlashCommand(event); - case "say" -> new SayCommand().runSlashCommand(event); - } + String commandName = event.getName().toLowerCase(); + SlashCommand command = registeredCommands.get(commandName); + if(command == null) return; + + command.runSlashCommand(event); } } diff --git a/src/main/java/wtf/beatrice/hidekobot/objects/SlashCommand.java b/src/main/java/wtf/beatrice/hidekobot/objects/SlashCommand.java new file mode 100644 index 0000000..54c3251 --- /dev/null +++ b/src/main/java/wtf/beatrice/hidekobot/objects/SlashCommand.java @@ -0,0 +1,11 @@ +package wtf.beatrice.hidekobot.objects; + +import net.dv8tion.jda.api.events.interaction.command.SlashCommandInteractionEvent; +import org.jetbrains.annotations.NotNull; + +public interface SlashCommand +{ + String getCommandName(); + void runSlashCommand(@NotNull SlashCommandInteractionEvent event); + +}