Search in sources :

Example 1 with CommandListUpdateAction

use of net.dv8tion.jda.api.requests.restaction.CommandListUpdateAction in project Bean by Xirado.

the class InteractionCommandHandler method updateCommands.

public void updateCommands(Consumer<List<Command>> success, Consumer<Throwable> failure) {
    if (!Bean.getInstance().isDebug()) {
        commandUpdateAction.queue(success, failure);
        for (Map.Entry<Long, List<GenericCommand>> entrySet : registeredGuildCommands.entrySet()) {
            Long guildID = entrySet.getKey();
            List<GenericCommand> slashCommands = entrySet.getValue();
            if (guildID == null || slashCommands == null)
                continue;
            if (slashCommands.isEmpty())
                continue;
            Guild guild = Bean.getInstance().getShardManager().getGuildById(guildID);
            if (guild == null)
                continue;
            CommandListUpdateAction guildCommandUpdateAction = guild.updateCommands();
            for (GenericCommand cmd : slashCommands) {
                guildCommandUpdateAction = guildCommandUpdateAction.addCommands(cmd.getData());
            }
            if (slashCommands.size() > 0)
                guildCommandUpdateAction.queue();
        }
    } else {
        List<GenericCommand> commands = registeredGuildCommands.get(Bean.TEST_SERVER_ID);
        if (commands != null && !commands.isEmpty()) {
            Guild guild = Bean.getInstance().getShardManager().getGuildById(Bean.TEST_SERVER_ID);
            if (guild == null)
                return;
            CommandListUpdateAction commandListUpdateAction = guild.updateCommands();
            for (GenericCommand cmd : commands) commandListUpdateAction.addCommands(cmd.getData());
            commandListUpdateAction.queue(success, failure);
        }
    }
}
Also used : GenericCommand(at.xirado.bean.command.GenericCommand) CommandListUpdateAction(net.dv8tion.jda.api.requests.restaction.CommandListUpdateAction) ArrayList(java.util.ArrayList) List(java.util.List) Guild(net.dv8tion.jda.api.entities.Guild) Map(java.util.Map) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap)

Example 2 with CommandListUpdateAction

use of net.dv8tion.jda.api.requests.restaction.CommandListUpdateAction in project HangmanDiscordBot by megoRU.

the class BotStartConfig method updateSlashCommands.

private void updateSlashCommands(boolean isUpdateInGuilds) {
    try {
        if (isUpdateInGuilds) {
            for (int i = 0; i < jda.getGuilds().size(); i++) {
                jda.getGuilds().get(i).updateCommands().queue();
            }
        } else {
            CommandListUpdateAction commands = jda.updateCommands();
            List<OptionData> options = new ArrayList<>();
            options.add(new OptionData(STRING, "game", "Setting the Game language").addChoice("eng", "eng").addChoice("rus", "rus").setRequired(true));
            options.add(new OptionData(STRING, "bot", "Setting the bot language").addChoice("eng", "eng").addChoice("rus", "rus").setRequired(true));
            commands.addCommands(Commands.slash("language", "Setting language").addOptions(options));
            commands.addCommands(Commands.slash("hg", "Start the game"));
            commands.addCommands(Commands.slash("stop", "Stop the game"));
            commands.addCommands(Commands.slash("help", "Bot commands"));
            commands.addCommands(Commands.slash("stats", "Get your statistics"));
            commands.addCommands(Commands.slash("mystats", "Find out the number of your wins and losses"));
            commands.addCommands(Commands.slash("allstats", "Find out the statistics of all the bot's games"));
            commands.addCommands(Commands.slash("delete", "Deleting your data"));
            commands.queue();
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}
Also used : OptionData(net.dv8tion.jda.api.interactions.commands.build.OptionData) CommandListUpdateAction(net.dv8tion.jda.api.requests.restaction.CommandListUpdateAction)

Example 3 with CommandListUpdateAction

use of net.dv8tion.jda.api.requests.restaction.CommandListUpdateAction in project Ree6 by Ree6-Applications.

the class CommandManager method addSlashCommand.

/**
 * Method used to add all Commands as SlashCommand on Discord.
 */
public void addSlashCommand() {
    CommandListUpdateAction listUpdateAction = BotInfo.botInstance.updateCommands();
    for (Command command : getCommands()) {
        if (command.getCategory() == Category.HIDDEN || command.getCommandData() == null)
            continue;
        // noinspection ResultOfMethodCallIgnored
        listUpdateAction.addCommands(command.getCommandData());
    }
    listUpdateAction.queue();
}
Also used : CommandListUpdateAction(net.dv8tion.jda.api.requests.restaction.CommandListUpdateAction)

Example 4 with CommandListUpdateAction

use of net.dv8tion.jda.api.requests.restaction.CommandListUpdateAction 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)

Example 5 with CommandListUpdateAction

use of net.dv8tion.jda.api.requests.restaction.CommandListUpdateAction in project BotCommands by freya022.

the class ApplicationCommandsUpdater method updateCommands.

@Blocking
public void updateCommands() {
    final CommandListUpdateAction updateAction = guild != null ? guild.updateCommands() : context.getJDA().updateCommands();
    final List<Command> commands = updateAction.addCommands(allCommandData).complete();
    updatedCommands = true;
    if (guild != null) {
        thenAcceptGuild(commands, guild);
    } else {
        thenAcceptGlobal(commands);
    }
}
Also used : Command(net.dv8tion.jda.api.interactions.commands.Command) CommandListUpdateAction(net.dv8tion.jda.api.requests.restaction.CommandListUpdateAction) Blocking(org.jetbrains.annotations.Blocking)

Aggregations

CommandListUpdateAction (net.dv8tion.jda.api.requests.restaction.CommandListUpdateAction)11 ArrayList (java.util.ArrayList)4 List (java.util.List)4 Guild (net.dv8tion.jda.api.entities.Guild)4 CommandData (net.dv8tion.jda.api.interactions.commands.build.CommandData)4 OptionData (net.dv8tion.jda.api.interactions.commands.build.OptionData)4 JDA (net.dv8tion.jda.api.JDA)3 Arrays (java.util.Arrays)2 Map (java.util.Map)2 Collectors (java.util.stream.Collectors)2 Command (net.dv8tion.jda.api.interactions.commands.Command)2 Commands (net.dv8tion.jda.api.interactions.commands.build.Commands)2 GenericCommand (at.xirado.bean.command.GenericCommand)1 AbstractIdleService (com.google.common.util.concurrent.AbstractIdleService)1 Color (java.awt.Color)1 FileInputStream (java.io.FileInputStream)1 IOException (java.io.IOException)1 PrintWriter (java.io.PrintWriter)1 StringWriter (java.io.StringWriter)1 InvocationTargetException (java.lang.reflect.InvocationTargetException)1