Search in sources :

Example 26 with OptionMapping

use of net.dv8tion.jda.api.interactions.commands.OptionMapping 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 27 with OptionMapping

use of net.dv8tion.jda.api.interactions.commands.OptionMapping in project TamaBot by Loli-Cafe.

the class SafebooruCommand method compose.

@Override
public void compose(SlashCommandInteractionEvent event) {
    OptionMapping tags = event.getOption("tags");
    OptionMapping search = event.getOption("search");
    if (search != null) {
        event.replyEmbeds(execute(search.getAsString(), event.getUser())).queue();
    } else {
        event.replyEmbeds(execute(TagsParser.parse(null, tags != null ? tags.getAsString().split(" ") : new String[] { "loli" }), event.getUser())).queue();
    }
}
Also used : OptionMapping(net.dv8tion.jda.api.interactions.commands.OptionMapping)

Example 28 with OptionMapping

use of net.dv8tion.jda.api.interactions.commands.OptionMapping in project TamaBot by Loli-Cafe.

the class YandereCommand method compose.

@Override
public void compose(SlashCommandInteractionEvent event) {
    OptionMapping tags = event.getOption("tags");
    OptionMapping rating = event.getOption("rating");
    OptionMapping search = event.getOption("search");
    if (search != null) {
        event.replyEmbeds(execute(search.getAsString(), event.getUser())).queue();
    } else {
        event.replyEmbeds(execute(TagsParser.parse(rating != null ? rating.getAsString() : "e", tags != null ? tags.getAsString().split(" ") : new String[] { "loli" }), event.getUser())).queue();
    }
}
Also used : OptionMapping(net.dv8tion.jda.api.interactions.commands.OptionMapping)

Example 29 with OptionMapping

use of net.dv8tion.jda.api.interactions.commands.OptionMapping in project TamaBot by Loli-Cafe.

the class BeatCommand method compose.

@Override
public void compose(SlashCommandInteractionEvent event) {
    checkContext(event.getMember(), event.getTextChannel());
    OptionMapping optionMapping = event.getOption("user");
    event.replyEmbeds(execute(optionMapping != null ? String.format("%s beaten %s", event.getUser(), optionMapping.getAsUser().getAsMention()) : (event.getUser().getAsMention() + " would like to slapped"))).queue();
}
Also used : OptionMapping(net.dv8tion.jda.api.interactions.commands.OptionMapping)

Example 30 with OptionMapping

use of net.dv8tion.jda.api.interactions.commands.OptionMapping in project TamaBot by Loli-Cafe.

the class BestialityCommand method compose.

@Override
public void compose(SlashCommandInteractionEvent event) {
    checkContext(event.getMember(), event.getTextChannel());
    OptionMapping optionMapping = event.getOption("user");
    event.replyEmbeds(execute(optionMapping != null ? String.format("%s forced %s to have sex with animal", event.getUser(), optionMapping.getAsUser().getAsMention()) : (event.getUser().getAsMention() + " have sex with animal"))).queue();
}
Also used : OptionMapping(net.dv8tion.jda.api.interactions.commands.OptionMapping)

Aggregations

OptionMapping (net.dv8tion.jda.api.interactions.commands.OptionMapping)133 CommandException (uwu.narumi.tama.command.CommandException)44 EmbedBuilder (net.dv8tion.jda.api.EmbedBuilder)12 Member (net.dv8tion.jda.api.entities.Member)8 TextChannel (net.dv8tion.jda.api.entities.TextChannel)8 Guild (net.dv8tion.jda.api.entities.Guild)5 Meeting (de.lightbolt.meeting.systems.meeting.model.Meeting)4 User (net.dv8tion.jda.api.entities.User)4 SlashCommandInteractionEvent (net.dv8tion.jda.api.events.interaction.command.SlashCommandInteractionEvent)4 Message (net.dv8tion.jda.api.entities.Message)3 JamSubmissionRepository (net.javadiscord.javabot.systems.jam.dao.JamSubmissionRepository)3 Logger (org.slf4j.Logger)3 LoggerFactory (org.slf4j.LoggerFactory)3 Bean (at.xirado.bean.Bean)2 SlashCommand (at.xirado.bean.command.SlashCommand)2 SlashCommandContext (at.xirado.bean.command.SlashCommandContext)2 EmbedUtil (at.xirado.bean.misc.EmbedUtil)2 ApplicationOptionData (com.freya02.botcommands.internal.ApplicationOptionData)2 EventWaiter (com.jagrosh.jdautilities.commons.waiter.EventWaiter)2 AudioPlayer (com.sedmelluq.discord.lavaplayer.player.AudioPlayer)2