Search in sources :

Example 1 with InteractionCommand

use of net.essentialsx.api.v2.services.discord.InteractionCommand 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 2 with InteractionCommand

use of net.essentialsx.api.v2.services.discord.InteractionCommand in project Essentials by drtshock.

the class InteractionControllerImpl method onSlashCommand.

@Override
public void onSlashCommand(@NotNull SlashCommandEvent event) {
    if (event.getGuild() == null || event.getMember() == null || !commandMap.containsKey(event.getName())) {
        return;
    }
    final InteractionCommand command = commandMap.get(event.getName());
    if (command.isDisabled()) {
        event.reply(tl("discordErrorCommandDisabled")).setEphemeral(true).queue();
        return;
    }
    event.deferReply(command.isEphemeral()).queue(null, failure -> logger.log(Level.SEVERE, "Error while deferring Discord command", failure));
    final InteractionEvent interactionEvent = new InteractionEventImpl(event);
    if (!DiscordUtil.hasRoles(event.getMember(), jda.getSettings().getCommandSnowflakes(command.getName()))) {
        interactionEvent.reply(tl("noAccessCommand"));
        return;
    }
    jda.getPlugin().getEss().scheduleSyncDelayedTask(() -> command.onCommand(interactionEvent));
}
Also used : InteractionCommand(net.essentialsx.api.v2.services.discord.InteractionCommand) InteractionEvent(net.essentialsx.api.v2.services.discord.InteractionEvent)

Example 3 with InteractionCommand

use of net.essentialsx.api.v2.services.discord.InteractionCommand in project Essentials by EssentialsX.

the class InteractionControllerImpl method onSlashCommand.

@Override
public void onSlashCommand(@NotNull SlashCommandEvent event) {
    if (event.getGuild() == null || event.getMember() == null || !commandMap.containsKey(event.getName())) {
        return;
    }
    final InteractionCommand command = commandMap.get(event.getName());
    if (command.isDisabled()) {
        event.reply(tl("discordErrorCommandDisabled")).setEphemeral(true).queue();
        return;
    }
    event.deferReply(command.isEphemeral()).queue(null, failure -> logger.log(Level.SEVERE, "Error while deferring Discord command", failure));
    final InteractionEvent interactionEvent = new InteractionEventImpl(event);
    if (!DiscordUtil.hasRoles(event.getMember(), jda.getSettings().getCommandSnowflakes(command.getName()))) {
        interactionEvent.reply(tl("noAccessCommand"));
        return;
    }
    jda.getPlugin().getEss().scheduleSyncDelayedTask(() -> command.onCommand(interactionEvent));
}
Also used : InteractionCommand(net.essentialsx.api.v2.services.discord.InteractionCommand) InteractionEvent(net.essentialsx.api.v2.services.discord.InteractionEvent)

Example 4 with InteractionCommand

use of net.essentialsx.api.v2.services.discord.InteractionCommand 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 5 with InteractionCommand

use of net.essentialsx.api.v2.services.discord.InteractionCommand in project Essentials by EssentialsX.

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)

Aggregations

ErrorResponseException (net.dv8tion.jda.api.exceptions.ErrorResponseException)4 CommandData (net.dv8tion.jda.api.interactions.commands.build.CommandData)4 InteractionCommand (net.essentialsx.api.v2.services.discord.InteractionCommand)4 InteractionCommandArgument (net.essentialsx.api.v2.services.discord.InteractionCommandArgument)4 ArrayList (java.util.ArrayList)2 Command (net.dv8tion.jda.api.interactions.commands.Command)2 InteractionEvent (net.essentialsx.api.v2.services.discord.InteractionEvent)2 InteractionException (net.essentialsx.api.v2.services.discord.InteractionException)2