Skip to content

Commit

Permalink
feat: auto-complete improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
yaansz committed Aug 26, 2024
1 parent a60fafe commit 9bb0780
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 16 deletions.
14 changes: 12 additions & 2 deletions src/main/java/com/softawii/curupira/example/controller/Foo.java
Original file line number Diff line number Diff line change
Expand Up @@ -61,14 +61,15 @@ public TextLocaleResponse charlie(
@DiscordChoice(name = "bar"),
@DiscordChoice(name = "baz")
}) String name,
@DiscordParameter(name= "profession", description = "your profession", autoComplete = true) String profession) {
@DiscordParameter(name = "occupation", description = "your occupation", autoComplete = true) String occupation,
@DiscordParameter(name = "food", description = "your favorite food", autoComplete = true) String food) {

return new TextLocaleResponse("foo.bar.charlie.response.ok", name);
}

@DiscordAutoComplete(name = "charlie")
public Command.Choice[] charlieAutoComplete(AutoCompleteQuery query) {
if(query.getName().equals("profession")) {
if(query.getName().equals("occupation")) {
return new Command.Choice[] {
new Command.Choice("developer", "developer"),
new Command.Choice("designer", "designer"),
Expand All @@ -77,4 +78,13 @@ public Command.Choice[] charlieAutoComplete(AutoCompleteQuery query) {
}
return new Command.Choice[0];
}

@DiscordAutoComplete(name = "charlie", variable = "food")
public Command.Choice[] charlieAutoCompleteFood(AutoCompleteQuery query) {
return new Command.Choice[]{
new Command.Choice("pizza", "pizza"),
new Command.Choice("hamburger", "hamburger"),
new Command.Choice("hotdog", "hotdog")
};
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,9 @@
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;


// TODO: v2.1 specify target variable (like), Foo Variable1 has one function, Foo Variable2 has another function
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface DiscordAutoComplete {
String name() default "";
String variable() default "";
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
package com.softawii.curupira.v2.core.command;

import com.softawii.curupira.v2.annotations.DiscordChoice;
import com.softawii.curupira.v2.annotations.DiscordCommand;
import com.softawii.curupira.v2.annotations.DiscordController;
import com.softawii.curupira.v2.annotations.DiscordParameter;
import com.softawii.curupira.v2.annotations.*;
import com.softawii.curupira.v2.api.TextLocaleResponse;
import com.softawii.curupira.v2.core.exception.MissingPermissionsException;
import com.softawii.curupira.v2.enums.DiscordEnvironment;
Expand All @@ -28,7 +25,9 @@
import java.lang.reflect.Method;
import java.lang.reflect.Parameter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

class CommandHandler {
private static final ChannelType[] PRIVATE_CHANNELS = {
Expand All @@ -40,7 +39,7 @@ class CommandHandler {
private final JDA jda;
private final Object instance;
private final Method command;
private Method autoComplete;
private Map<String, Method> autoComplete;
// i18n
private final LocalizationManager localization;
private final DiscordEnvironment environment;
Expand All @@ -54,6 +53,7 @@ public CommandHandler(JDA jda, Object instance, Method command, LocalizationFunc
this.instance = instance;
this.localization = new LocalizationManager(localization, defaultLocale);
this.environment = environment;
this.autoComplete = new HashMap<>();

register();
}
Expand All @@ -70,11 +70,11 @@ public LocalizationManager getLocalization() {
return localization;
}

public void addAutoComplete(Method autoComplete) {
if(this.autoComplete != null) {
public void addAutoComplete(Method autoComplete, DiscordAutoComplete autoCompleteInfo) {
if(this.autoComplete.containsKey(autoCompleteInfo.variable())) {
throw new RuntimeException("AutoComplete method already set");
}
this.autoComplete = autoComplete;
this.autoComplete.put(autoCompleteInfo.variable(), autoComplete);
}

public String getFullCommandName() {
Expand Down Expand Up @@ -182,11 +182,20 @@ public void execute(GenericCommandInteractionEvent event) throws InvocationTarge
}

public void autoComplete(CommandAutoCompleteInteractionEvent event) throws InvocationTargetException, IllegalAccessException {
if(autoComplete == null) {
if(autoComplete.isEmpty()) {
throw new RuntimeException("AutoComplete method not set");
}

Command.Choice[] choices = (Command.Choice[]) autoComplete.invoke(instance, getParameters(event, autoComplete));
String variable = event.getFocusedOption().getName();

Method defaultHandler = autoComplete.getOrDefault("", null);
Method finalHandler = this.autoComplete.getOrDefault(variable, defaultHandler);

if(finalHandler == null) {
throw new RuntimeException("AutoComplete method not found");
}

Command.Choice[] choices = (Command.Choice[]) finalHandler.invoke(instance, getParameters(event, finalHandler));
event.replyChoices(choices).queue();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -131,12 +131,14 @@ private void findAutoComplete(Class clazz) {
for(Method method : methods) {
this.logger.info("findAutoComplete: Found method: {}", method.getName());
// get key from method name
String key = getAutoCompleteKey(controllerInfo, method.getAnnotation(DiscordAutoComplete.class));
DiscordAutoComplete autoComplete = method.getAnnotation(DiscordAutoComplete.class);
String key = getAutoCompleteKey(controllerInfo, autoComplete);
if(!this.commands.containsKey(key)) {
this.logger.error("findAutoComplete: Command not found: {}", key);
throw new RuntimeException("Command not found to set autocomplete: " + key);
}
this.commands.get(key).addAutoComplete(method);
this.logger.info("findAutoComplete: Registering autocomplete to command: {}, variable: {}", key, autoComplete.variable());
this.commands.get(key).addAutoComplete(method, autoComplete);
}
}

Expand Down

0 comments on commit 9bb0780

Please sign in to comment.