Search in sources :

Example 11 with SubcommandData

use of net.dv8tion.jda.api.interactions.commands.build.SubcommandData 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)

Example 12 with SubcommandData

use of net.dv8tion.jda.api.interactions.commands.build.SubcommandData in project TechDiscordBot by TechsCode-Team.

the class ModulesManager method load.

public void load() {
    TechDiscordBot.getJDA().updateCommands().queue();
    CommandListUpdateAction commands = TechDiscordBot.getGuild().updateCommands();
    for (Class<?> each : ProjectUtil.getClasses("me.TechsCode.TechDiscordBot.module")) {
        if (CommandModule.class.isAssignableFrom(each) && !Modifier.isAbstract(each.getModifiers())) {
            try {
                CommandModule module = (CommandModule) each.getConstructor(TechDiscordBot.class).newInstance(TechDiscordBot.getBot());
                if (module.getName() == null)
                    continue;
                cmdModules.add(module);
                CommandData cmdData = new CommandData(module.getName(), module.getDescription() == null ? "No description set." : module.getDescription()).addOptions(module.getOptions()).setDefaultEnabled(module.getCommandPrivileges().length == 0);
                commands.addCommands(cmdData);
            } catch (InstantiationException | IllegalAccessException | InvocationTargetException | NoSuchMethodException e) {
                e.printStackTrace();
            }
        } else if (Module.class.isAssignableFrom(each) && !Modifier.isAbstract(each.getModifiers())) {
            try {
                Module module = (Module) each.getConstructor(TechDiscordBot.class).newInstance(TechDiscordBot.getBot());
                module.enable();
                modules.add(module);
            } catch (InstantiationException | IllegalAccessException | InvocationTargetException | NoSuchMethodException e) {
                e.printStackTrace();
            }
        }
    }
    commands.addCommands(new CommandData("ticket", "Manage tickets.").addSubcommands(new SubcommandData("add", "Add a member to a ticket.").addOptions(new OptionData(OptionType.USER, "member", "Member to add.", true)), new SubcommandData("remove", "Remove a member from a ticket.").addOptions(new OptionData(OptionType.USER, "member", "Member to remove.", true)), new SubcommandData("transcript", "Force make a ticket transcript."), new SubcommandData("close", "Close a ticket.").addOptions(new OptionData(OptionType.STRING, "reason", "Reason to close the ticket. (Optional)")))).queue(cmds -> {
        cmds.forEach(command -> {
            CommandPrivilege[] privilege = cmdModules.stream().filter(c -> c.getName().equals(command.getName())).map(CommandModule::getCommandPrivileges).findFirst().orElse(new CommandPrivilege[] {});
            if (privilege.length > 0)
                TechDiscordBot.getGuild().updateCommandPrivilegesById(command.getId(), Arrays.asList(privilege)).queue();
        });
    });
    TechDiscordBot.getJDA().addEventListener(modules.toArray());
    TechDiscordBot.getJDA().addEventListener(cmdModules.toArray());
    Runtime.getRuntime().addShutdownHook(new Thread(() -> modules.forEach(Module::onDisable)));
}
Also used : OptionType(net.dv8tion.jda.api.interactions.commands.OptionType) Arrays(java.util.Arrays) SubcommandData(net.dv8tion.jda.api.interactions.commands.build.SubcommandData) TechDiscordBot(me.TechsCode.TechDiscordBot.TechDiscordBot) OptionData(net.dv8tion.jda.api.interactions.commands.build.OptionData) CommandData(net.dv8tion.jda.api.interactions.commands.build.CommandData) InvocationTargetException(java.lang.reflect.InvocationTargetException) ArrayList(java.util.ArrayList) SlashCommandEvent(net.dv8tion.jda.api.events.interaction.SlashCommandEvent) SubscribeEvent(net.dv8tion.jda.api.hooks.SubscribeEvent) List(java.util.List) Modifier(java.lang.reflect.Modifier) ProjectUtil(me.TechsCode.TechDiscordBot.util.ProjectUtil) CommandPrivilege(net.dv8tion.jda.api.interactions.commands.privileges.CommandPrivilege) CommandListUpdateAction(net.dv8tion.jda.api.requests.restaction.CommandListUpdateAction) Cooldown(me.TechsCode.TechDiscordBot.objects.Cooldown) TechDiscordBot(me.TechsCode.TechDiscordBot.TechDiscordBot) CommandListUpdateAction(net.dv8tion.jda.api.requests.restaction.CommandListUpdateAction) InvocationTargetException(java.lang.reflect.InvocationTargetException) OptionData(net.dv8tion.jda.api.interactions.commands.build.OptionData) CommandData(net.dv8tion.jda.api.interactions.commands.build.CommandData) CommandPrivilege(net.dv8tion.jda.api.interactions.commands.privileges.CommandPrivilege) SubcommandData(net.dv8tion.jda.api.interactions.commands.build.SubcommandData)

Aggregations

SubcommandData (net.dv8tion.jda.api.interactions.commands.build.SubcommandData)12 CommandDataImpl (net.dv8tion.jda.internal.interactions.CommandDataImpl)5 Test (org.junit.jupiter.api.Test)4 List (java.util.List)3 SlashCommandData (net.dv8tion.jda.api.interactions.commands.build.SlashCommandData)3 SubcommandGroupData (net.dv8tion.jda.api.interactions.commands.build.SubcommandGroupData)3 DataArray (net.dv8tion.jda.api.utils.data.DataArray)3 DataObject (net.dv8tion.jda.api.utils.data.DataObject)3 Modifier (java.lang.reflect.Modifier)2 HashMap (java.util.HashMap)2 Map (java.util.Map)2 Nonnull (javax.annotation.Nonnull)2 CommandData (net.dv8tion.jda.api.interactions.commands.build.CommandData)2 OptionData (net.dv8tion.jda.api.interactions.commands.build.OptionData)2 Bean (at.xirado.bean.Bean)1 SlashCommand (at.xirado.bean.command.SlashCommand)1 ISlashCommand (com.dynxsty.dih4jda.commands.interactions.slash_command.ISlashCommand)1 SlashCommandInteraction (com.dynxsty.dih4jda.commands.interactions.slash_command.SlashCommandInteraction)1 ClassPath (com.google.common.reflect.ClassPath)1 BaseCommand (dev.triumphteam.cmd.core.BaseCommand)1