diff --git a/README.md b/README.md index 777c8ed..dcffb7b 100644 --- a/README.md +++ b/README.md @@ -38,29 +38,29 @@ A few of the things you can do with Slash Commands: ## How to Use ```java -@Slash.Tag("slash_cmd") +@Slash.Tag("ping_command") @Slash.Command( - name = "slash-command", - description = "A proof of concept Slash Command" + name = "ping", + description = "Check if the application is online" ) -public final class SlashCommand { +public class PingCommand { public static void main(String[] args) throws LoginException, InterruptedException { final JDA jda = JDABuilder.createDefault(...) .build() .awaitReady(); final SlashClient slash = SlashClientBuilder.create(jda) - .addCommand(new SlashCommand()) // register your commands + .addCommand(new PingCommand()) // register the ping command .build(); - slash.getCommand("slash_cmd") // get a SlashCommand by it's @Slash.Tag - .upsertGuild(...); // upsert as a guild Slash Command + slash.getCommand("ping_command") // get the ping command by it's @Slash.Tag + .upsertGuild(...); // upsert it as a guild Slash Command } - @Slash.Handler + @Slash.Handler() public void callback(SlashCommandEvent event) { event.deferReply() - .setContent("Hello World!") + .setContent("pong!") .queue(); } } @@ -68,7 +68,7 @@ public final class SlashCommand { *For more examples and usage guides, please refer to the [wiki](https://github.com/Azzerial/slash-commands/wiki) and the [playground module](playground/).* -## Installation +## Installation This project uses [Jitpack](https://jitpack.io/#azzerial/slash-commands). @@ -110,5 +110,5 @@ This project is licensed under the [Apache License 2.0](LICENSE) © 2021 [Robin ---

- GitHub @Azzerial + Slash Commands by @Azzerial

diff --git a/api/src/main/java/com/github/azzerial/slash/internal/AnnotationCompiler.java b/api/src/main/java/com/github/azzerial/slash/internal/AnnotationCompiler.java index 1d38080..d059f40 100644 --- a/api/src/main/java/com/github/azzerial/slash/internal/AnnotationCompiler.java +++ b/api/src/main/java/com/github/azzerial/slash/internal/AnnotationCompiler.java @@ -75,15 +75,29 @@ public Map compileHandlers(Class cls, CommandData data) { && method.getParameterTypes()[0] == SlashCommandEvent.class ) .collect(Collectors.toList()); - final Map> handlers = buildHandlersMap(methods); - - checkHandlers(data, handlers); - return new HashMap() {{ - handlers.forEach((k, v) -> put( - data.getName() + (k.isEmpty() ? "" : "/" + k), - v.get(0) - )); - }}; + final Map handlers = buildHandlers(cls, methods); + final Set paths = buildPaths(data); + final Map mappings = new HashMap<>(); + + for (String path : paths) { + final String commandPath = path.isEmpty() ? data.getName() : data.getName() + "/" + path; + + if (handlers.containsKey(path)) { + mappings.put(commandPath, handlers.get(path)); + continue; + } + + final String[] parts = path.split("/"); + + if (parts.length == 2 && handlers.containsKey("*/" + parts[1])) { + mappings.put(commandPath, handlers.get("*/" + parts[1])); + } else if (parts.length == 2 && handlers.containsKey(parts[0])) { + mappings.put(commandPath, handlers.get(parts[0])); + } else if (handlers.containsKey("")) { + mappings.put(commandPath, handlers.get("")); + } + } + return mappings; } /* Internal */ @@ -134,21 +148,22 @@ private SubcommandGroupData compileSubcommandGroup(SubcommandGroup subcommandGro ); } - private Map> buildHandlersMap(List methods) { - final Map> handlers = new HashMap<>(); + private Map buildHandlers(Class cls, List methods) { + final Map handlers = new HashMap<>(); for (Method method : methods) { final Slash.Handler handler = method.getAnnotation(Slash.Handler.class); if (!handlers.containsKey(handler.value())) { - handlers.put(handler.value(), new LinkedList<>()); + handlers.put(handler.value(), method); + } else { + throw new IllegalArgumentException("Multiple handlers were declared for the '" + handler.value() + "' command path in " + cls.getSimpleName() + ".class!"); } - handlers.get(handler.value()).add(method); } return handlers; } - private void checkHandlers(CommandData data, Map> handlers) { + private Set buildPaths(CommandData data) { final Set paths = new HashSet<>(); if (!data.getSubcommandGroups().isEmpty()) { @@ -164,17 +179,6 @@ private void checkHandlers(CommandData data, Map> handlers) } else { paths.add(""); } - - for (String path : handlers.keySet()) { - final List methods = handlers.get(path); - - if (!paths.contains(path)) { - throw new IllegalArgumentException("Could not find the '" + path + "' command path in '" + data.getName() + "'!"); - } - if (methods.size() != 1) { - throw new IllegalArgumentException("Multiple handlers were declared for the '" + path + "' command path in '" + data.getName() + "'!"); - } - } + return paths; } - }