Search in sources :

Example 11 with OptionData

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

OptionData (net.dv8tion.jda.api.interactions.commands.build.OptionData)11 CommandListUpdateAction (net.dv8tion.jda.api.requests.restaction.CommandListUpdateAction)5 ArrayList (java.util.ArrayList)3 List (java.util.List)3 Guild (net.dv8tion.jda.api.entities.Guild)2 Command (net.dv8tion.jda.api.interactions.commands.Command)2 OptionType (net.dv8tion.jda.api.interactions.commands.OptionType)2 SubcommandData (net.dv8tion.jda.api.interactions.commands.build.SubcommandData)2 Bean (at.xirado.bean.Bean)1 SlashCommand (at.xirado.bean.command.SlashCommand)1 AbstractSubCommand (dev.triumphteam.cmd.core.AbstractSubCommand)1 InternalArgument (dev.triumphteam.cmd.core.argument.InternalArgument)1 ExecutionProvider (dev.triumphteam.cmd.core.execution.ExecutionProvider)1 Choice (dev.triumphteam.cmd.slash.choices.Choice)1 EmptyChoice (dev.triumphteam.cmd.slash.choices.EmptyChoice)1 JdaOptionUtil (dev.triumphteam.cmd.slash.util.JdaOptionUtil)1 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 Modifier (java.lang.reflect.Modifier)1 Parameter (java.lang.reflect.Parameter)1 Arrays (java.util.Arrays)1