From 50ccda214fbaf6f5a0a75f367615cb6c683c0116 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Beatrice=20Dellac=C3=A0?= Date: Wed, 23 Nov 2022 00:01:05 +0100 Subject: [PATCH] Finish command completion listener implementation Very similarly to how the slash command interface works, now a slash command auto-completion interface also exists, with its respective listener. --- .../java/wtf/beatrice/hidekobot/Cache.java | 8 ++++ .../wtf/beatrice/hidekobot/HidekoBot.java | 14 +++++-- ...leter.java => AvatarCommandCompleter.java} | 12 ++++-- .../listeners/SlashCommandCompleter.java | 17 --------- .../SlashCommandCompletionListener.java | 38 +++++++++++++++++++ .../listeners/SlashCommandListener.java | 2 +- .../commands/SlashArgumentsCompleter.java | 21 ++++++++++ .../commands/SlashArgumentsCompleterImpl.java | 21 ++++++++++ 8 files changed, 108 insertions(+), 25 deletions(-) rename src/main/java/wtf/beatrice/hidekobot/commands/completer/{AvatarCompleter.java => AvatarCommandCompleter.java} (68%) delete mode 100644 src/main/java/wtf/beatrice/hidekobot/listeners/SlashCommandCompleter.java create mode 100644 src/main/java/wtf/beatrice/hidekobot/listeners/SlashCommandCompletionListener.java create mode 100644 src/main/java/wtf/beatrice/hidekobot/objects/commands/SlashArgumentsCompleter.java create mode 100644 src/main/java/wtf/beatrice/hidekobot/objects/commands/SlashArgumentsCompleterImpl.java diff --git a/src/main/java/wtf/beatrice/hidekobot/Cache.java b/src/main/java/wtf/beatrice/hidekobot/Cache.java index 216a7e6..e41476b 100644 --- a/src/main/java/wtf/beatrice/hidekobot/Cache.java +++ b/src/main/java/wtf/beatrice/hidekobot/Cache.java @@ -7,6 +7,7 @@ import wtf.beatrice.hidekobot.datasources.DatabaseSource; import wtf.beatrice.hidekobot.datasources.PropertiesSource; import wtf.beatrice.hidekobot.listeners.MessageCommandListener; import wtf.beatrice.hidekobot.listeners.MessageLogger; +import wtf.beatrice.hidekobot.listeners.SlashCommandCompletionListener; import wtf.beatrice.hidekobot.listeners.SlashCommandListener; import wtf.beatrice.hidekobot.util.Logger; @@ -40,6 +41,7 @@ public class Cache private static final String botName = "Hideko"; private static SlashCommandListener slashCommandListener = null; + private static SlashCommandCompletionListener slashCommandCompletionListener = null; private static MessageCommandListener messageCommandListener = null; private final static String defaultInviteLink = @@ -226,6 +228,12 @@ public class Cache public static SlashCommandListener getSlashCommandListener() { return slashCommandListener; } + public static void setSlashCommandCompletionListener(SlashCommandCompletionListener commandCompletionListener) + { slashCommandCompletionListener = commandCompletionListener; } + + public static SlashCommandCompletionListener getSlashCommandCompletionListener() { return slashCommandCompletionListener; } + + public static void setMessageCommandListener(MessageCommandListener commandListener) { messageCommandListener = commandListener; } diff --git a/src/main/java/wtf/beatrice/hidekobot/HidekoBot.java b/src/main/java/wtf/beatrice/hidekobot/HidekoBot.java index c09147a..893637b 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.completer.AvatarCommandCompleter; import wtf.beatrice.hidekobot.commands.message.HelloCommand; import wtf.beatrice.hidekobot.commands.slash.*; import wtf.beatrice.hidekobot.datasources.ConfigurationSource; @@ -13,7 +14,7 @@ import wtf.beatrice.hidekobot.datasources.DatabaseSource; import wtf.beatrice.hidekobot.datasources.PropertiesSource; import wtf.beatrice.hidekobot.listeners.ButtonInteractionListener; import wtf.beatrice.hidekobot.listeners.MessageCommandListener; -import wtf.beatrice.hidekobot.listeners.SlashCommandCompleter; +import wtf.beatrice.hidekobot.listeners.SlashCommandCompletionListener; import wtf.beatrice.hidekobot.listeners.SlashCommandListener; import wtf.beatrice.hidekobot.runnables.ExpiredMessageTask; import wtf.beatrice.hidekobot.runnables.HeartBeatTask; @@ -107,9 +108,13 @@ public class HidekoBot } - // register slash commands + // register slash commands and completers SlashCommandListener slashCommandListener = new SlashCommandListener(); - slashCommandListener.registerCommand(new AvatarCommand()); + SlashCommandCompletionListener slashCommandCompletionListener = new SlashCommandCompletionListener(); + AvatarCommand avatarCommand = new AvatarCommand(); + AvatarCommandCompleter avatarCommandCompleter = new AvatarCommandCompleter(avatarCommand); + slashCommandListener.registerCommand(avatarCommand); + slashCommandCompletionListener.registerCommandCompleter(avatarCommandCompleter); slashCommandListener.registerCommand(new BotInfoCommand()); slashCommandListener.registerCommand(new ClearCommand()); slashCommandListener.registerCommand(new CoinFlipCommand()); @@ -119,6 +124,7 @@ public class HidekoBot slashCommandListener.registerCommand(new PingCommand()); slashCommandListener.registerCommand(new SayCommand()); Cache.setSlashCommandListener(slashCommandListener); + Cache.setSlashCommandCompletionListener(slashCommandCompletionListener); // register message commands MessageCommandListener messageCommandListener = new MessageCommandListener(); @@ -132,7 +138,7 @@ public class HidekoBot // register listeners jda.addEventListener(messageCommandListener); jda.addEventListener(slashCommandListener); - jda.addEventListener(new SlashCommandCompleter()); + jda.addEventListener(slashCommandCompletionListener); jda.addEventListener(new ButtonInteractionListener()); // update slash commands (delayed) diff --git a/src/main/java/wtf/beatrice/hidekobot/commands/completer/AvatarCompleter.java b/src/main/java/wtf/beatrice/hidekobot/commands/completer/AvatarCommandCompleter.java similarity index 68% rename from src/main/java/wtf/beatrice/hidekobot/commands/completer/AvatarCompleter.java rename to src/main/java/wtf/beatrice/hidekobot/commands/completer/AvatarCommandCompleter.java index 52c8d31..331b07f 100644 --- a/src/main/java/wtf/beatrice/hidekobot/commands/completer/AvatarCompleter.java +++ b/src/main/java/wtf/beatrice/hidekobot/commands/completer/AvatarCommandCompleter.java @@ -4,15 +4,21 @@ import net.dv8tion.jda.api.events.interaction.command.CommandAutoCompleteInterac import net.dv8tion.jda.api.interactions.commands.Command; import org.jetbrains.annotations.NotNull; import wtf.beatrice.hidekobot.Cache; +import wtf.beatrice.hidekobot.objects.commands.SlashArgumentsCompleterImpl; +import wtf.beatrice.hidekobot.objects.commands.SlashCommand; import java.util.ArrayList; import java.util.List; -public class AvatarCompleter +public class AvatarCommandCompleter extends SlashArgumentsCompleterImpl { - public AvatarCompleter(@NotNull CommandAutoCompleteInteractionEvent event) - { + public AvatarCommandCompleter(SlashCommand parentCommand) { + super(parentCommand); + } + + @Override + public void runCompletion(@NotNull CommandAutoCompleteInteractionEvent event) { if(event.getFocusedOption().getName().equals("size")) { diff --git a/src/main/java/wtf/beatrice/hidekobot/listeners/SlashCommandCompleter.java b/src/main/java/wtf/beatrice/hidekobot/listeners/SlashCommandCompleter.java deleted file mode 100644 index 0eec987..0000000 --- a/src/main/java/wtf/beatrice/hidekobot/listeners/SlashCommandCompleter.java +++ /dev/null @@ -1,17 +0,0 @@ -package wtf.beatrice.hidekobot.listeners; - -import net.dv8tion.jda.api.events.interaction.command.CommandAutoCompleteInteractionEvent; -import net.dv8tion.jda.api.hooks.ListenerAdapter; -import wtf.beatrice.hidekobot.commands.completer.AvatarCompleter; - -public class SlashCommandCompleter extends ListenerAdapter -{ - - @Override - public void onCommandAutoCompleteInteraction(CommandAutoCompleteInteractionEvent event) - { - switch (event.getName().toLowerCase()) { - case "avatar" -> new AvatarCompleter(event); - } - } -} diff --git a/src/main/java/wtf/beatrice/hidekobot/listeners/SlashCommandCompletionListener.java b/src/main/java/wtf/beatrice/hidekobot/listeners/SlashCommandCompletionListener.java new file mode 100644 index 0000000..85943c5 --- /dev/null +++ b/src/main/java/wtf/beatrice/hidekobot/listeners/SlashCommandCompletionListener.java @@ -0,0 +1,38 @@ +package wtf.beatrice.hidekobot.listeners; + +import net.dv8tion.jda.api.events.interaction.command.CommandAutoCompleteInteractionEvent; +import net.dv8tion.jda.api.hooks.ListenerAdapter; +import wtf.beatrice.hidekobot.objects.commands.SlashArgumentsCompleter; + +import java.util.LinkedList; +import java.util.TreeMap; + +public class SlashCommandCompletionListener extends ListenerAdapter +{ + + // map that stores command label and command auto-completer alphabetically. + private final TreeMap registeredCompleters = new TreeMap<>(); + + public void registerCommandCompleter(SlashArgumentsCompleter completer) + { + String parentCommandName = completer.getCommand().getCommandName(); + registeredCompleters.remove(parentCommandName); + registeredCompleters.put(parentCommandName, completer); + } + + public SlashArgumentsCompleter getRegisteredCompleter(String label) + { return registeredCompleters.get(label); } + + public LinkedList getRegisteredCompleters() + { return new LinkedList<>(registeredCompleters.values()); } + @Override + public void onCommandAutoCompleteInteraction(CommandAutoCompleteInteractionEvent event) + { + String commandName = event.getName().toLowerCase(); + SlashArgumentsCompleter completer = registeredCompleters.get(commandName); + if(completer == null) return; + + // not running in a thread because nothing heavy should be done here... + completer.runCompletion(event); + } +} diff --git a/src/main/java/wtf/beatrice/hidekobot/listeners/SlashCommandListener.java b/src/main/java/wtf/beatrice/hidekobot/listeners/SlashCommandListener.java index 0dcf455..e84f26f 100644 --- a/src/main/java/wtf/beatrice/hidekobot/listeners/SlashCommandListener.java +++ b/src/main/java/wtf/beatrice/hidekobot/listeners/SlashCommandListener.java @@ -11,7 +11,7 @@ import java.util.TreeMap; public class SlashCommandListener extends ListenerAdapter { - // map storing command label and command object alphabetically. + // map that stores command label and command object alphabetically. private final TreeMap registeredCommands = new TreeMap<>(); public void registerCommand(SlashCommand command) diff --git a/src/main/java/wtf/beatrice/hidekobot/objects/commands/SlashArgumentsCompleter.java b/src/main/java/wtf/beatrice/hidekobot/objects/commands/SlashArgumentsCompleter.java new file mode 100644 index 0000000..b03091a --- /dev/null +++ b/src/main/java/wtf/beatrice/hidekobot/objects/commands/SlashArgumentsCompleter.java @@ -0,0 +1,21 @@ +package wtf.beatrice.hidekobot.objects.commands; + +import net.dv8tion.jda.api.events.interaction.command.CommandAutoCompleteInteractionEvent; +import org.jetbrains.annotations.NotNull; + +public interface SlashArgumentsCompleter +{ + + /** + * Get the parent slash command's object. + * + * @return the command object. + */ + SlashCommand getCommand(); + /** + * Run the argument-completion logic by parsing the event and replying accordingly. + * + * @param event the received auto-complete event. + */ + void runCompletion(@NotNull CommandAutoCompleteInteractionEvent event); +} diff --git a/src/main/java/wtf/beatrice/hidekobot/objects/commands/SlashArgumentsCompleterImpl.java b/src/main/java/wtf/beatrice/hidekobot/objects/commands/SlashArgumentsCompleterImpl.java new file mode 100644 index 0000000..b16715d --- /dev/null +++ b/src/main/java/wtf/beatrice/hidekobot/objects/commands/SlashArgumentsCompleterImpl.java @@ -0,0 +1,21 @@ +package wtf.beatrice.hidekobot.objects.commands; + +import net.dv8tion.jda.api.events.interaction.command.CommandAutoCompleteInteractionEvent; +import org.jetbrains.annotations.NotNull; + +public class SlashArgumentsCompleterImpl implements SlashArgumentsCompleter +{ + private final SlashCommand parentCommand; + public SlashArgumentsCompleterImpl(SlashCommand parentCommand) + { + this.parentCommand = parentCommand; + } + + public SlashCommand getCommand() + { return parentCommand; } + + public void runCompletion(@NotNull CommandAutoCompleteInteractionEvent event) + { + return; + } +}