Search in sources :

Example 6 with SlashCommandInfo

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);
}
Also used : SlashCommandInfo(com.freya02.botcommands.internal.application.slash.SlashCommandInfo) Command(net.dv8tion.jda.api.interactions.commands.Command) List(java.util.List) CommandPath(com.freya02.botcommands.api.application.CommandPath)

Example 7 with SlashCommandInfo

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]);
}
Also used : OptionMapping(net.dv8tion.jda.api.interactions.commands.OptionMapping) ArrayList(java.util.ArrayList) AutocompleteCommandParameter(com.freya02.botcommands.internal.application.slash.autocomplete.AutocompleteCommandParameter) ApplicationOptionData(com.freya02.botcommands.internal.ApplicationOptionData)

Example 8 with SlashCommandInfo

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);
    }
}
Also used : CompositeAutocompletionKey(com.freya02.botcommands.internal.application.slash.autocomplete.CompositeAutocompletionKey)

Aggregations

SlashCommandInfo (com.freya02.botcommands.internal.application.slash.SlashCommandInfo)6 CommandPath (com.freya02.botcommands.api.application.CommandPath)4 ApplicationOptionData (com.freya02.botcommands.internal.ApplicationOptionData)3 Command (net.dv8tion.jda.api.interactions.commands.Command)3 MessageCommandInfo (com.freya02.botcommands.internal.application.context.message.MessageCommandInfo)2 UserCommandInfo (com.freya02.botcommands.internal.application.context.user.UserCommandInfo)2 ArrayList (java.util.ArrayList)2 List (java.util.List)2 Logging (com.freya02.botcommands.api.Logging)1 SettingsProvider (com.freya02.botcommands.api.SettingsProvider)1 GlobalSlashEvent (com.freya02.botcommands.api.application.slash.GlobalSlashEvent)1 GuildSlashEvent (com.freya02.botcommands.api.application.slash.GuildSlashEvent)1 JDASlashCommand (com.freya02.botcommands.api.application.slash.annotations.JDASlashCommand)1 DebugBuilder (com.freya02.botcommands.api.builder.DebugBuilder)1 BContextImpl (com.freya02.botcommands.internal.BContextImpl)1 LocalizedOption (com.freya02.botcommands.internal.application.LocalizedCommandData.LocalizedOption)1 SlashCommandParameter (com.freya02.botcommands.internal.application.slash.SlashCommandParameter)1 SlashUtils.appendCommands (com.freya02.botcommands.internal.application.slash.SlashUtils.appendCommands)1 SlashUtils.getLocalizedMethodOptions (com.freya02.botcommands.internal.application.slash.SlashUtils.getLocalizedMethodOptions)1 AutocompleteCommandParameter (com.freya02.botcommands.internal.application.slash.autocomplete.AutocompleteCommandParameter)1