-
-
Notifications
You must be signed in to change notification settings - Fork 1
Command Interceptors
Command interceptors, like middlewares and afterwares, can be established through two primary approaches:
- Creating an Interceptor Repository.
- Directly Registering Interceptors within the Framework.
Let's start by examining the second approach. Nexus aggregates all interceptors within a global container accessible by name. All interceptors are stored within NexusCommandInterceptor. You can add them using the following methods:
// Example One: Reducing code duplication
NexusCommandInterceptor.addMiddleware("nexus.auth.server") { event -> event.stopIf(event.server.isEmpty) }
// Example Two: Creating quick middlewares (not recommended)
// This uses UUID as a namespace instead of a user-defined one.
NexusCommandInterceptor.middleware { event -> event.stopIf(event.server.isEmpty) }Middlewares implement the NexusMiddleware interface, utilizing the NexusMiddlewareEvent, which contains more methods compared to the traditional NexusCommandEvent:
-
next: Instructs Nexus to proceed to the next middleware. Does not require being called as a middleware's response. -
stop: Directs Nexus to halt the command's execution, resulting in a "Interaction has failed" outcome in the user's Discord client. -
stop(NexusMessage): Halts the command's execution and sends a specified message. -
stopIf(boolean, NexusMessage?): Stops command execution if the boolean is true, sending a message if provided. -
stopIf(Predicate, NexusMessage?): Similar to the previous method but accepts a predicate.
Nexus introduces two primary methods for deferring responses in middlewares. However, neither method will automatically defer commands. Responsibility for using deferred responses in commands remains with you. Let's explore the two main ways to defer responses in middlewares:
You can manually defer middlewares by utilizing defer or deferEphemeral, followed by a response such as stop(NexusMessage). Here's an example of a middleware that defers:
NexusInterceptor.middleware { event ->
event.deferEphemeral().join()
event.stop(NexusMessage.from("I have deferred the response!"))
}Automatic deferring is a newer Nexus feature where deferring middlewares is automated. To use this, you need to enable it in the configuration:
Nexus.configuration.interceptors.autoDeferMiddlewareResponses = trueOnce enabled, middleware responses will automatically defer if their execution time exceeds 2.5 seconds, like this:
NexusInterceptor.middleware { event ->
Thread.sleep(3000)
event.stop(NexusMessage.from("I have deferred the response!"))
}You can further configure properties such as whether deferred responses should be ephemeral or when to automatically defer. Adjust these properties:
// Default values
Nexus.configuration.global.autoDeferAfterMilliseconds = 2350
Nexus.configuration.interceptors.autoDeferAsEphemeral = trueNote
As previously mentioned, you need to manually use deferred responses in commands after enabling this feature. Nexus will not automatically defer command responses. Utilize methods like
event.respondLater()orevent.respondLaterAsEphemeral()to handle these cases, or you can enable Nexus to auto-defer for you.For more details on auto-deferring responses, refer to Auto-deferring Responses.
All information in this wiki was written during v1.2.0, we recommend updating to that version if you are below, or creating an issue in GitHub, updating the wiki if the current information is missing, outdated or lacking.
To get started with Nexus, we recommend reading the following in chronological:
- Installation & Preparing Nexus
- Designing Commands
- Command Interceptors
- Additional Features (Subcommand Router, Option Validation)
- Context Menus
- Command Synchronization
You may want to read a specific part of handling command and middleware responses:
You can also read about additional features of Nexus:
- Subcommand Routing
- Option Validation
- Express Way (shard management)
- Inheritance
- Feather Pagination
- Nexus.R
You can read about synchronizing commands to Discord:
For more additional performance:
Additional configurations: