|
| 1 | +package net.javadiscord.javabot2.command; |
| 2 | + |
| 3 | +import org.javacord.api.interaction.SlashCommandInteraction; |
| 4 | +import org.javacord.api.interaction.callback.InteractionImmediateResponseBuilder; |
| 5 | + |
| 6 | +import java.util.Collections; |
| 7 | +import java.util.HashMap; |
| 8 | +import java.util.Map; |
| 9 | + |
| 10 | +/** |
| 11 | + * Abstract command handler which is useful for commands which consist of lots |
| 12 | + * of subcommands. A child class will supply a map of subcommand handlers, so |
| 13 | + * that this parent handler can do the logic of finding the right subcommand to |
| 14 | + * invoke depending on the event received. |
| 15 | + */ |
| 16 | +public class DelegatingCommandHandler implements SlashCommandHandler { |
| 17 | + private final Map<String, SlashCommandHandler> subcommandHandlers; |
| 18 | + private final Map<String, SlashCommandHandler> subcommandGroupHandlers; |
| 19 | + |
| 20 | + /** |
| 21 | + * Constructs the handler with an already-initialized map of subcommands. |
| 22 | + * @param subcommandHandlers The map of subcommands to use. |
| 23 | + */ |
| 24 | + public DelegatingCommandHandler(Map<String, SlashCommandHandler> subcommandHandlers) { |
| 25 | + this.subcommandHandlers = subcommandHandlers; |
| 26 | + this.subcommandGroupHandlers = new HashMap<>(); |
| 27 | + } |
| 28 | + |
| 29 | + /** |
| 30 | + * Constructs the handler with an empty map, which subcommands can be added |
| 31 | + * to via {@link DelegatingCommandHandler#addSubcommand(String, SlashCommandHandler)}. |
| 32 | + */ |
| 33 | + public DelegatingCommandHandler() { |
| 34 | + this.subcommandHandlers = new HashMap<>(); |
| 35 | + this.subcommandGroupHandlers = new HashMap<>(); |
| 36 | + } |
| 37 | + |
| 38 | + /** |
| 39 | + * Gets an unmodifiable map of the subcommand handlers this delegating |
| 40 | + * handler has registered. |
| 41 | + * @return An unmodifiable map containing all registered subcommands. |
| 42 | + */ |
| 43 | + public Map<String, SlashCommandHandler> getSubcommandHandlers() { |
| 44 | + return Collections.unmodifiableMap(this.subcommandHandlers); |
| 45 | + } |
| 46 | + |
| 47 | + /** |
| 48 | + * Gets an unmodifiable map of the subcommand group handlers that this |
| 49 | + * handler has registered. |
| 50 | + * @return An unmodifiable map containing all registered group handlers. |
| 51 | + */ |
| 52 | + public Map<String, SlashCommandHandler> getSubcommandGroupHandlers() { |
| 53 | + return Collections.unmodifiableMap(this.subcommandGroupHandlers); |
| 54 | + } |
| 55 | + |
| 56 | + /** |
| 57 | + * Adds a subcommand to this handler. |
| 58 | + * @param name The name of the subcommand. <em>This is case-sensitive.</em> |
| 59 | + * @param handler The handler that will be called to handle subcommands with |
| 60 | + * the given name. |
| 61 | + * @throws UnsupportedOperationException If this handler was initialized |
| 62 | + * with an unmodifiable map of subcommand handlers. |
| 63 | + */ |
| 64 | + protected void addSubcommand(String name, SlashCommandHandler handler) { |
| 65 | + this.subcommandHandlers.put(name, handler); |
| 66 | + } |
| 67 | + |
| 68 | + /** |
| 69 | + * Adds a subcommand group handler to this handler. |
| 70 | + * @param name The name of the subcommand group. <em>This is case-sensitive.</em> |
| 71 | + * @param handler The handler that will be called to handle commands within |
| 72 | + * the given subcommand's name. |
| 73 | + * @throws UnsupportedOperationException If this handler was initialized |
| 74 | + * with an unmodifiable map of subcommand group handlers. |
| 75 | + */ |
| 76 | + protected void addSubcommandGroup(String name, SlashCommandHandler handler) { |
| 77 | + this.subcommandGroupHandlers.put(name, handler); |
| 78 | + } |
| 79 | + |
| 80 | + /** |
| 81 | + * Handles the case where the main command is called without any subcommand. |
| 82 | + * @param interaction The event. |
| 83 | + * @return The reply action that is sent to the user. |
| 84 | + */ |
| 85 | + protected InteractionImmediateResponseBuilder handleNonSubcommand(SlashCommandInteraction interaction) { |
| 86 | + return Responses.warning(interaction, "Missing Subcommand", "Please specify a subcommand."); |
| 87 | + } |
| 88 | + |
| 89 | + /** |
| 90 | + * Handles a slash command interaction. |
| 91 | + * |
| 92 | + * @param interaction The interaction. |
| 93 | + * @return An immediate response to the interaction. |
| 94 | + * @throws ResponseException If an error occurs while handling the event. |
| 95 | + */ |
| 96 | + @Override |
| 97 | + public InteractionImmediateResponseBuilder handle(SlashCommandInteraction interaction) throws ResponseException { |
| 98 | + // First we check if the event has specified a subcommand group, and if we have a group handler for it. |
| 99 | + var subCommandOption = interaction.getOptionByIndex(0); |
| 100 | + if (subCommandOption.isPresent() && subCommandOption.get().isSubcommandOrGroup()) { |
| 101 | + SlashCommandHandler groupHandler = this.getSubcommandGroupHandlers().get(subCommandOption.get().getName()); |
| 102 | + if (groupHandler != null) return groupHandler.handle(interaction); |
| 103 | + |
| 104 | + } |
| 105 | + return Responses.warning(interaction, "Delegating command handler is not yet implemented."); |
| 106 | + |
| 107 | +// if (interaction.getCommandName() != null) { |
| 108 | +// SlashCommandHandler groupHandler = this.getSubcommandGroupHandlers().get(event.getSubcommandGroup()); |
| 109 | +// if (groupHandler != null) { |
| 110 | +// return groupHandler.handle(event); |
| 111 | +// } |
| 112 | +// } |
| 113 | +// // If the event doesn't have a subcommand group, or no handler was found for the group, we just move on to the subcommand. |
| 114 | +// if (event.getSubcommandName() == null) { |
| 115 | +// return this.handleNonSubcommand(event); |
| 116 | +// } else { |
| 117 | +// SlashCommandHandler handler = this.getSubcommandHandlers().get(event.getSubcommandName()); |
| 118 | +// if (handler != null) { |
| 119 | +// return handler.handle(event); |
| 120 | +// } else { |
| 121 | +// return Responses.warning(event, "Unknown Subcommand", "The subcommand you entered could not be found."); |
| 122 | +// } |
| 123 | +// } |
| 124 | + } |
| 125 | +} |
0 commit comments