Contexts
EnhancedJDA implements an option to quickly register & implement all contexts from certain packages. You should create a package (or multiple packages) with all contexts. They should extend EnhancedContext<T>
class (where T
is a User
or Message
). You also need to add @Context
annotation to class. To register them, use the PackageMapping
annotation in the main class.
dev.projectenhanced.enhancedjda.controller.PackageMapping
public @interface PackageMapping {
String[] commands() default {};
String[] contexts() default {};
String[] listeners() default {};
String[] tables() default {};
String[] persisters() default {};
}
dev.projectenhanced.enhancedjda.controller.command.annotation.Context
public @interface Context {
String name();
Command.Type type();
}
Example setup:
.
├── myContexts/
│ ├── ExampleContext.class
│ ├── AnotherContext.class
│ └── ...
└── Main.class
myContexts.ExampleContext
@Context(type = Command.Type.USER, name = "Get user's avatar")
public class ExampleContext extends EnhancedContext<User> {
private final EnhancedBot bot;
public ExampleContext(EnhancedBot bot) {
super(bot);
this.bot = bot;
}
@Override
protected void execute(GenericContextInteractionEvent<User> event) {
event.reply("Avatar: " + event.getTarget().getEffectiveAvatarUrl()).queue();
}
}
Main
@PackageMapping(
contexts = {"myContexts"},
)
public class Main extends EnhancedBot {
// ...
}