Finish command completion listener implementation
All checks were successful
continuous-integration/drone/push Build is passing
All checks were successful
continuous-integration/drone/push Build is passing
Very similarly to how the slash command interface works, now a slash command auto-completion interface also exists, with its respective listener.
This commit is contained in:
parent
ff084cf8e8
commit
50ccda214f
@ -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; }
|
||||
|
||||
|
@ -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)
|
||||
|
@ -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"))
|
||||
{
|
||||
|
@ -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);
|
||||
}
|
||||
}
|
||||
}
|
@ -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<String, SlashArgumentsCompleter> 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<SlashArgumentsCompleter> 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);
|
||||
}
|
||||
}
|
@ -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<String, SlashCommand> registeredCommands = new TreeMap<>();
|
||||
|
||||
public void registerCommand(SlashCommand command)
|
||||
|
@ -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);
|
||||
}
|
@ -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;
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user