From 673f3a22d0de76ec38772ad2bc29935daa796609 Mon Sep 17 00:00:00 2001 From: Azzerial Date: Sun, 13 Jun 2021 07:49:48 +0100 Subject: [PATCH 1/3] updated: README.md --- README.md | 46 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) diff --git a/README.md b/README.md index 61929b4..8fb8d3c 100644 --- a/README.md +++ b/README.md @@ -6,9 +6,20 @@

Slash Commands

An open source utility library for JDA to simplify the use of Discord Slash Commands

+ +

+ + + + + + +

+

FeaturesHow To Use • + InstallationLicense

@@ -57,6 +68,41 @@ public final class SlashCommand { *For more examples and usage, please refer to the [playground module](playground/).* +## Installation + +This project uses [Jitpack](https://jitpack.io/#azzerial/slash-commands). + +Latest release: [![](https://jitpack.io/v/azzerial/slash-commands.svg)](https://jitpack.io/#azzerial/slash-commands) + +### Gradle + +```groovy +repositories { + maven { url 'https://jitpack.io' } +} + +dependencies { + implementation 'com.github.azzerial.slash-commands:api:1.0' +} +``` + +### Maven + +```xml + + + jitpack.io + https://jitpack.io + + + + + com.github.azzerial.slash-commands + api + 1.0 + +``` + ## License This project is licensed under the [Apache License 2.0](LICENSE) © 2021 [Robin Mercier](https://github.com/azzerial/). From 3d0172bc3644856bf8526bbeca79cd6dc30a62aa Mon Sep 17 00:00:00 2001 From: Azzerial Date: Tue, 15 Jun 2021 07:30:53 +0100 Subject: [PATCH 2/3] updated: changed README.md footer and how to use example --- README.md | 26 +++++++++++++------------- 1 file changed, 13 insertions(+), 13 deletions(-) diff --git a/README.md b/README.md index 8fb8d3c..dcffb7b 100644 --- a/README.md +++ b/README.md @@ -38,37 +38,37 @@ 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(); } } ``` -*For more examples and usage, please refer to the [playground module](playground/).* +*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 -

\ No newline at end of file + Slash Commands by @Azzerial +

From 440ec37490fde6bfbbc3c7af2a7a0cbd610af68e Mon Sep 17 00:00:00 2001 From: Azzerial Date: Tue, 15 Jun 2021 11:01:31 +0100 Subject: [PATCH 3/3] updated: added fallbacks and globbing to the command callbacks compilation in AnnotationCompiler class --- .../slash/internal/AnnotationCompiler.java | 56 ++++++++++--------- 1 file changed, 30 insertions(+), 26 deletions(-) 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; } - }