io.github.freya022.botcommands.api.commands.CommandList Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of BotCommands Show documentation
Show all versions of BotCommands Show documentation
A Kotlin-first (and Java) framework that makes creating Discord bots a piece of cake, using the JDA library.
package io.github.freya022.botcommands.api.commands;
import java.util.Collection;
import java.util.List;
import java.util.function.Predicate;
import java.util.stream.Collectors;
/**
* Represents a list of enabled commands, the enabled commands are only qualified by their base name.
*
Keep in mind you cannot disable global commands on a per-guild basis.
*/
public class CommandList {
private final Predicate filter;
private CommandList(Predicate filter) {
this.filter = filter;
}
/**
* Makes a list of usable commands (in a Guild context),
*
You have to insert full commands paths such as {@code name group subcommand}, which comes from the Discord representation of {@code /name group subcommand}
*
This is constructed by joining each path component with a space
*
Keep in mind you cannot enable global commands on a per-guild basis
*
* @param enabledCommandsStrs A collection of enabled command paths
* @return A {@link CommandList} of enabled command
*/
public static CommandList of(Collection enabledCommandsStrs) {
List enabledCommands = enabledCommandsStrs.stream().map(CommandPath::of).collect(Collectors.toList());
return new CommandList(path -> containsCommand(enabledCommands, path));
}
private static boolean containsCommand(List commandOrGroupPaths, CommandPath path) {
for (CommandPath commandOrGroupPath : commandOrGroupPaths) {
if (path.startsWith(commandOrGroupPath)) {
return true;
}
}
return false;
}
/**
* Makes a list of unusable commands (in a Guild context),
*
You have to insert full commands paths such as {@code name group subcommand}, which comes from the Discord representation of {@code /name group subcommand}
*
This is constructed by joining each path component with a space
*
Keep in mind you cannot disable global commands on a per-guild basis
*
* @param disabledCommandsStrs A collection of disabled command paths
* @return A {@link CommandList} of disabled command
*/
public static CommandList notOf(Collection disabledCommandsStrs) {
List disabledCommands = disabledCommandsStrs.stream().map(CommandPath::of).collect(Collectors.toList());
return new CommandList(path -> !containsCommand(disabledCommands, path));
}
/**
* Makes a list that disables all of this current guild's commands
*
* @return A {@link CommandList} with no command enabled
*/
public static CommandList none() {
return new CommandList(s -> false);
}
/**
* Makes a list that enables all of this current guild's commands
*
* @return A {@link CommandList} with all commands enabled
*/
public static CommandList all() {
return new CommandList(s -> true);
}
/**
* Makes a list that enables current guild's commands if they satisfy the given predicate
*
* @param predicate The CommandPath predicate, returns {@code true} if the command can be used
* @return The CommandList for this predicate
*/
public static CommandList filter(Predicate predicate) {
return new CommandList(predicate);
}
/**
* Return a predicate that returns true if the command can be used
*
* @return The filter predicate
*/
public Predicate getFilter() {
return filter;
}
}