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.

@FunctionalInterface public interface Middleware extends Consumer<InvocationContext<?>>

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 Type
    Method
    Description
    void
    Executes this middleware with the given InvocationContext.
    default @Nullable Collection<Class<?>>
    Declares the interaction controllers for which this middleware should run.

    Methods inherited from interface Consumer

    andThen