Middlewares¶
Middlewares run just before an interaction event gets dispatched. They are used to perform additional checks or add more info the InvocationContext. Middlewares are intended to provide a flexible system for extending the execution chain.
They are executed based on their Priority in the following order:
PERMISSIONS: Middlewares with this priority will always be executed firstHIGH: Highest priority for custom implementations, will be executed right after internal middlewaresNORMAL: Default priorityLOW: Lowest priority, will be executed at the end
If one middleware fails, the entire interaction execution gets immediately aborted and no more middlewares will be executed.
Default Middlewares¶
JDA-Commands uses its own Middleware API internally to implement some features. All these features can either be extended or replaced by the user. You can either register your own implementations at the respective builder method or use the @Implementation annotation.
Note
Using the @Implementation annotation requires the guice integration (shipped by default). You can read more about it here.
Middlewares provided by JDA-Commands include:
Writing own Middlewares¶
You can write your own middlewares by implementing the Middleware interface. You can cancel an execution by calling InvocationContext#cancel(MessageCreateData).
Example
Then, either register your Middleware at the builder:
JDACommands.builder(jda, Main.class)
.middleware(Priority.NORMAL, new LoggingMiddleware());
.start();
or use the @Implementation annotation:
@Implementation.Middleware(priority = Priority.NORMAL)
public class LoggingMiddleware implements Middleware {...}
Run only for certain interaction controllers¶
If you want your Middleware to only run for certain interaction controllers, just implement Middleware#runFor() returning the classes of the interaction controllers for which the middleware should run.
!!! example (run only for HelloController.java) ```java @Middleware(priority = Priority.NORMAL) public class CustomMiddleware implements Middleware { private static final Logger log = LoggerFactory.getLogger(CustomMiddleware.class);
@Override
public void accept(InvocationContext<?> context) {
log.info("run custom middleware");
}
@Override
public Collection<Class<?>> runFor() {
return List.of(HelloController.class);
}
}
```