Search in sources :

Example 1 with BContext

use of com.freya02.botcommands.api.BContext in project BotCommands by freya022.

the class LocalizationData method getData.

public static LocalizationData getData(BContext context, @Nullable Guild guild, @NotNull ApplicationCommandInfo info) {
    final CommandPath localizedPath;
    final String localizedDescription;
    final List<LocalizedOption> localizedOptions;
    final List<List<Command.Choice>> localizedChoices;
    final Locale locale = context.getEffectiveLocale(guild);
    final BResourceBundle bundle = BResourceBundle.getBundle("BotCommands", locale);
    if (bundle == null) {
        return null;
    }
    final String prefix;
    if (info instanceof SlashCommandInfo) {
        prefix = "slash";
    } else if (info instanceof UserCommandInfo) {
        prefix = "user";
    } else if (info instanceof MessageCommandInfo) {
        prefix = "message";
    } else {
        throw new IllegalArgumentException("Unknown localization prefix for class: " + info.getClass().getSimpleName());
    }
    final String qualifier = info.getMethod().getName();
    final StringJoiner pathJoiner = new StringJoiner("/");
    pathJoiner.add(tryLocalize(bundle, info.getPath().getName(), prefix, qualifier, "name"));
    if (info instanceof SlashCommandInfo) {
        final String notLocalizedGroup = info.getPath().getGroup();
        final String notLocalizedSubname = info.getPath().getSubname();
        if (notLocalizedGroup != null)
            pathJoiner.add(tryLocalize(bundle, notLocalizedGroup, prefix, qualifier, "group"));
        if (notLocalizedSubname != null)
            pathJoiner.add(tryLocalize(bundle, notLocalizedSubname, prefix, qualifier, "subname"));
    }
    localizedPath = CommandPath.of(pathJoiner.toString());
    if (info instanceof SlashCommandInfo) {
        localizedDescription = tryLocalize(bundle, ((SlashCommandInfo) info).getDescription(), prefix, qualifier, "description");
    } else
        localizedDescription = null;
    if (info instanceof SlashCommandInfo) {
        localizedOptions = new ArrayList<>();
        localizedChoices = new ArrayList<>();
        final List<List<Command.Choice>> notLocalizedChoices = SlashUtils.getNotLocalizedChoices(context, guild, info);
        final List<? extends ApplicationCommandParameter<?>> parameters = info.getOptionParameters();
        for (int optionIndex = 0, parametersSize = parameters.size(); optionIndex < parametersSize; optionIndex++) {
            ApplicationCommandParameter<?> parameter = parameters.get(optionIndex);
            final ApplicationOptionData optionData = parameter.getApplicationOptionData();
            final String optionName = tryLocalize(bundle, optionData.getEffectiveName(), prefix, qualifier, "options", optionIndex, "name");
            final String optionDescription = tryLocalize(bundle, optionData.getEffectiveDescription(), prefix, qualifier, "options", optionIndex, "description");
            localizedOptions.add(new LocalizedOption(optionName, optionDescription));
            final List<Command.Choice> choices = getLocalizedChoices(bundle, prefix, qualifier, notLocalizedChoices, optionIndex, parameter);
            localizedChoices.add(choices);
        }
    } else {
        localizedOptions = null;
        localizedChoices = null;
    }
    return new LocalizationData(localizedPath, localizedDescription, localizedOptions, localizedChoices);
}
Also used : Locale(java.util.Locale) SlashCommandInfo(com.freya02.botcommands.internal.application.slash.SlashCommandInfo) LocalizedOption(com.freya02.botcommands.internal.application.LocalizedCommandData.LocalizedOption) ApplicationOptionData(com.freya02.botcommands.internal.ApplicationOptionData) BResourceBundle(com.freya02.botcommands.internal.utils.BResourceBundle) MessageCommandInfo(com.freya02.botcommands.internal.application.context.message.MessageCommandInfo) Command(net.dv8tion.jda.api.interactions.commands.Command) ArrayList(java.util.ArrayList) List(java.util.List) UserCommandInfo(com.freya02.botcommands.internal.application.context.user.UserCommandInfo) CommandPath(com.freya02.botcommands.api.application.CommandPath) StringJoiner(java.util.StringJoiner)

Example 2 with BContext

use of com.freya02.botcommands.api.BContext 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 BContext

use of com.freya02.botcommands.api.BContext 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 4 with BContext

use of com.freya02.botcommands.api.BContext in project BotCommands by freya022.

the class MessageCommandInfo method execute.

public boolean execute(BContext context, MessageContextInteractionEvent event, Consumer<Throwable> throwableConsumer) throws Exception {
    final Object[] objects = new Object[commandParameters.size() + 1];
    if (guildOnly) {
        objects[0] = new GuildMessageEvent(context, event);
    } else {
        objects[0] = new GlobalMessageEvent(context, event);
    }
    for (int i = 0, commandParametersLength = commandParameters.size(); i < commandParametersLength; i++) {
        ContextCommandParameter<MessageContextParameterResolver> parameter = commandParameters.get(i);
        if (parameter.isOption()) {
            objects[i + 1] = parameter.getResolver().resolve(context, this, event);
        // no need to check for unresolved parameters,
        // it is impossible to have other arg types other than Message (and custom resolvers)
        } else {
            objects[i + 1] = parameter.getCustomResolver().resolve(context, this, event);
        }
    }
    applyCooldown(event);
    getMethodRunner().invoke(objects, throwableConsumer);
    return true;
}
Also used : MessageContextParameterResolver(com.freya02.botcommands.api.parameters.MessageContextParameterResolver) GlobalMessageEvent(com.freya02.botcommands.api.application.context.message.GlobalMessageEvent) GuildMessageEvent(com.freya02.botcommands.api.application.context.message.GuildMessageEvent)

Example 5 with BContext

use of com.freya02.botcommands.api.BContext in project BotCommands by freya022.

the class UserCommandInfo method execute.

public boolean execute(BContext context, UserContextInteractionEvent event, Consumer<Throwable> throwableConsumer) throws Exception {
    final Object[] objects = new Object[commandParameters.size() + 1];
    if (guildOnly) {
        objects[0] = new GuildUserEvent(context, event);
    } else {
        objects[0] = new GlobalUserEvent(context, event);
    }
    for (int i = 0, commandParametersLength = commandParameters.size(); i < commandParametersLength; i++) {
        ContextCommandParameter<UserContextParameterResolver> parameter = commandParameters.get(i);
        if (parameter.isOption()) {
            objects[i + 1] = parameter.getResolver().resolve(context, this, event);
        // no need to check for unresolved parameters,
        // it is impossible to have other arg types other than User (and custom resolvers)
        } else {
            objects[i + 1] = parameter.getCustomResolver().resolve(context, this, event);
        }
    }
    applyCooldown(event);
    getMethodRunner().invoke(objects, throwableConsumer);
    return true;
}
Also used : GuildUserEvent(com.freya02.botcommands.api.application.context.user.GuildUserEvent) UserContextParameterResolver(com.freya02.botcommands.api.parameters.UserContextParameterResolver) GlobalUserEvent(com.freya02.botcommands.api.application.context.user.GlobalUserEvent)

Aggregations

CommandPath (com.freya02.botcommands.api.application.CommandPath)2 GuildSlashEvent (com.freya02.botcommands.api.application.slash.GuildSlashEvent)2 ApplicationOptionData (com.freya02.botcommands.internal.ApplicationOptionData)2 SlashCommandInfo (com.freya02.botcommands.internal.application.slash.SlashCommandInfo)2 ArrayList (java.util.ArrayList)2 List (java.util.List)2 Command (net.dv8tion.jda.api.interactions.commands.Command)2 BContext (com.freya02.botcommands.api.BContext)1 ApplicationCommand (com.freya02.botcommands.api.application.ApplicationCommand)1 AppOption (com.freya02.botcommands.api.application.annotations.AppOption)1 Test (com.freya02.botcommands.api.application.annotations.Test)1 GlobalMessageEvent (com.freya02.botcommands.api.application.context.message.GlobalMessageEvent)1 GuildMessageEvent (com.freya02.botcommands.api.application.context.message.GuildMessageEvent)1 GlobalUserEvent (com.freya02.botcommands.api.application.context.user.GlobalUserEvent)1 GuildUserEvent (com.freya02.botcommands.api.application.context.user.GuildUserEvent)1 JDASlashCommand (com.freya02.botcommands.api.application.slash.annotations.JDASlashCommand)1 ComponentManager (com.freya02.botcommands.api.components.ComponentManager)1 Components (com.freya02.botcommands.api.components.Components)1 InteractionConstraints (com.freya02.botcommands.api.components.InteractionConstraints)1 Paginator (com.freya02.botcommands.api.pagination.paginator.Paginator)1