Search in sources :

Example 1 with SlashCommandInteraction

use of com.dynxsty.dih4jda.commands.interactions.slash_command.SlashCommandInteraction in project DIH4JDA by DynxstyGIT.

the class InteractionHandler method registerCommandPrivileges.

/**
 * Registers all Command Privileges.
 *
 * @param jda The {@link JDA} instance.
 */
private void registerCommandPrivileges(JDA jda) {
    for (Guild guild : jda.getGuilds()) {
        Map<String, Set<CommandPrivilege>> privileges = new HashMap<>();
        guild.retrieveCommands().queue(commands -> {
            for (Command command : commands) {
                if (privileges.containsKey(command.getId()))
                    continue;
                Optional<SlashCommandInteraction> interactionOptional = this.slashCommandIndex.keySet().stream().filter(p -> p.equals(command.getName()) || p.split("/")[0].equals(command.getName())).map(slashCommandIndex::get).filter(p -> p.getPrivileges() != null && p.getPrivileges().length > 0).findFirst();
                if (interactionOptional.isPresent()) {
                    SlashCommandInteraction interaction = interactionOptional.get();
                    if (interaction.getBaseClass().getSuperclass().equals(GlobalSlashCommand.class)) {
                        DIH4JDALogger.error(String.format("Can not register command privileges for global command %s (%s).", command.getName(), interaction.getBaseClass().getSimpleName()));
                        continue;
                    }
                    privileges.put(command.getId(), new HashSet<>(Arrays.asList(interaction.getPrivileges())));
                    DIH4JDALogger.info(String.format("[%s] Registered privileges for command %s: %s", guild.getName(), command.getName(), Arrays.stream(interaction.getPrivileges()).map(CommandPrivilege::toData).collect(Collectors.toList())), DIH4JDALogger.Type.COMMAND_PRIVILEGE_REGISTERED);
                }
                if (privileges.isEmpty())
                    continue;
                guild.updateCommandPrivileges(privileges).queue();
            }
        });
    }
}
Also used : java.util(java.util) JDA(net.dv8tion.jda.api.JDA) IMessageContextCommand(com.dynxsty.dih4jda.commands.interactions.context_command.IMessageContextCommand) MessageContextInteractionEvent(net.dv8tion.jda.api.events.interaction.command.MessageContextInteractionEvent) MessageContextInteraction(com.dynxsty.dih4jda.commands.interactions.context_command.MessageContextInteraction) GuildContextCommand(com.dynxsty.dih4jda.commands.interactions.context_command.dao.GuildContextCommand) CompletableFuture(java.util.concurrent.CompletableFuture) Reflections(org.reflections.Reflections) Command(net.dv8tion.jda.api.interactions.commands.Command) UserContextInteractionEvent(net.dv8tion.jda.api.events.interaction.command.UserContextInteractionEvent) Guild(net.dv8tion.jda.api.entities.Guild) DIH4JDA(com.dynxsty.dih4jda.DIH4JDA) SlashCommandInteraction(com.dynxsty.dih4jda.commands.interactions.slash_command.SlashCommandInteraction) DIH4JDALogger(com.dynxsty.dih4jda.DIH4JDALogger) ISlashCommand(com.dynxsty.dih4jda.commands.interactions.slash_command.ISlashCommand) SubcommandGroupData(net.dv8tion.jda.api.interactions.commands.build.SubcommandGroupData) UserContextInteraction(com.dynxsty.dih4jda.commands.interactions.context_command.UserContextInteraction) CommandPrivilege(net.dv8tion.jda.api.interactions.commands.privileges.CommandPrivilege) IUserContextCommand(com.dynxsty.dih4jda.commands.interactions.context_command.IUserContextCommand) SubcommandData(net.dv8tion.jda.api.interactions.commands.build.SubcommandData) GlobalContextCommand(com.dynxsty.dih4jda.commands.interactions.context_command.dao.GlobalContextCommand) SlashCommandInteractionEvent(net.dv8tion.jda.api.events.interaction.command.SlashCommandInteractionEvent) ListenerAdapter(net.dv8tion.jda.api.hooks.ListenerAdapter) CommandData(net.dv8tion.jda.api.interactions.commands.build.CommandData) Collectors(java.util.stream.Collectors) SlashCommandData(net.dv8tion.jda.api.interactions.commands.build.SlashCommandData) Nullable(org.jetbrains.annotations.Nullable) com.dynxsty.dih4jda.commands.interactions.slash_command.dao(com.dynxsty.dih4jda.commands.interactions.slash_command.dao) Contract(org.jetbrains.annotations.Contract) BaseContextCommand(com.dynxsty.dih4jda.commands.interactions.context_command.dao.BaseContextCommand) CommandNotRegisteredException(com.dynxsty.dih4jda.exceptions.CommandNotRegisteredException) CommandListUpdateAction(net.dv8tion.jda.api.requests.restaction.CommandListUpdateAction) NotNull(org.jetbrains.annotations.NotNull) IMessageContextCommand(com.dynxsty.dih4jda.commands.interactions.context_command.IMessageContextCommand) GuildContextCommand(com.dynxsty.dih4jda.commands.interactions.context_command.dao.GuildContextCommand) Command(net.dv8tion.jda.api.interactions.commands.Command) ISlashCommand(com.dynxsty.dih4jda.commands.interactions.slash_command.ISlashCommand) IUserContextCommand(com.dynxsty.dih4jda.commands.interactions.context_command.IUserContextCommand) GlobalContextCommand(com.dynxsty.dih4jda.commands.interactions.context_command.dao.GlobalContextCommand) BaseContextCommand(com.dynxsty.dih4jda.commands.interactions.context_command.dao.BaseContextCommand) Guild(net.dv8tion.jda.api.entities.Guild) SlashCommandInteraction(com.dynxsty.dih4jda.commands.interactions.slash_command.SlashCommandInteraction)

Example 2 with SlashCommandInteraction

use of com.dynxsty.dih4jda.commands.interactions.slash_command.SlashCommandInteraction in project DIH4JDA by DynxstyGIT.

the class InteractionHandler method getSubcommandData.

/**
 * Gets all {@link SubcommandData} from the given array of {@link Subcommand} classes.
 *
 * @param command      The base command's instance.
 * @param subClasses   All sub command classes.
 * @param subGroupName The Subcommand Group's name. (if available)
 * @param guild        The current guild (if available)
 * @return The new {@link CommandListUpdateAction}.
 * @throws Exception If an error occurs.
 */
private Set<SubcommandData> getSubcommandData(BaseSlashCommand command, Class<? extends Subcommand>[] subClasses, @Nullable String subGroupName, @Nullable Guild guild) throws Exception {
    Set<SubcommandData> subDataList = new HashSet<>();
    for (Class<? extends Subcommand> sub : subClasses) {
        Subcommand instance = (Subcommand) this.getClassInstance(guild, sub);
        if (instance.getSubcommandData() == null) {
            DIH4JDALogger.warn(String.format("Class %s is missing SubcommandData. It will be ignored.", sub.getName()));
            continue;
        }
        String commandPath;
        if (subGroupName == null) {
            commandPath = buildCommandPath(command.getCommandData().getName(), instance.getSubcommandData().getName());
        } else {
            commandPath = buildCommandPath(command.getCommandData().getName(), subGroupName, instance.getSubcommandData().getName());
        }
        slashCommandIndex.put(commandPath, new SlashCommandInteraction((ISlashCommand) instance, sub, command.getCommandPrivileges()));
        DIH4JDALogger.info(String.format("\t[*] Registered command: /%s", commandPath), DIH4JDALogger.Type.SLASH_COMMAND_REGISTERED);
        subDataList.add(instance.getSubcommandData());
    }
    return subDataList;
}
Also used : ISlashCommand(com.dynxsty.dih4jda.commands.interactions.slash_command.ISlashCommand) SubcommandData(net.dv8tion.jda.api.interactions.commands.build.SubcommandData) SlashCommandInteraction(com.dynxsty.dih4jda.commands.interactions.slash_command.SlashCommandInteraction)

Example 3 with SlashCommandInteraction

use of com.dynxsty.dih4jda.commands.interactions.slash_command.SlashCommandInteraction in project DIH4JDA by DynxstyGIT.

the class InteractionHandler method getBaseCommandData.

/**
 * Gets the complete {@link SlashCommandData} (including Subcommands & Subcommand Groups) of a single {@link BaseSlashCommand}.
 *
 * @param command      The base command's instance.
 * @param commandClass The base command's class.
 * @param guild        The current guild (if available)
 * @return The new {@link CommandListUpdateAction}.
 * @throws Exception If an error occurs.
 */
private SlashCommandData getBaseCommandData(@NotNull BaseSlashCommand command, Class<? extends BaseSlashCommand> commandClass, @Nullable Guild guild) throws Exception {
    if (command.getCommandData() == null) {
        DIH4JDALogger.warn(String.format("Class %s is missing CommandData. It will be ignored.", commandClass.getName()));
        return null;
    }
    SlashCommandData commandData = command.getCommandData();
    if (command.getSubcommandGroups() != null) {
        commandData.addSubcommandGroups(this.getSubcommandGroupData(command, guild));
    }
    if (command.getSubcommands() != null) {
        commandData.addSubcommands(this.getSubcommandData(command, command.getSubcommands(), null, guild));
    }
    if (command.getSubcommandGroups() == null && command.getSubcommands() == null) {
        slashCommandIndex.put(buildCommandPath(commandData.getName()), new SlashCommandInteraction((ISlashCommand) command, commandClass, command.getCommandPrivileges()));
        DIH4JDALogger.info(String.format("\t[*] Registered command: /%s", command.getCommandData().getName()), DIH4JDALogger.Type.SLASH_COMMAND_REGISTERED);
    }
    return commandData;
}
Also used : ISlashCommand(com.dynxsty.dih4jda.commands.interactions.slash_command.ISlashCommand) SlashCommandData(net.dv8tion.jda.api.interactions.commands.build.SlashCommandData) SlashCommandInteraction(com.dynxsty.dih4jda.commands.interactions.slash_command.SlashCommandInteraction)

Aggregations

ISlashCommand (com.dynxsty.dih4jda.commands.interactions.slash_command.ISlashCommand)3 SlashCommandInteraction (com.dynxsty.dih4jda.commands.interactions.slash_command.SlashCommandInteraction)3 SlashCommandData (net.dv8tion.jda.api.interactions.commands.build.SlashCommandData)2 SubcommandData (net.dv8tion.jda.api.interactions.commands.build.SubcommandData)2 DIH4JDA (com.dynxsty.dih4jda.DIH4JDA)1 DIH4JDALogger (com.dynxsty.dih4jda.DIH4JDALogger)1 IMessageContextCommand (com.dynxsty.dih4jda.commands.interactions.context_command.IMessageContextCommand)1 IUserContextCommand (com.dynxsty.dih4jda.commands.interactions.context_command.IUserContextCommand)1 MessageContextInteraction (com.dynxsty.dih4jda.commands.interactions.context_command.MessageContextInteraction)1 UserContextInteraction (com.dynxsty.dih4jda.commands.interactions.context_command.UserContextInteraction)1 BaseContextCommand (com.dynxsty.dih4jda.commands.interactions.context_command.dao.BaseContextCommand)1 GlobalContextCommand (com.dynxsty.dih4jda.commands.interactions.context_command.dao.GlobalContextCommand)1 GuildContextCommand (com.dynxsty.dih4jda.commands.interactions.context_command.dao.GuildContextCommand)1 com.dynxsty.dih4jda.commands.interactions.slash_command.dao (com.dynxsty.dih4jda.commands.interactions.slash_command.dao)1 CommandNotRegisteredException (com.dynxsty.dih4jda.exceptions.CommandNotRegisteredException)1 java.util (java.util)1 CompletableFuture (java.util.concurrent.CompletableFuture)1 Collectors (java.util.stream.Collectors)1 JDA (net.dv8tion.jda.api.JDA)1 Guild (net.dv8tion.jda.api.entities.Guild)1