Skip to content

Commit

Permalink
Merge pull request #2 from Azzerial/development
Browse files Browse the repository at this point in the history
hot-fix: v1.0 - Slash Command callbacks
  • Loading branch information
azzerial authored Jun 16, 2021
2 parents 6b5ed87 + ad7a8bc commit 7b9c0f4
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 37 deletions.
22 changes: 11 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 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).

Expand Down Expand Up @@ -110,5 +110,5 @@ This project is licensed under the [Apache License 2.0](LICENSE) © 2021 [Robin
---

<p align="center">
GitHub <a href="https://github.com/azzerial">@Azzerial</a>
Slash Commands by <a href="https://github.com/azzerial">@Azzerial</a>
</p>
Original file line number Diff line number Diff line change
Expand Up @@ -75,15 +75,29 @@ public Map<String, Method> compileHandlers(Class<?> cls, CommandData data) {
&& method.getParameterTypes()[0] == SlashCommandEvent.class
)
.collect(Collectors.toList());
final Map<String, List<Method>> handlers = buildHandlersMap(methods);

checkHandlers(data, handlers);
return new HashMap<String, Method>() {{
handlers.forEach((k, v) -> put(
data.getName() + (k.isEmpty() ? "" : "/" + k),
v.get(0)
));
}};
final Map<String, Method> handlers = buildHandlers(cls, methods);
final Set<String> paths = buildPaths(data);
final Map<String, Method> 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 */
Expand Down Expand Up @@ -134,21 +148,22 @@ private SubcommandGroupData compileSubcommandGroup(SubcommandGroup subcommandGro
);
}

private Map<String, List<Method>> buildHandlersMap(List<Method> methods) {
final Map<String, List<Method>> handlers = new HashMap<>();
private Map<String, Method> buildHandlers(Class<?> cls, List<Method> methods) {
final Map<String, Method> 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<String, List<Method>> handlers) {
private Set<String> buildPaths(CommandData data) {
final Set<String> paths = new HashSet<>();

if (!data.getSubcommandGroups().isEmpty()) {
Expand All @@ -164,17 +179,6 @@ private void checkHandlers(CommandData data, Map<String, List<Method>> handlers)
} else {
paths.add("");
}

for (String path : handlers.keySet()) {
final List<Method> 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;
}

}

0 comments on commit 7b9c0f4

Please sign in to comment.