Search in sources :

Example 11 with CommandListUpdateAction

use of net.dv8tion.jda.api.requests.restaction.CommandListUpdateAction 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

CommandListUpdateAction (net.dv8tion.jda.api.requests.restaction.CommandListUpdateAction)11 ArrayList (java.util.ArrayList)4 List (java.util.List)4 Guild (net.dv8tion.jda.api.entities.Guild)4 CommandData (net.dv8tion.jda.api.interactions.commands.build.CommandData)4 OptionData (net.dv8tion.jda.api.interactions.commands.build.OptionData)4 JDA (net.dv8tion.jda.api.JDA)3 Arrays (java.util.Arrays)2 Map (java.util.Map)2 Collectors (java.util.stream.Collectors)2 Command (net.dv8tion.jda.api.interactions.commands.Command)2 Commands (net.dv8tion.jda.api.interactions.commands.build.Commands)2 GenericCommand (at.xirado.bean.command.GenericCommand)1 AbstractIdleService (com.google.common.util.concurrent.AbstractIdleService)1 Color (java.awt.Color)1 FileInputStream (java.io.FileInputStream)1 IOException (java.io.IOException)1 PrintWriter (java.io.PrintWriter)1 StringWriter (java.io.StringWriter)1 InvocationTargetException (java.lang.reflect.InvocationTargetException)1