[AB-xxx] actively limiting auto complete responses to the max allowed values

changing interface of slash command parameter service to be more applicable
adding utility functions to slash command auto complete service bean
This commit is contained in:
Sheldan
2023-10-29 18:21:29 +01:00
parent 208d9a28ed
commit d53126e5a5
6 changed files with 48 additions and 21 deletions

View File

@@ -15,6 +15,7 @@ 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 net.dv8tion.jda.api.interactions.commands.build.OptionData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.core.task.TaskExecutor;
@@ -129,7 +130,8 @@ public class SlashCommandListenerBean extends ListenerAdapter {
Optional<Command> potentialCommand = findCommand(event);
potentialCommand.ifPresent(command -> {
try {
List<String> replies = command.performAutoComplete(event);
List<String> fullRepliesList = command.performAutoComplete(event);
List<String> replies = fullRepliesList.subList(0, Math.min(fullRepliesList.size(), OptionData.MAX_CHOICES));
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) {

View File

@@ -1,12 +1,33 @@
package dev.sheldan.abstracto.core.interaction.slash.parameter;
import net.dv8tion.jda.api.events.interaction.command.CommandAutoCompleteInteractionEvent;
import net.dv8tion.jda.api.interactions.AutoCompleteQuery;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
@Component
public class SlashCommandAutoCompleteServiceBean implements SlashCommandAutoCompleteService{
public class SlashCommandAutoCompleteServiceBean implements SlashCommandAutoCompleteService {
@Autowired
private SlashCommandParameterService slashCommandParameterServiceBean;
@Override
public boolean matchesParameter(AutoCompleteQuery query, String parameterName) {
return query.getName().equalsIgnoreCase(parameterName);
}
@Override
public <T, Z> Z getCommandOption(String name, CommandAutoCompleteInteractionEvent event, Class<T> parameterType, Class<Z> slashParameterType) {
return slashCommandParameterServiceBean.getCommandOption(name, event, parameterType, slashParameterType);
}
@Override
public <T, Z> boolean hasCommandOption(String name, CommandAutoCompleteInteractionEvent event, Class<T> parameterType, Class<Z> slashParameterType) {
return slashCommandParameterServiceBean.hasCommandOption(name, event, parameterType, slashParameterType);
}
@Override
public <T> T getCommandOption(String name, CommandAutoCompleteInteractionEvent event, Class<T> parameterType) {
return getCommandOption(name, event, parameterType, parameterType);
}
}

View File

@@ -6,7 +6,7 @@ import dev.sheldan.abstracto.core.models.database.AEmote;
import dev.sheldan.abstracto.core.service.EmoteService;
import net.dv8tion.jda.api.entities.User;
import net.dv8tion.jda.api.entities.emoji.Emoji;
import net.dv8tion.jda.api.events.interaction.command.SlashCommandInteractionEvent;
import net.dv8tion.jda.api.interactions.commands.CommandInteractionPayload;
import net.dv8tion.jda.api.interactions.commands.OptionType;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
@@ -25,7 +25,7 @@ public class SlashCommandParameterServiceBean implements SlashCommandParameterSe
private List<SlashCommandParameterProvider> parameterProviders;
@Override
public <T, Z> Z getCommandOption(String name, SlashCommandInteractionEvent event, Class<T> parameterType, Class<Z> slashParameterType) {
public <T, Z> Z getCommandOption(String name, CommandInteractionPayload event, Class<T> parameterType, Class<Z> slashParameterType) {
name = name.toLowerCase(Locale.ROOT);
List<OptionType> potentialOptionTypes = getTypesFromParameter(parameterType);
OptionType actualOptionType = potentialOptionTypes.size() == 1 ? potentialOptionTypes.get(0) : null;
@@ -71,7 +71,7 @@ public class SlashCommandParameterServiceBean implements SlashCommandParameterSe
}
@Override
public <T, Z> boolean hasCommandOption(String name, SlashCommandInteractionEvent event, Class<T> parameterType, Class<Z> slashParameterType) {
public <T, Z> boolean hasCommandOption(String name, CommandInteractionPayload event, Class<T> parameterType, Class<Z> slashParameterType) {
name = name.toLowerCase(Locale.ROOT);
List<OptionType> potentialOptionTypes = getTypesFromParameter(parameterType);
OptionType actualOptionType = potentialOptionTypes.size() == 1 ? potentialOptionTypes.get(0) : null;
@@ -117,33 +117,33 @@ public class SlashCommandParameterServiceBean implements SlashCommandParameterSe
}
@Override
public <T> T getCommandOption(String name, SlashCommandInteractionEvent event, Class<T> parameterType) {
public <T> T getCommandOption(String name, CommandInteractionPayload event, Class<T> parameterType) {
return getCommandOption(name, event, parameterType, parameterType);
}
@Override
public Object getCommandOption(String name, SlashCommandInteractionEvent event) {
public Object getCommandOption(String name, CommandInteractionPayload event) {
return event.getOption(name.toLowerCase(Locale.ROOT));
}
@Override
public Boolean hasCommandOption(String name, SlashCommandInteractionEvent event) {
public Boolean hasCommandOption(String name, CommandInteractionPayload event) {
return event.getOption(name.toLowerCase(Locale.ROOT)) != null;
}
@Override
public Boolean hasCommandOptionWithFullType(String name, SlashCommandInteractionEvent event, OptionType optionType) {
public Boolean hasCommandOptionWithFullType(String name, CommandInteractionPayload event, OptionType optionType) {
return hasCommandOption(getFullQualifiedParameterName(name, optionType), event);
}
@Override
public AEmote loadAEmoteFromString(String input, SlashCommandInteractionEvent event) {
public AEmote loadAEmoteFromString(String input, CommandInteractionPayload event) {
Emoji emoji = loadEmoteFromString(input, event);
return emoteService.getFakeEmoteFromEmoji(emoji);
}
@Override
public Emoji loadEmoteFromString(String input, SlashCommandInteractionEvent event) {
public Emoji loadEmoteFromString(String input, CommandInteractionPayload event) {
if(StringUtils.isNumeric(input)) {
long emoteId = Long.parseLong(input);
return event.getGuild().getEmojiById(emoteId);

View File

@@ -1,7 +1,11 @@
package dev.sheldan.abstracto.core.interaction.slash.parameter;
import net.dv8tion.jda.api.events.interaction.command.CommandAutoCompleteInteractionEvent;
import net.dv8tion.jda.api.interactions.AutoCompleteQuery;
public interface SlashCommandAutoCompleteService {
boolean matchesParameter(AutoCompleteQuery query, String parameterName);
<T, Z> Z getCommandOption(String name, CommandAutoCompleteInteractionEvent event, Class<T> parameterType, Class<Z> slashParameterType);
<T, Z> boolean hasCommandOption(String name, CommandAutoCompleteInteractionEvent event, Class<T> parameterType, Class<Z> slashParameterType);
<T> T getCommandOption(String name, CommandAutoCompleteInteractionEvent event, Class<T> parameterType);
}

View File

@@ -3,20 +3,20 @@ package dev.sheldan.abstracto.core.interaction.slash.parameter;
import dev.sheldan.abstracto.core.models.database.AEmote;
import net.dv8tion.jda.api.entities.emoji.Emoji;
import net.dv8tion.jda.api.events.interaction.command.SlashCommandInteractionEvent;
import net.dv8tion.jda.api.interactions.commands.CommandInteractionPayload;
import net.dv8tion.jda.api.interactions.commands.OptionType;
import java.util.List;
public interface SlashCommandParameterService {
<T, Z> Z getCommandOption(String name, SlashCommandInteractionEvent event, Class<T> parameterType, Class<Z> slashParameterType);
<T, Z> boolean hasCommandOption(String name, SlashCommandInteractionEvent event, Class<T> parameterType, Class<Z> slashParameterType);
<T> T getCommandOption(String name, SlashCommandInteractionEvent event, Class<T> parameterType);
Object getCommandOption(String name, SlashCommandInteractionEvent event);
Boolean hasCommandOption(String name, SlashCommandInteractionEvent event);
Boolean hasCommandOptionWithFullType(String name, SlashCommandInteractionEvent event, OptionType optionType);
AEmote loadAEmoteFromString(String input, SlashCommandInteractionEvent event);
Emoji loadEmoteFromString(String input, SlashCommandInteractionEvent event);
<T, Z> Z getCommandOption(String name, CommandInteractionPayload event, Class<T> parameterType, Class<Z> slashParameterType);
<T, Z> boolean hasCommandOption(String name, CommandInteractionPayload event, Class<T> parameterType, Class<Z> slashParameterType);
<T> T getCommandOption(String name, CommandInteractionPayload event, Class<T> parameterType);
Object getCommandOption(String name, CommandInteractionPayload event);
Boolean hasCommandOption(String name, CommandInteractionPayload event);
Boolean hasCommandOptionWithFullType(String name, CommandInteractionPayload event, OptionType optionType);
AEmote loadAEmoteFromString(String input, CommandInteractionPayload event);
Emoji loadEmoteFromString(String input, CommandInteractionPayload event);
List<OptionType> getTypesFromParameter(Class clazz);
String getFullQualifiedParameterName(String name, OptionType type);
}