Search in sources :

Example 6 with Command

use of net.dv8tion.jda.api.interactions.commands.Command in project BotCommands by freya022.

the class ApplicationCommandsUpdater method computeContextCommands.

@SuppressWarnings("unchecked")
private <T extends ApplicationCommandInfo> void computeContextCommands(List<ApplicationCommandInfo> guildApplicationCommands, Class<T> targetClazz, Command.Type type) {
    guildApplicationCommands.stream().filter(a -> targetClazz.isAssignableFrom(a.getClass())).map(a -> (T) a).distinct().forEachOrdered(info -> {
        final CommandPath notLocalizedPath = info.getPath();
        try {
            final LocalizedCommandData localizedCommandData = LocalizedCommandData.of(context, guild, info);
            localizedBaseNameToBaseName.put(localizedCommandData.getLocalizedPath().getName(), notLocalizedPath.getName());
            // User command name
            final CommandPath localizedPath = localizedCommandData.getLocalizedPath();
            Checks.check(localizedPath.getNameCount() == notLocalizedPath.getNameCount(), "Localized path does not have the same name count as the not-localized path");
            final boolean isDefaultEnabled = isDefaultEnabled(info);
            if (localizedPath.getNameCount() == 1) {
                // Standard command
                final CommandData rightCommand = Commands.context(type, localizedPath.getName());
                map.put(type, localizedPath, rightCommand);
                rightCommand.setDefaultEnabled(isDefaultEnabled);
                if (info.isOwnerRequired()) {
                    // Must be non-localized name
                    ownerOnlyCommands.add(notLocalizedPath.getName());
                }
            } else {
                throw new IllegalStateException("A " + type.name() + " command with more than 1 path component got registered");
            }
            context.addApplicationCommandAlternative(localizedPath, type, info);
        } catch (Exception e) {
            throw new RuntimeException("An exception occurred while processing a " + type.name() + " command " + notLocalizedPath, e);
        }
    });
}
Also used : java.util(java.util) Logging(com.freya02.botcommands.api.Logging) UserCommandInfo(com.freya02.botcommands.internal.application.context.user.UserCommandInfo) Command(net.dv8tion.jda.api.interactions.commands.Command) Function(java.util.function.Function) DebugBuilder(com.freya02.botcommands.api.builder.DebugBuilder) Guild(net.dv8tion.jda.api.entities.Guild) CommandPath(com.freya02.botcommands.api.application.CommandPath) CommandPrivilege(net.dv8tion.jda.api.interactions.commands.privileges.CommandPrivilege) Blocking(org.jetbrains.annotations.Blocking) Path(java.nio.file.Path) SettingsProvider(com.freya02.botcommands.api.SettingsProvider) SlashUtils.getLocalizedMethodOptions(com.freya02.botcommands.internal.application.slash.SlashUtils.getLocalizedMethodOptions) Checks(net.dv8tion.jda.internal.utils.Checks) Logger(org.slf4j.Logger) BContextImpl(com.freya02.botcommands.internal.BContextImpl) Utils(com.freya02.botcommands.internal.utils.Utils) MessageCommandInfo(com.freya02.botcommands.internal.application.context.message.MessageCommandInfo) Files(java.nio.file.Files) StandardOpenOption(java.nio.file.StandardOpenOption) IOException(java.io.IOException) Collectors(java.util.stream.Collectors) net.dv8tion.jda.api.interactions.commands.build(net.dv8tion.jda.api.interactions.commands.build) Nullable(org.jetbrains.annotations.Nullable) SlashUtils.appendCommands(com.freya02.botcommands.internal.application.slash.SlashUtils.appendCommands) SlashCommandInfo(com.freya02.botcommands.internal.application.slash.SlashCommandInfo) CommandListUpdateAction(net.dv8tion.jda.api.requests.restaction.CommandListUpdateAction) NotNull(org.jetbrains.annotations.NotNull) CommandPath(com.freya02.botcommands.api.application.CommandPath) IOException(java.io.IOException)

Example 7 with Command

use of net.dv8tion.jda.api.interactions.commands.Command in project SqueethDiscordBot by AlphaSerpentis.

the class About method addCommand.

@Override
public void addCommand(JDA jda) {
    Command cmd = jda.upsertCommand(name, description).complete();
    commandId = cmd.getIdLong();
}
Also used : Command(net.dv8tion.jda.api.interactions.commands.Command)

Example 8 with Command

use of net.dv8tion.jda.api.interactions.commands.Command in project SqueethDiscordBot by AlphaSerpentis.

the class Funding method addCommand.

@Override
public void addCommand(JDA jda) {
    Command cmd = jda.upsertCommand(name, description).addOption(OptionType.NUMBER, "amount", "The amount of oSQTH in USD you have", true).addOption(OptionType.INTEGER, "days", "The amount of days you will maintain this position", true).addOption(OptionType.NUMBER, "funding", "The funding rate in %", false).complete();
    commandId = cmd.getIdLong();
}
Also used : Command(net.dv8tion.jda.api.interactions.commands.Command)

Example 9 with Command

use of net.dv8tion.jda.api.interactions.commands.Command in project Essentials by EssentialsX.

the class InteractionControllerImpl method processBatchRegistration.

public void processBatchRegistration() {
    if (!initialBatchRegistration && !batchRegistrationQueue.isEmpty()) {
        initialBatchRegistration = true;
        final List<CommandData> list = new ArrayList<>();
        for (final InteractionCommand command : batchRegistrationQueue.values()) {
            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());
                }
            }
            list.add(data);
        }
        jda.getGuild().updateCommands().addCommands(list).queue(success -> {
            for (final Command command : success) {
                commandMap.put(command.getName(), batchRegistrationQueue.get(command.getName()));
                batchRegistrationQueue.remove(command.getName());
                if (jda.isDebug()) {
                    logger.info("Registered guild command " + command.getName() + " with id " + command.getId());
                }
            }
            if (!batchRegistrationQueue.isEmpty()) {
                logger.warning(batchRegistrationQueue.size() + " Discord commands were lost during command registration!");
                if (jda.isDebug()) {
                    logger.warning("Lost commands: " + batchRegistrationQueue.keySet());
                }
                batchRegistrationQueue.clear();
            }
        }, 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) InteractionCommand(net.essentialsx.api.v2.services.discord.InteractionCommand) Command(net.dv8tion.jda.api.interactions.commands.Command) InteractionCommand(net.essentialsx.api.v2.services.discord.InteractionCommand) ArrayList(java.util.ArrayList) ErrorResponseException(net.dv8tion.jda.api.exceptions.ErrorResponseException) CommandData(net.dv8tion.jda.api.interactions.commands.build.CommandData)

Example 10 with Command

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

the class InteractionControllerImpl method processBatchRegistration.

public void processBatchRegistration() {
    if (!initialBatchRegistration && !batchRegistrationQueue.isEmpty()) {
        initialBatchRegistration = true;
        final List<CommandData> list = new ArrayList<>();
        for (final InteractionCommand command : batchRegistrationQueue.values()) {
            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());
                }
            }
            list.add(data);
        }
        jda.getGuild().updateCommands().addCommands(list).queue(success -> {
            for (final Command command : success) {
                commandMap.put(command.getName(), batchRegistrationQueue.get(command.getName()));
                batchRegistrationQueue.remove(command.getName());
                if (jda.isDebug()) {
                    logger.info("Registered guild command " + command.getName() + " with id " + command.getId());
                }
            }
            if (!batchRegistrationQueue.isEmpty()) {
                logger.warning(batchRegistrationQueue.size() + " Discord commands were lost during command registration!");
                if (jda.isDebug()) {
                    logger.warning("Lost commands: " + batchRegistrationQueue.keySet());
                }
                batchRegistrationQueue.clear();
            }
        }, 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) InteractionCommand(net.essentialsx.api.v2.services.discord.InteractionCommand) Command(net.dv8tion.jda.api.interactions.commands.Command) InteractionCommand(net.essentialsx.api.v2.services.discord.InteractionCommand) ArrayList(java.util.ArrayList) ErrorResponseException(net.dv8tion.jda.api.exceptions.ErrorResponseException) CommandData(net.dv8tion.jda.api.interactions.commands.build.CommandData)

Aggregations

Command (net.dv8tion.jda.api.interactions.commands.Command)19 ArrayList (java.util.ArrayList)7 Guild (net.dv8tion.jda.api.entities.Guild)6 CommandListUpdateAction (net.dv8tion.jda.api.requests.restaction.CommandListUpdateAction)6 List (java.util.List)5 IOException (java.io.IOException)4 Collectors (java.util.stream.Collectors)4 MessageContextInteractionEvent (net.dv8tion.jda.api.events.interaction.command.MessageContextInteractionEvent)4 SlashCommandInteractionEvent (net.dv8tion.jda.api.events.interaction.command.SlashCommandInteractionEvent)4 UserContextInteractionEvent (net.dv8tion.jda.api.events.interaction.command.UserContextInteractionEvent)4 CommandData (net.dv8tion.jda.api.interactions.commands.build.CommandData)4 NotNull (org.jetbrains.annotations.NotNull)4 Nullable (org.jetbrains.annotations.Nullable)4 Bean (at.xirado.bean.Bean)3 CommandFlag (at.xirado.bean.command.CommandFlag)3 GenericCommand (at.xirado.bean.command.GenericCommand)3 SlashCommand (at.xirado.bean.command.SlashCommand)3 SlashCommandContext (at.xirado.bean.command.SlashCommandContext)3 java.util (java.util)3 CommandPrivilege (net.dv8tion.jda.api.interactions.commands.privileges.CommandPrivilege)3