Search in sources :

Example 1 with MessageCommandInfo

use of com.freya02.botcommands.internal.application.context.message.MessageCommandInfo 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 MessageCommandInfo

use of com.freya02.botcommands.internal.application.context.message.MessageCommandInfo in project BotCommands by freya022.

the class ApplicationCommandsBuilder method processMessageCommand.

private void processMessageCommand(ApplicationCommand applicationCommand, Method method) {
    if (method.getAnnotation(JDAMessageCommand.class).guildOnly()) {
        if (!ReflectionUtils.hasFirstParameter(method, GlobalMessageEvent.class) && !ReflectionUtils.hasFirstParameter(method, GuildMessageEvent.class))
            throw new IllegalArgumentException("Message command at " + Utils.formatMethodShort(method) + " must have a GuildMessageEvent or GlobalMessageEvent as first parameter");
        if (!ReflectionUtils.hasFirstParameter(method, GuildMessageEvent.class)) {
            // If type is correct but guild specialization isn't used
            LOGGER.warn("Guild-only message command {} uses GlobalMessageEvent, consider using GuildMessageEvent to remove warnings related to guild stuff's nullability", Utils.formatMethodShort(method));
        }
    } else {
        if (!ReflectionUtils.hasFirstParameter(method, GlobalMessageEvent.class))
            throw new IllegalArgumentException("Message command at " + Utils.formatMethodShort(method) + " must have a GlobalMessageEvent as first parameter");
    }
    final MessageCommandInfo info = new MessageCommandInfo(context, applicationCommand, method);
    LOGGER.debug("Adding message command {} for method {}", info.getPath().getName(), Utils.formatMethodShort(method));
    context.addMessageCommand(info);
}
Also used : MessageCommandInfo(com.freya02.botcommands.internal.application.context.message.MessageCommandInfo) GlobalMessageEvent(com.freya02.botcommands.api.application.context.message.GlobalMessageEvent) JDAMessageCommand(com.freya02.botcommands.api.application.context.annotations.JDAMessageCommand) GuildMessageEvent(com.freya02.botcommands.api.application.context.message.GuildMessageEvent)

Example 3 with MessageCommandInfo

use of com.freya02.botcommands.internal.application.context.message.MessageCommandInfo in project BotCommands by freya022.

the class BContextImpl method addMessageCommand.

public void addMessageCommand(MessageCommandInfo commandInfo) {
    final CommandPath path = commandInfo.getPath();
    MessageCommandInfo oldCmd = getMessageCommandsMap().put(path, commandInfo);
    if (oldCmd != null) {
        throw new IllegalStateException(String.format("Two message commands have the same names: '%s' from %s and %s", path, Utils.formatMethodShort(oldCmd.getMethod()), Utils.formatMethodShort(commandInfo.getMethod())));
    }
}
Also used : MessageCommandInfo(com.freya02.botcommands.internal.application.context.message.MessageCommandInfo) CommandPath(com.freya02.botcommands.api.application.CommandPath)

Aggregations

MessageCommandInfo (com.freya02.botcommands.internal.application.context.message.MessageCommandInfo)3 CommandPath (com.freya02.botcommands.api.application.CommandPath)2 JDAMessageCommand (com.freya02.botcommands.api.application.context.annotations.JDAMessageCommand)1 GlobalMessageEvent (com.freya02.botcommands.api.application.context.message.GlobalMessageEvent)1 GuildMessageEvent (com.freya02.botcommands.api.application.context.message.GuildMessageEvent)1 ApplicationOptionData (com.freya02.botcommands.internal.ApplicationOptionData)1 LocalizedOption (com.freya02.botcommands.internal.application.LocalizedCommandData.LocalizedOption)1 UserCommandInfo (com.freya02.botcommands.internal.application.context.user.UserCommandInfo)1 SlashCommandInfo (com.freya02.botcommands.internal.application.slash.SlashCommandInfo)1 BResourceBundle (com.freya02.botcommands.internal.utils.BResourceBundle)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1 Locale (java.util.Locale)1 StringJoiner (java.util.StringJoiner)1 Command (net.dv8tion.jda.api.interactions.commands.Command)1