Search in sources :

Example 1 with SlashCommandParameter

use of com.freya02.botcommands.internal.application.slash.SlashCommandParameter in project BotCommands by freya022.

the class SlashCommandInfo method getAutocompletionHandlerName.

@Nullable
public String getAutocompletionHandlerName(CommandAutoCompleteInteractionEvent event) {
    final AutoCompleteQuery autoCompleteQuery = event.getFocusedOption();
    int optionIndex = 0;
    final List<String> optionNames = event.getGuild() != null ? getLocalizedOptions(event.getGuild()) : null;
    for (final SlashCommandParameter parameter : commandParameters) {
        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(getMethod())));
            }
            optionIndex++;
            if (optionName.equals(autoCompleteQuery.getName())) {
                return applicationOptionData.getAutocompletionHandlerName();
            }
        }
    }
    return null;
}
Also used : AutoCompleteQuery(net.dv8tion.jda.api.interactions.AutoCompleteQuery) ApplicationOptionData(com.freya02.botcommands.internal.ApplicationOptionData) Nullable(org.jetbrains.annotations.Nullable)

Example 2 with SlashCommandParameter

use of com.freya02.botcommands.internal.application.slash.SlashCommandParameter in project BotCommands by freya022.

the class SlashCommandInfo method execute.

public boolean execute(BContext context, SlashCommandInteractionEvent event, Consumer<Throwable> throwableConsumer) throws Exception {
    List<Object> objects = new ArrayList<>(commandParameters.size() + 1) {

        {
            if (guildOnly) {
                add(new GuildSlashEvent(context, event));
            } else {
                add(new GlobalSlashEventImpl(context, event));
            }
        }
    };
    int optionIndex = 0;
    final List<String> optionNames = event.getGuild() != null ? getLocalizedOptions(event.getGuild()) : null;
    for (final SlashCommandParameter parameter : commandParameters) {
        final ApplicationOptionData applicationOptionData = parameter.getApplicationOptionData();
        final Object obj;
        if (parameter.isOption()) {
            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(getMethod())));
            }
            optionIndex++;
            final OptionMapping optionMapping = event.getOption(optionName);
            if (optionMapping == null) {
                if (parameter.isOptional()) {
                    if (parameter.isPrimitive()) {
                        objects.add(0);
                    } else {
                        objects.add(null);
                    }
                    continue;
                } else {
                    throw new RuntimeException("Slash parameter couldn't be resolved for method " + Utils.formatMethodShort(commandMethod) + " at parameter " + applicationOptionData.getEffectiveName() + " (localized '" + optionName + "')");
                }
            }
            obj = parameter.getResolver().resolve(context, this, event, optionMapping);
            if (obj == null) {
                event.replyFormat(context.getDefaultMessages(event.getGuild()).getSlashCommandUnresolvableParameterMsg(), applicationOptionData.getEffectiveName(), parameter.getBoxedType().getSimpleName()).setEphemeral(true).queue();
                // Not a warning, could be normal if the user did not supply a valid string for user-defined resolvers
                LOGGER.trace("The parameter '{}' of value '{}' could not be resolved into a {}", applicationOptionData.getEffectiveName(), optionMapping.getAsString(), parameter.getBoxedType().getSimpleName());
                return false;
            }
            if (!parameter.getBoxedType().isAssignableFrom(obj.getClass())) {
                event.replyFormat(context.getDefaultMessages(event.getGuild()).getSlashCommandInvalidParameterTypeMsg(), applicationOptionData.getEffectiveName(), parameter.getBoxedType().getSimpleName(), obj.getClass().getSimpleName()).setEphemeral(true).queue();
                LOGGER.error("The parameter '{}' of value '{}' is not a valid type (expected a {})", applicationOptionData.getEffectiveName(), optionMapping.getAsString(), parameter.getBoxedType().getSimpleName());
                return false;
            }
        } else {
            obj = parameter.getCustomResolver().resolve(context, this, event);
        }
        // For some reason using an array list instead of a regular array
        // magically unboxes primitives when passed to Method#invoke
        objects.add(obj);
    }
    applyCooldown(event);
    getMethodRunner().invoke(objects.toArray(), throwableConsumer);
    return true;
}
Also used : OptionMapping(net.dv8tion.jda.api.interactions.commands.OptionMapping) ArrayList(java.util.ArrayList) ApplicationOptionData(com.freya02.botcommands.internal.ApplicationOptionData) GuildSlashEvent(com.freya02.botcommands.api.application.slash.GuildSlashEvent)

Example 3 with SlashCommandParameter

use of com.freya02.botcommands.internal.application.slash.SlashCommandParameter in project BotCommands by freya022.

the class AutocompletionHandlersBuilder method postProcess.

public void postProcess() {
    for (SlashCommandInfo info : context.getApplicationCommandInfoMap().getSlashCommands().values()) {
        MethodParameters<SlashCommandParameter> parameters = info.getParameters();
        for (int i = 0, parametersSize = parameters.size(); i < parametersSize; i++) {
            SlashCommandParameter parameter = parameters.get(i);
            if (!parameter.isOption())
                continue;
            final ApplicationOptionData applicationOptionData = parameter.getApplicationOptionData();
            final String autocompleteHandlerName = applicationOptionData.getAutocompletionHandlerName();
            if (autocompleteHandlerName != null) {
                final AutocompletionHandlerInfo handler = context.getAutocompletionHandler(autocompleteHandlerName);
                if (handler == null) {
                    throw new IllegalArgumentException("Slash command parameter #" + i + " at " + Utils.formatMethodShort(info.getMethod()) + " uses autocompletion but has no handler assigned, did you misspell the handler name ? Consider using a constant variable to share with the handler and the option");
                }
                handler.checkParameters(info);
            }
        }
    }
    context.addEventListeners(new AutocompletionListener(context));
}
Also used : SlashCommandInfo(com.freya02.botcommands.internal.application.slash.SlashCommandInfo) ApplicationOptionData(com.freya02.botcommands.internal.ApplicationOptionData) SlashCommandParameter(com.freya02.botcommands.internal.application.slash.SlashCommandParameter)

Aggregations

ApplicationOptionData (com.freya02.botcommands.internal.ApplicationOptionData)3 GuildSlashEvent (com.freya02.botcommands.api.application.slash.GuildSlashEvent)1 SlashCommandInfo (com.freya02.botcommands.internal.application.slash.SlashCommandInfo)1 SlashCommandParameter (com.freya02.botcommands.internal.application.slash.SlashCommandParameter)1 ArrayList (java.util.ArrayList)1 AutoCompleteQuery (net.dv8tion.jda.api.interactions.AutoCompleteQuery)1 OptionMapping (net.dv8tion.jda.api.interactions.commands.OptionMapping)1 Nullable (org.jetbrains.annotations.Nullable)1