enabling auto complete for youtube command

This commit is contained in:
Sheldan
2022-07-27 22:13:48 +02:00
parent 51edb50eee
commit f9b85feeaf
17 changed files with 238 additions and 7 deletions

View File

@@ -82,6 +82,11 @@ public class ListenerExecutorConfig {
return executorService.setupExecutorFor("slashCommandListener");
}
@Bean(name = "slashCommandAutoCompleteExecutor")
public TaskExecutor slashCommandAutoCompleteExecutor() {
return executorService.setupExecutorFor("slashCommandAutoCompleteListener");
}
@Bean(name = "emoteDeletedExecutor")
public TaskExecutor emoteDeletedExecutor() {
return executorService.setupExecutorFor("emoteDeletedListener");

View File

@@ -12,6 +12,7 @@ import dev.sheldan.abstracto.core.metric.service.MetricService;
import dev.sheldan.abstracto.core.metric.service.MetricTag;
import lombok.Getter;
import lombok.extern.slf4j.Slf4j;
import net.dv8tion.jda.api.events.interaction.command.CommandAutoCompleteInteractionEvent;
import net.dv8tion.jda.api.events.interaction.command.SlashCommandInteractionEvent;
import net.dv8tion.jda.api.hooks.ListenerAdapter;
import org.jetbrains.annotations.NotNull;
@@ -41,6 +42,10 @@ public class SlashCommandListenerBean extends ListenerAdapter {
@Qualifier("slashCommandExecutor")
private TaskExecutor slashCommandExecutor;
@Autowired
@Qualifier("slashCommandAutoCompleteExecutor")
private TaskExecutor slashCommandAutoCompleteExecutor;
@Autowired
private SlashCommandListenerBean self;
@@ -100,6 +105,29 @@ public class SlashCommandListenerBean extends ListenerAdapter {
});
}
@Override
public void onCommandAutoCompleteInteraction(@NotNull CommandAutoCompleteInteractionEvent event) {
if(commands == null || commands.isEmpty()) return;
CompletableFuture.runAsync(() -> self.executeAutCompleteListenerLogic(event), slashCommandAutoCompleteExecutor).exceptionally(throwable -> {
log.error("Failed to execute listener logic in async auto complete interaction event.", throwable);
return null;
});
}
@Transactional
public void executeAutCompleteListenerLogic(CommandAutoCompleteInteractionEvent event) {
Optional<Command> potentialCommand = findCommand(event);
potentialCommand.ifPresent(command -> {
try {
List<String> replies = command.performAutoComplete(event);
event.replyChoiceStrings(replies).queue(unused -> {},
throwable -> log.error("Failed to response to complete of command {} in guild {}.", command.getConfiguration().getName(), event.getGuild().getIdLong()));
} catch (Exception exception) {
log.error("Error while executing autocomplete of command {}.", command.getConfiguration().getName(), exception);
}
});
}
@Transactional(rollbackFor = AbstractoRunTimeException.class)
public void executeCommand(SlashCommandInteractionEvent event, Command command, ConditionResult conditionResult) {
CompletableFuture<CommandResult> commandOutput;
@@ -136,6 +164,14 @@ public class SlashCommandListenerBean extends ListenerAdapter {
.findAny();
}
private Optional<Command> findCommand(CommandAutoCompleteInteractionEvent event) {
return commands
.stream()
.filter(command -> command.getConfiguration().getSlashCommandConfig().isEnabled())
.filter(command -> command.getConfiguration().getSlashCommandConfig().matchesInteraction(event.getInteraction()))
.findAny();
}
@PostConstruct
public void filterPostProcessors() {
metricService.registerCounter(SLASH_COMMANDS_PROCESSED_COUNTER, "Slash Commands processed");

View File

@@ -124,11 +124,7 @@ public class SlashCommandServiceBean implements SlashCommandService {
optionalParameters.add(new OptionData(type, parameter.getSlashCompatibleName() + "_" + i, parameterDescription, false));
}
} else {
if(!parameter.isOptional()) {
requiredParameters.add(new OptionData(type, parameter.getSlashCompatibleName(), parameterDescription, true));
} else {
optionalParameters.add(new OptionData(type, parameter.getSlashCompatibleName(), parameterDescription, false));
}
requiredParameters.add(new OptionData(type, parameter.getSlashCompatibleName(), parameterDescription, !parameter.isOptional(), parameter.getSupportsAutoComplete()));
}
}
});

View File

@@ -0,0 +1,12 @@
package dev.sheldan.abstracto.core.interaction.slash.parameter;
import net.dv8tion.jda.api.interactions.AutoCompleteQuery;
import org.springframework.stereotype.Component;
@Component
public class SlashCommandAutoCompleteServiceBean implements SlashCommandAutoCompleteService{
@Override
public boolean matchesParameter(AutoCompleteQuery query, String parameterName) {
return query.getName().equalsIgnoreCase(parameterName);
}
}

View File

@@ -15,6 +15,16 @@
<heap unit="entries">2000</heap>
</resources>
</cache>
<!-- TODO no nice way yet to have this configuration split up -->
<cache uses-template="default" alias="general-use-cache">
<expiry>
<ttl unit="seconds">7200</ttl>
</expiry>
<resources>
<heap unit="entries">2000</heap>
</resources>
</cache>
<cache-template name="default">
<expiry>
<ttl unit="seconds">600</ttl>

View File

@@ -4,8 +4,11 @@ import dev.sheldan.abstracto.core.FeatureAware;
import dev.sheldan.abstracto.core.command.config.CommandConfiguration;
import dev.sheldan.abstracto.core.command.execution.CommandContext;
import dev.sheldan.abstracto.core.command.execution.CommandResult;
import net.dv8tion.jda.api.events.interaction.command.CommandAutoCompleteInteractionEvent;
import net.dv8tion.jda.api.events.interaction.command.SlashCommandInteractionEvent;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.CompletableFuture;
public interface Command extends FeatureAware {
@@ -13,5 +16,6 @@ public interface Command extends FeatureAware {
default CommandResult execute(CommandContext commandContext) {return CommandResult.fromSuccess();}
default CompletableFuture<CommandResult> executeAsync(CommandContext commandContext) {return CompletableFuture.completedFuture(CommandResult.fromSuccess());}
default CompletableFuture<CommandResult> executeSlash(SlashCommandInteractionEvent event) { return CompletableFuture.completedFuture(CommandResult.fromSuccess());}
default List<String> performAutoComplete(CommandAutoCompleteInteractionEvent event) { return new ArrayList<>(); }
CommandConfiguration getConfiguration();
}

View File

@@ -29,6 +29,8 @@ public class Parameter implements Serializable {
@Builder.Default
private Integer listSize = 0;
@Builder.Default
private Boolean supportsAutoComplete = false;
@Builder.Default
private List<ParameterValidator> validators = new ArrayList<>();
@Builder.Default
private Map<String, Object> additionalInfo = new HashMap<>();

View File

@@ -0,0 +1,7 @@
package dev.sheldan.abstracto.core.interaction.slash.parameter;
import net.dv8tion.jda.api.interactions.AutoCompleteQuery;
public interface SlashCommandAutoCompleteService {
boolean matchesParameter(AutoCompleteQuery query, String parameterName);
}