Search in sources :

Example 11 with Command

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

the class CommandData method fromData.

/**
 * Parses the provided serialization back into an CommandData instance.
 * <br>This is the reverse function for {@link CommandData#toData()}.
 *
 * @param  object
 *         The serialized {@link DataObject} representing the command
 *
 * @throws net.dv8tion.jda.api.exceptions.ParsingException
 *         If the serialized object is missing required fields
 * @throws IllegalArgumentException
 *         If any of the values are failing the respective checks such as length
 *
 * @return The parsed CommandData instance, which can be further configured through setters
 *
 * @see    SlashCommandData#fromData(DataObject)
 * @see    Commands#fromList(Collection)
 */
@Nonnull
static CommandData fromData(@Nonnull DataObject object) {
    Checks.notNull(object, "DataObject");
    String name = object.getString("name");
    Command.Type commandType = Command.Type.fromId(object.getInt("type", 1));
    if (commandType != Command.Type.SLASH)
        return new CommandDataImpl(commandType, name);
    return SlashCommandData.fromData(object);
}
Also used : Command(net.dv8tion.jda.api.interactions.commands.Command) CommandDataImpl(net.dv8tion.jda.internal.interactions.CommandDataImpl) Nonnull(javax.annotation.Nonnull)

Example 12 with Command

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

the class SlashCommandData method fromData.

/**
 * Parses the provided serialization back into a SlashCommandData instance.
 * <br>This is the reverse function for {@link SlashCommandData#toData()}.
 *
 * @param  object
 *         The serialized {@link DataObject} representing the command
 *
 * @throws net.dv8tion.jda.api.exceptions.ParsingException
 *         If the serialized object is missing required fields
 * @throws IllegalArgumentException
 *         If any of the values are failing the respective checks such as length
 *
 * @return The parsed SlashCommandData instance, which can be further configured through setters
 *
 * @see    CommandData#fromData(DataObject)
 * @see    Commands#fromList(Collection)
 */
@Nonnull
static SlashCommandData fromData(@Nonnull DataObject object) {
    Checks.notNull(object, "DataObject");
    String name = object.getString("name");
    Command.Type commandType = Command.Type.fromId(object.getInt("type", 1));
    if (commandType != Command.Type.SLASH)
        throw new IllegalArgumentException("Cannot convert command of type " + commandType + " to SlashCommandData!");
    String description = object.getString("description");
    DataArray options = object.optArray("options").orElseGet(DataArray::empty);
    CommandDataImpl command = new CommandDataImpl(name, description);
    options.stream(DataArray::getObject).forEach(opt -> {
        OptionType type = OptionType.fromKey(opt.getInt("type"));
        switch(type) {
            case SUB_COMMAND:
                command.addSubcommands(SubcommandData.fromData(opt));
                break;
            case SUB_COMMAND_GROUP:
                command.addSubcommandGroups(SubcommandGroupData.fromData(opt));
                break;
            default:
                command.addOptions(OptionData.fromData(opt));
        }
    });
    return command;
}
Also used : Command(net.dv8tion.jda.api.interactions.commands.Command) CommandDataImpl(net.dv8tion.jda.internal.interactions.CommandDataImpl) DataArray(net.dv8tion.jda.api.utils.data.DataArray) OptionType(net.dv8tion.jda.api.interactions.commands.OptionType) Nonnull(javax.annotation.Nonnull)

Example 13 with Command

use of net.dv8tion.jda.api.interactions.commands.Command in project discord-bot-reddit-java by Glaxier0.

the class RetrievePermissionCommand method deleteCommandsFromGuild.

private void deleteCommandsFromGuild(Guild guild) throws IllegalStateException {
    RestAction<List<Command>> guildCommands = guild.retrieveCommands();
    List<Command> commandList = guildCommands.complete();
    for (Command command : commandList) {
        command.delete();
    }
    guild.updateCommands().queue();
}
Also used : Command(net.dv8tion.jda.api.interactions.commands.Command) ISlashCommand(com.discord.bot.commands.ISlashCommand) List(java.util.List)

Example 14 with Command

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

the class ApplicationCommandsUpdater method thenAcceptGuild.

private void thenAcceptGuild(List<Command> commands, @NotNull Guild guild) {
    for (Command command : commands) {
        context.getRegistrationListeners().forEach(l -> l.onGuildSlashCommandRegistered(this.guild, command));
    }
    this.commands.clear();
    this.commands.addAll(commands);
    try {
        Files.write(commandsCachePath, ApplicationCommandsCache.getCommandsBytes(allCommandData), StandardOpenOption.CREATE, StandardOpenOption.TRUNCATE_EXISTING);
    } catch (IOException e) {
        LOGGER.error("An exception occurred while temporarily saving guild ({} ({})) commands in '{}'", guild.getName(), guild.getId(), commandsCachePath.toAbsolutePath(), e);
    }
    if (!LOGGER.isTraceEnabled())
        return;
    final StringBuilder sb = new StringBuilder("Updated " + commands.size() + " / " + allCommandData.size() + " (" + context.getApplicationCommandsView().size() + ") commands for ");
    sb.append(guild.getName()).append(" :\n");
    appendCommands(commands, sb);
    LOGGER.trace(sb.toString().trim());
}
Also used : Command(net.dv8tion.jda.api.interactions.commands.Command) IOException(java.io.IOException)

Example 15 with Command

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

the class ApplicationCommandsUpdater method thenAcceptGlobal.

private void thenAcceptGlobal(List<Command> commands) {
    for (Command command : commands) {
        context.getRegistrationListeners().forEach(l -> l.onGlobalSlashCommandRegistered(command));
    }
    try {
        Files.write(commandsCachePath, ApplicationCommandsCache.getCommandsBytes(allCommandData), StandardOpenOption.CREATE, StandardOpenOption.TRUNCATE_EXISTING);
    } catch (IOException e) {
        LOGGER.error("An exception occurred while temporarily saving {} commands in '{}'", guild == null ? "global" : String.format("guild '%s' (%s)", guild.getName(), guild.getId()), commandsCachePath.toAbsolutePath(), e);
    }
    if (!LOGGER.isTraceEnabled())
        return;
    final StringBuilder sb = new StringBuilder("Updated global commands:\n");
    appendCommands(commands, sb);
    LOGGER.trace(sb.toString().trim());
}
Also used : Command(net.dv8tion.jda.api.interactions.commands.Command) IOException(java.io.IOException)

Aggregations

Command (net.dv8tion.jda.api.interactions.commands.Command)22 ArrayList (java.util.ArrayList)8 Guild (net.dv8tion.jda.api.entities.Guild)7 List (java.util.List)6 CommandListUpdateAction (net.dv8tion.jda.api.requests.restaction.CommandListUpdateAction)6 IOException (java.io.IOException)4 Collectors (java.util.stream.Collectors)4 JDA (net.dv8tion.jda.api.JDA)4 MessageContextInteractionEvent (net.dv8tion.jda.api.events.interaction.command.MessageContextInteractionEvent)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 ElementTag (com.denizenscript.denizencore.objects.core.ElementTag)3 ListTag (com.denizenscript.denizencore.objects.core.ListTag)3 java.util (java.util)3 Member (net.dv8tion.jda.api.entities.Member)3