Skip to content

Commit 7b9c0f4

Browse files
authored
Merge pull request #2 from Azzerial/development
hot-fix: v1.0 - Slash Command callbacks
2 parents 6b5ed87 + ad7a8bc commit 7b9c0f4

File tree

2 files changed

+41
-37
lines changed

2 files changed

+41
-37
lines changed

README.md

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -38,37 +38,37 @@ A few of the things you can do with Slash Commands:
3838
## How to Use
3939

4040
```java
41-
@Slash.Tag("slash_cmd")
41+
@Slash.Tag("ping_command")
4242
@Slash.Command(
43-
name = "slash-command",
44-
description = "A proof of concept Slash Command"
43+
name = "ping",
44+
description = "Check if the application is online"
4545
)
46-
public final class SlashCommand {
46+
public class PingCommand {
4747

4848
public static void main(String[] args) throws LoginException, InterruptedException {
4949
final JDA jda = JDABuilder.createDefault(...)
5050
.build()
5151
.awaitReady();
5252
final SlashClient slash = SlashClientBuilder.create(jda)
53-
.addCommand(new SlashCommand()) // register your commands
53+
.addCommand(new PingCommand()) // register the ping command
5454
.build();
5555

56-
slash.getCommand("slash_cmd") // get a SlashCommand by it's @Slash.Tag
57-
.upsertGuild(...); // upsert as a guild Slash Command
56+
slash.getCommand("ping_command") // get the ping command by it's @Slash.Tag
57+
.upsertGuild(...); // upsert it as a guild Slash Command
5858
}
5959

60-
@Slash.Handler
60+
@Slash.Handler()
6161
public void callback(SlashCommandEvent event) {
6262
event.deferReply()
63-
.setContent("Hello World!")
63+
.setContent("pong!")
6464
.queue();
6565
}
6666
}
6767
```
6868

6969
*For more examples and usage guides, please refer to the [wiki](https://github.com/Azzerial/slash-commands/wiki) and the [playground module](playground/).*
7070

71-
## Installation
71+
## Installation
7272

7373
This project uses [Jitpack](https://jitpack.io/#azzerial/slash-commands).
7474

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

112112
<p align="center">
113-
GitHub <a href="https://github.com/azzerial">@Azzerial</a>
113+
Slash Commands by <a href="https://github.com/azzerial">@Azzerial</a>
114114
</p>

api/src/main/java/com/github/azzerial/slash/internal/AnnotationCompiler.java

Lines changed: 30 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -75,15 +75,29 @@ public Map<String, Method> compileHandlers(Class<?> cls, CommandData data) {
7575
&& method.getParameterTypes()[0] == SlashCommandEvent.class
7676
)
7777
.collect(Collectors.toList());
78-
final Map<String, List<Method>> handlers = buildHandlersMap(methods);
79-
80-
checkHandlers(data, handlers);
81-
return new HashMap<String, Method>() {{
82-
handlers.forEach((k, v) -> put(
83-
data.getName() + (k.isEmpty() ? "" : "/" + k),
84-
v.get(0)
85-
));
86-
}};
78+
final Map<String, Method> handlers = buildHandlers(cls, methods);
79+
final Set<String> paths = buildPaths(data);
80+
final Map<String, Method> mappings = new HashMap<>();
81+
82+
for (String path : paths) {
83+
final String commandPath = path.isEmpty() ? data.getName() : data.getName() + "/" + path;
84+
85+
if (handlers.containsKey(path)) {
86+
mappings.put(commandPath, handlers.get(path));
87+
continue;
88+
}
89+
90+
final String[] parts = path.split("/");
91+
92+
if (parts.length == 2 && handlers.containsKey("*/" + parts[1])) {
93+
mappings.put(commandPath, handlers.get("*/" + parts[1]));
94+
} else if (parts.length == 2 && handlers.containsKey(parts[0])) {
95+
mappings.put(commandPath, handlers.get(parts[0]));
96+
} else if (handlers.containsKey("")) {
97+
mappings.put(commandPath, handlers.get(""));
98+
}
99+
}
100+
return mappings;
87101
}
88102

89103
/* Internal */
@@ -134,21 +148,22 @@ private SubcommandGroupData compileSubcommandGroup(SubcommandGroup subcommandGro
134148
);
135149
}
136150

137-
private Map<String, List<Method>> buildHandlersMap(List<Method> methods) {
138-
final Map<String, List<Method>> handlers = new HashMap<>();
151+
private Map<String, Method> buildHandlers(Class<?> cls, List<Method> methods) {
152+
final Map<String, Method> handlers = new HashMap<>();
139153

140154
for (Method method : methods) {
141155
final Slash.Handler handler = method.getAnnotation(Slash.Handler.class);
142156

143157
if (!handlers.containsKey(handler.value())) {
144-
handlers.put(handler.value(), new LinkedList<>());
158+
handlers.put(handler.value(), method);
159+
} else {
160+
throw new IllegalArgumentException("Multiple handlers were declared for the '" + handler.value() + "' command path in " + cls.getSimpleName() + ".class!");
145161
}
146-
handlers.get(handler.value()).add(method);
147162
}
148163
return handlers;
149164
}
150165

151-
private void checkHandlers(CommandData data, Map<String, List<Method>> handlers) {
166+
private Set<String> buildPaths(CommandData data) {
152167
final Set<String> paths = new HashSet<>();
153168

154169
if (!data.getSubcommandGroups().isEmpty()) {
@@ -164,17 +179,6 @@ private void checkHandlers(CommandData data, Map<String, List<Method>> handlers)
164179
} else {
165180
paths.add("");
166181
}
167-
168-
for (String path : handlers.keySet()) {
169-
final List<Method> methods = handlers.get(path);
170-
171-
if (!paths.contains(path)) {
172-
throw new IllegalArgumentException("Could not find the '" + path + "' command path in '" + data.getName() + "'!");
173-
}
174-
if (methods.size() != 1) {
175-
throw new IllegalArgumentException("Multiple handlers were declared for the '" + path + "' command path in '" + data.getName() + "'!");
176-
}
177-
}
182+
return paths;
178183
}
179-
180184
}

0 commit comments

Comments
 (0)