Search in sources :

Example 6 with SubcommandGroupData

use of net.dv8tion.jda.api.interactions.commands.build.SubcommandGroupData in project Discord-Core-Bot-Apple by demodude4u.

the class DiscordBot method buildUpdateCommands.

// Hold my beer
@SuppressWarnings("unchecked")
private void buildUpdateCommands(CommandListUpdateAction updateCommands) {
    Map<String, Object> root = new LinkedHashMap<>();
    for (SlashCommandDefinition command : commandSlash.values()) {
        String[] pathSplit = command.getPath().split("/");
        Map<String, Object> group = root;
        for (int i = 0; i < pathSplit.length; i++) {
            String name = pathSplit[i];
            if (i == pathSplit.length - 1) {
                group.put(name, command);
            } else {
                Object subGroup = group.get(name);
                if (subGroup == null) {
                    group.put(name, subGroup = new LinkedHashMap<String, Object>());
                }
                group = (Map<String, Object>) subGroup;
            }
        }
    }
    for (Entry<String, Object> rootEntry : root.entrySet()) {
        SlashCommandData commandData;
        if (rootEntry.getValue() instanceof SlashCommandDefinition) {
            SlashCommandDefinition commandDefinition = (SlashCommandDefinition) rootEntry.getValue();
            commandData = Commands.slash(rootEntry.getKey(), commandDefinition.getDescription());
            for (SlashCommandOptionDefinition option : commandDefinition.getOptions()) {
                commandData = commandData.addOption(option.getType(), option.getName(), option.getDescription(), option.isRequired(), option.isAutoComplete());
            }
        } else {
            Map<String, Object> sub = (Map<String, Object>) rootEntry.getValue();
            commandData = Commands.slash(rootEntry.getKey(), sub.keySet().stream().collect(Collectors.joining(", ")));
            for (Entry<String, Object> subEntry : sub.entrySet()) {
                if (subEntry.getValue() instanceof SlashCommandDefinition) {
                    SlashCommandDefinition commandDefinition = (SlashCommandDefinition) subEntry.getValue();
                    SubcommandData subcommandData = new SubcommandData(subEntry.getKey(), commandDefinition.getDescription());
                    for (SlashCommandOptionDefinition option : commandDefinition.getOptions()) {
                        subcommandData = subcommandData.addOption(option.getType(), option.getName(), option.getDescription(), option.isRequired(), option.isAutoComplete());
                    }
                    commandData = commandData.addSubcommands(subcommandData);
                } else {
                    Map<String, SlashCommandDefinition> subSub = (Map<String, SlashCommandDefinition>) subEntry.getValue();
                    SubcommandGroupData subcommandGroupData = new SubcommandGroupData(subEntry.getKey(), subSub.keySet().stream().collect(Collectors.joining(", ")));
                    for (Entry<String, SlashCommandDefinition> subSubEntry : subSub.entrySet()) {
                        SubcommandData subcommandData = new SubcommandData(subSubEntry.getKey(), subSubEntry.getValue().getDescription());
                        for (SlashCommandOptionDefinition option : subSubEntry.getValue().getOptions()) {
                            subcommandData = subcommandData.addOption(option.getType(), option.getName(), option.getDescription(), option.isRequired(), option.isAutoComplete());
                        }
                        subcommandGroupData = subcommandGroupData.addSubcommands(subcommandData);
                    }
                    commandData = commandData.addSubcommandGroups(subcommandGroupData);
                }
            }
        }
        updateCommands = updateCommands.addCommands(commandData);
    }
    for (MessageCommandDefinition commandDefinition : commandMessage.values()) {
        CommandData commandData = Commands.message(commandDefinition.getName());
        updateCommands = updateCommands.addCommands(commandData);
    }
}
Also used : SlashCommandData(net.dv8tion.jda.api.interactions.commands.build.SlashCommandData) LinkedHashMap(java.util.LinkedHashMap) JSONObject(org.json.JSONObject) SlashCommandData(net.dv8tion.jda.api.interactions.commands.build.SlashCommandData) CommandData(net.dv8tion.jda.api.interactions.commands.build.CommandData) Map(java.util.Map) LinkedHashMap(java.util.LinkedHashMap) SubcommandData(net.dv8tion.jda.api.interactions.commands.build.SubcommandData) SubcommandGroupData(net.dv8tion.jda.api.interactions.commands.build.SubcommandGroupData)

Aggregations

SubcommandGroupData (net.dv8tion.jda.api.interactions.commands.build.SubcommandGroupData)6 SubcommandData (net.dv8tion.jda.api.interactions.commands.build.SubcommandData)3 Nonnull (javax.annotation.Nonnull)2 CommandDataImpl (net.dv8tion.jda.internal.interactions.CommandDataImpl)2 Test (org.junit.jupiter.api.Test)2 LinkedHashMap (java.util.LinkedHashMap)1 Map (java.util.Map)1 CommandData (net.dv8tion.jda.api.interactions.commands.build.CommandData)1 SlashCommandData (net.dv8tion.jda.api.interactions.commands.build.SlashCommandData)1 DataArray (net.dv8tion.jda.api.utils.data.DataArray)1 DataObject (net.dv8tion.jda.api.utils.data.DataObject)1 JSONObject (org.json.JSONObject)1