Search in sources :

Example 1 with CommandData

use of net.dv8tion.jda.api.interactions.commands.build.CommandData in project cjda-util by RainbowDashLabs.

the class CommandHub method updateCommands.

private void updateCommands() {
    log.info("Updating slash commands.");
    for (var language : localizer.languages()) {
        log.info("Creating command data for {} language", language.getCode());
        List<CommandData> localizedCommandData = new ArrayList<>();
        for (var command : new HashSet<>(commands.values())) {
            localizedCommandData.add(command.getCommandData(localizer, language));
        }
        commandData.put(language, localizedCommandData);
    }
    for (var command : new HashSet<>(commands.values())) {
        if (command.subCommands() != null) {
            log.info("Registering command {} with {} subcommands", command.command(), command.subCommands().length);
        } else {
            log.info("Registering command {}.", command.command());
        }
    }
    for (JDA shard : shardManager.getShards()) {
        try {
            shard.awaitReady();
            log.info("Shard {} is ready.", shard.getShardInfo().getShardId());
        } catch (InterruptedException e) {
        // ignore
        }
    }
    if (useSlashGlobalCommands) {
        var baseShard = shardManager.getShards().get(0);
        log.info("Removing guild commands");
        for (JDA shard : shardManager.getShards()) {
            for (Guild guild : shard.getGuilds()) {
                guild.updateCommands().complete();
            }
        }
        log.info("Updating global slash commands.");
        baseShard.updateCommands().addCommands(commandData.get(localizer.defaultLanguage())).complete();
        return;
    }
    var baseShard = shardManager.getShards().get(0);
    log.info("Removing global slash commands and using guild commands.");
    baseShard.updateCommands().complete();
    for (var shard : shardManager.getShards()) {
        for (var guild : shard.getGuilds()) {
            refreshGuildCommands(guild);
        }
    }
}
Also used : JDA(net.dv8tion.jda.api.JDA) ArrayList(java.util.ArrayList) CommandData(net.dv8tion.jda.api.interactions.commands.build.CommandData) Guild(net.dv8tion.jda.api.entities.Guild) HashSet(java.util.HashSet)

Example 2 with CommandData

use of net.dv8tion.jda.api.interactions.commands.build.CommandData in project DIH4JDA by DynxstyGIT.

the class InteractionHandler method registerInteractions.

public void registerInteractions(JDA jda) throws Exception {
    this.registerSlashCommands();
    this.registerContextCommands();
    for (Guild guild : jda.getGuilds()) {
        Set<CommandData> commands = new HashSet<>();
        commands.addAll(this.getGuildSlashCommandData(guild));
        commands.addAll(this.getGuildContextCommandData(guild));
        guild.updateCommands().addCommands(commands).queue();
    }
    Set<CommandData> commands = new HashSet<>();
    commands.addAll(this.getGlobalSlashCommandData());
    commands.addAll(this.getGlobalContextCommandData());
    jda.updateCommands().addCommands(commands).queue();
    this.registerCommandPrivileges(jda);
}
Also used : Guild(net.dv8tion.jda.api.entities.Guild) CommandData(net.dv8tion.jda.api.interactions.commands.build.CommandData) SlashCommandData(net.dv8tion.jda.api.interactions.commands.build.SlashCommandData)

Example 3 with CommandData

use of net.dv8tion.jda.api.interactions.commands.build.CommandData in project DIH4JDA by DynxstyGIT.

the class InteractionHandler method getGlobalContextCommandData.

/**
 * Gets all Global Context commands registered in {@link InteractionHandler#registerContextCommands()} and
 * returns their {@link CommandData} as a List.
 *
 * @throws Exception If an error occurs.
 */
private Set<CommandData> getGlobalContextCommandData() throws Exception {
    Set<CommandData> commands = new HashSet<>();
    for (Class<? extends BaseContextCommand> contextCommandClass : this.globalContexts) {
        BaseContextCommand instance = (BaseContextCommand) this.getClassInstance(null, contextCommandClass);
        CommandData data = this.getContextCommandData(instance, contextCommandClass);
        if (data != null) {
            commands.add(data);
        }
    }
    DIH4JDALogger.info("[*] Queuing Global Context Commands", DIH4JDALogger.Type.CONTEXT_COMMANDS_QUEUED);
    return commands;
}
Also used : BaseContextCommand(com.dynxsty.dih4jda.commands.interactions.context_command.dao.BaseContextCommand) CommandData(net.dv8tion.jda.api.interactions.commands.build.CommandData) SlashCommandData(net.dv8tion.jda.api.interactions.commands.build.SlashCommandData)

Example 4 with CommandData

use of net.dv8tion.jda.api.interactions.commands.build.CommandData in project Essentials by drtshock.

the class InteractionControllerImpl method registerCommand.

@Override
public void registerCommand(InteractionCommand command) throws InteractionException {
    if (command.isDisabled()) {
        throw new InteractionException("The given command has been disabled!");
    }
    if (commandMap.containsKey(command.getName())) {
        throw new InteractionException("A command with that name is already registered!");
    }
    if (!initialBatchRegistration) {
        if (jda.isDebug()) {
            logger.info("Marked guild command for batch registration: " + command.getName());
        }
        batchRegistrationQueue.put(command.getName(), command);
        return;
    }
    final CommandData data = new CommandData(command.getName(), command.getDescription());
    if (command.getArguments() != null) {
        for (final InteractionCommandArgument argument : command.getArguments()) {
            data.addOption(OptionType.valueOf(argument.getType().name()), argument.getName(), argument.getDescription(), argument.isRequired());
        }
    }
    jda.getGuild().upsertCommand(data).queue(success -> {
        commandMap.put(command.getName(), command);
        if (jda.isDebug()) {
            logger.info("Registered guild command " + success.getName() + " with id " + success.getId());
        }
    }, failure -> {
        if (failure instanceof ErrorResponseException && ((ErrorResponseException) failure).getErrorResponse() == ErrorResponse.MISSING_ACCESS) {
            logger.severe(tl("discordErrorCommand"));
            return;
        }
        logger.log(Level.SEVERE, "Error while registering command", failure);
    });
}
Also used : InteractionCommandArgument(net.essentialsx.api.v2.services.discord.InteractionCommandArgument) InteractionException(net.essentialsx.api.v2.services.discord.InteractionException) ErrorResponseException(net.dv8tion.jda.api.exceptions.ErrorResponseException) CommandData(net.dv8tion.jda.api.interactions.commands.build.CommandData)

Example 5 with CommandData

use of net.dv8tion.jda.api.interactions.commands.build.CommandData in project JDA by DV8FromTheWorld.

the class CommandListUpdateActionImpl method addCommands.

@Nonnull
@Override
public CommandListUpdateAction addCommands(@Nonnull Collection<? extends CommandData> commands) {
    Checks.noneNull(commands, "Command");
    int newSlash = 0, newUser = 0, newMessage = 0;
    for (CommandData command : commands) {
        switch(command.getType()) {
            case SLASH:
                newSlash++;
                break;
            case MESSAGE:
                newMessage++;
                break;
            case USER:
                newUser++;
                break;
        }
    }
    Checks.check(slash + newSlash <= Commands.MAX_SLASH_COMMANDS, "Cannot have more than %d slash commands! Try using subcommands instead.", Commands.MAX_SLASH_COMMANDS);
    Checks.check(user + newUser <= Commands.MAX_USER_COMMANDS, "Cannot have more than %d user context commands!", Commands.MAX_USER_COMMANDS);
    Checks.check(message + newMessage <= Commands.MAX_MESSAGE_COMMANDS, "Cannot have more than %d message context commands!", Commands.MAX_MESSAGE_COMMANDS);
    Checks.checkUnique(Stream.concat(commands.stream(), this.commands.stream()).map(c -> c.getType() + " " + c.getName()), "Cannot have multiple commands of the same type with identical names. " + "Name: \"%s\" with type %s appeared %d times!", (count, value) -> {
        String[] tuple = value.split(" ", 2);
        return new Object[] { tuple[1], tuple[0], count };
    });
    slash += newSlash;
    user += newUser;
    message += newMessage;
    this.commands.addAll(commands);
    return this;
}
Also used : DataArray(net.dv8tion.jda.api.utils.data.DataArray) Checks(net.dv8tion.jda.internal.utils.Checks) JDA(net.dv8tion.jda.api.JDA) Collection(java.util.Collection) Request(net.dv8tion.jda.api.requests.Request) CommandData(net.dv8tion.jda.api.interactions.commands.build.CommandData) Route(net.dv8tion.jda.internal.requests.Route) RestActionImpl(net.dv8tion.jda.internal.requests.RestActionImpl) Command(net.dv8tion.jda.api.interactions.commands.Command) Collectors(java.util.stream.Collectors) RequestBody(okhttp3.RequestBody) ArrayList(java.util.ArrayList) BooleanSupplier(java.util.function.BooleanSupplier) TimeUnit(java.util.concurrent.TimeUnit) Commands(net.dv8tion.jda.api.interactions.commands.build.Commands) CommandImpl(net.dv8tion.jda.internal.interactions.command.CommandImpl) List(java.util.List) Stream(java.util.stream.Stream) Response(net.dv8tion.jda.api.requests.Response) CommandListUpdateAction(net.dv8tion.jda.api.requests.restaction.CommandListUpdateAction) Nonnull(javax.annotation.Nonnull) GuildImpl(net.dv8tion.jda.internal.entities.GuildImpl) CommandData(net.dv8tion.jda.api.interactions.commands.build.CommandData) Nonnull(javax.annotation.Nonnull)

Aggregations

CommandData (net.dv8tion.jda.api.interactions.commands.build.CommandData)20 ArrayList (java.util.ArrayList)6 JDA (net.dv8tion.jda.api.JDA)5 SlashCommandData (net.dv8tion.jda.api.interactions.commands.build.SlashCommandData)5 Guild (net.dv8tion.jda.api.entities.Guild)4 List (java.util.List)3 ErrorResponseException (net.dv8tion.jda.api.exceptions.ErrorResponseException)3 InteractionCommandArgument (net.essentialsx.api.v2.services.discord.InteractionCommandArgument)3 BaseContextCommand (com.dynxsty.dih4jda.commands.interactions.context_command.dao.BaseContextCommand)2 Nonnull (javax.annotation.Nonnull)2 WrappedException (net.anweisen.utilities.common.collection.WrappedException)2 Command (net.dv8tion.jda.api.interactions.commands.Command)2 OptionData (net.dv8tion.jda.api.interactions.commands.build.OptionData)2 SubcommandData (net.dv8tion.jda.api.interactions.commands.build.SubcommandData)2 CommandListUpdateAction (net.dv8tion.jda.api.requests.restaction.CommandListUpdateAction)2 InteractionCommand (net.essentialsx.api.v2.services.discord.InteractionCommand)2 Test (org.junit.jupiter.api.Test)2 IMessageContextCommand (com.dynxsty.dih4jda.commands.interactions.context_command.IMessageContextCommand)1 MessageContextInteraction (com.dynxsty.dih4jda.commands.interactions.context_command.MessageContextInteraction)1 UserContextInteraction (com.dynxsty.dih4jda.commands.interactions.context_command.UserContextInteraction)1