Interface Middleware
- All Superinterfaces:
Consumer<InvocationContext<?>>
- All Known Implementing Classes:
ConstraintMiddleware, PermissionsMiddleware
- Functional Interface:
- This is a functional interface and can therefore be used as the assignment target for a lambda expression or method reference.
Middlewares run just before an interaction event gets dispatched. They are used to perform additional checks or add
more info to the InvocationContext. Middlewares can have different Priorities dictating their priority
on execution.
Register them at the JDACBuilder.middleware(Priority, Middleware) or use the
@Implementation.Validator
annotation of the guice extension.
If you want a Middleware to only run for certain interaction controllers, just implement runFor()
returning the classes of the interaction controllers for which the middleware should run.
Example
@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");
}
}
Example (run only for specific interaction controller)
@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);
}
}
-
Method Summary
Modifier and TypeMethodDescriptionvoidaccept(InvocationContext<?> context) Executes this middleware with the givenInvocationContext.default @Nullable Collection<Class<?>> runFor()Declares the interaction controllers for which this middleware should run.
-
Method Details
-
accept
Executes this middleware with the givenInvocationContext. UseInvocationContext.cancel(MessageCreateData)to cancel the execution chain.- Specified by:
acceptin interfaceConsumer<InvocationContext<?>>- Parameters:
context- theInvocationContextof the current interaction event
-
runFor
Declares the interaction controllers for which this middleware should run.
If this method returns
null, then thisMiddlewareruns for all interaction controllers.- Returns:
- the classes of the interaction controllers or null (run for all interaction controllers)
-