Search in sources :

Example 1 with Commands

use of net.dv8tion.jda.api.interactions.commands.build.Commands 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

ArrayList (java.util.ArrayList)1 Collection (java.util.Collection)1 List (java.util.List)1 TimeUnit (java.util.concurrent.TimeUnit)1 BooleanSupplier (java.util.function.BooleanSupplier)1 Collectors (java.util.stream.Collectors)1 Stream (java.util.stream.Stream)1 Nonnull (javax.annotation.Nonnull)1 JDA (net.dv8tion.jda.api.JDA)1 Command (net.dv8tion.jda.api.interactions.commands.Command)1 CommandData (net.dv8tion.jda.api.interactions.commands.build.CommandData)1 Commands (net.dv8tion.jda.api.interactions.commands.build.Commands)1 Request (net.dv8tion.jda.api.requests.Request)1 Response (net.dv8tion.jda.api.requests.Response)1 CommandListUpdateAction (net.dv8tion.jda.api.requests.restaction.CommandListUpdateAction)1 DataArray (net.dv8tion.jda.api.utils.data.DataArray)1 GuildImpl (net.dv8tion.jda.internal.entities.GuildImpl)1 CommandImpl (net.dv8tion.jda.internal.interactions.command.CommandImpl)1 RestActionImpl (net.dv8tion.jda.internal.requests.RestActionImpl)1 Route (net.dv8tion.jda.internal.requests.Route)1