-
Notifications
You must be signed in to change notification settings - Fork 0
04) Configuring Commands
Aside from configuring the options by directly using the command builder, there are other options available.
Annotations can be added to a method or class to configure its commands. The order in which each annotation's configuration is applied to the object is indeterminable and should not be relied upon.
public class ExampleCommand {
@MainCommand("key")
@Name("test")
@Group("testing")
@Description("a test command")
public String execute() {
return "Success!";
}
}
You can create your own annotations that can be used to modify your commands
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface AcceptRoles {
long roleIds();
}
breadbotBuilder.putCommandModifier(AcceptRoles.class,
(acceptRoles, commandBuilder) -> commandBuilder.addPreprocessorPredicate("accept_roles",
(event) -> Arrays.stream(acceptRoles.roleIds())
.anyMatch(value -> value == event.getAuthorId()));
There is an annotation provided that can be used on static methods to configure your commands This annotation is given special treatment in that it is executed after all other annotation modifiers have been applied.
public class ExampleCommand {
@MainCommand
public String execute() {
return "Success!";
}
@ConfigureCommand("execute")
public static void anyArbitraryMethodName(CommandHandleBuilder command) {
command.setKeys("key")
.setName("test")
.setGroup("testing")
.setDescription("a test command");
}
}
If a string is not provided to @ConfigureCommand
, the annotated method will be applied to all commands generated from that class.
Aside from using provided annotations, you can create your own annotations.
For example, we will create a @WhitelistOnly
annotation that allows certain commands to only be executed by certain users.
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import com.github.breadmoirai.breadbot.framework.annotation.InheritedProperty;
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.METHOD, ElementType.Type})
@InheritedProperty
public @interface WhitelistOnly {
/**
* The name of the whitelist to check against.
*/
String listName();
}
The @InheritedProperty
indicates that the annotation can be applied to a class and if so, it will apply to all commands contained within that class. Note that the annotation will not apply to sub-commands if they are in a different class. If the annotation is repeated on a method or an inner class, that annotation will override the one on the outer class.
public class WowCommand {
@Command
@WhitelistOnly("beta-testers")
public void doingSomethingNewAndExciting(...) {...}
}
We assume that there is some data structure already in place that contains the whitelists which can be obtained via #getWhitelist(String whitelistName)
.
builder = new BreadBotBuilder();
builder.bindCommandModifier(WhitelistOnly.class, (annotation, command) -> {
WhiteList whitelist = getWhitelist(annotation.listName());
command.addPreprocessorPredicate("whitelist", event -> whitelist.contains(event.getAuthor()));
});
// Now all we need to do is just add the command
builder.addCommand(WowCommand::new);