use of com.freya02.botcommands.internal.application.slash.SlashCommandInfo in project BotCommands by freya022.
the class LocalizedCommandData method of.
public static LocalizedCommandData of(@NotNull BContext context, @Nullable Guild guild, @NotNull ApplicationCommandInfo info) {
final LocalizationData data = LocalizationData.getData(context, guild, info);
final CommandPath localizedPath;
if (data != null) {
localizedPath = data.getLocalizedPath();
} else {
localizedPath = info.getPath();
}
if (!(info instanceof SlashCommandInfo))
return new LocalizedCommandData(localizedPath, "No description", Collections.emptyList(), Collections.emptyList());
final String localizedDescription;
final List<LocalizedOption> localizedOptions;
final List<List<Command.Choice>> localizedChoices;
if (data != null && data.getLocalizedDescription() != null) {
localizedDescription = data.getLocalizedDescription();
} else {
localizedDescription = ((SlashCommandInfo) info).getDescription();
}
if (data != null && data.getLocalizedOptions() != null) {
localizedOptions = data.getLocalizedOptions();
} else {
localizedOptions = info.getOptionParameters().stream().filter(CommandParameter::isOption).map(ApplicationCommandParameter::getApplicationOptionData).map(a -> new LocalizedOption(a.getEffectiveName(), a.getEffectiveDescription())).collect(Collectors.toList());
}
if (data != null && data.getLocalizedChoices() != null) {
localizedChoices = data.getLocalizedChoices();
} else {
localizedChoices = SlashUtils.getNotLocalizedChoices(context, guild, info);
}
return new LocalizedCommandData(localizedPath, localizedDescription, localizedOptions, localizedChoices);
}
use of com.freya02.botcommands.internal.application.slash.SlashCommandInfo in project BotCommands by freya022.
the class BaseAutocompletionCache method getCompositeOptionValues.
private String[] getCompositeOptionValues(AutocompletionHandlerInfo info, SlashCommandInfo slashCommand, CommandAutoCompleteInteractionEvent event) {
final List<String> optionValues = new ArrayList<>();
optionValues.add(event.getFocusedOption().getValue());
int optionIndex = 0;
final List<String> optionNames = event.getGuild() != null ? slashCommand.getLocalizedOptions(event.getGuild()) : null;
for (final AutocompleteCommandParameter parameter : info.getParameters()) {
final ApplicationOptionData applicationOptionData = parameter.getApplicationOptionData();
if (parameter.isOption()) {
final String optionName = optionNames == null ? applicationOptionData.getEffectiveName() : optionNames.get(optionIndex);
if (optionName == null) {
throw new IllegalArgumentException(String.format("Option name #%d (%s) could not be resolved for %s", optionIndex, applicationOptionData.getEffectiveName(), Utils.formatMethodShort(info.getMethod())));
}
optionIndex++;
if (parameter.isCompositeKey()) {
final OptionMapping option = event.getOption(optionName);
if (option == null) {
optionValues.add("null");
} else if (!event.getFocusedOption().getName().equals(optionName)) {
// Only add the options other than the focused one, since it's already there, saves us from an HashSet
optionValues.add(option.getAsString());
}
}
}
}
return optionValues.toArray(new String[0]);
}
use of com.freya02.botcommands.internal.application.slash.SlashCommandInfo in project BotCommands by freya022.
the class ConstantByKeyAutocompletionCache method retrieveAndCall.
@Override
public void retrieveAndCall(SlashCommandInfo slashCommand, CommandAutoCompleteInteractionEvent event, Consumer<List<Command.Choice>> choiceCallback, ConsumerEx<CompositeAutocompletionKey> valueComputer) throws Exception {
final CompositeAutocompletionKey compositeKey = getCompositeKey(handlerInfo, slashCommand, event);
final List<Command.Choice> cachedValue = cache.getIfPresent(compositeKey);
if (cachedValue != null) {
choiceCallback.accept(cachedValue);
} else {
// Choice callback is called by valueComputer
valueComputer.accept(compositeKey);
}
}
Aggregations