-
-
Notifications
You must be signed in to change notification settings - Fork 1
Designing Commands
Caution
Before we dive in, when using Nexus, it's advised to avoid employing the
event.interaction.respondLater()methods provided by Javacord. This is due to the special handling of middleware in Nexus, which necessitates coordination between the command and middleware. Instead, it's recommended to useevent.respondLater()orevent.respondLaterAsEphemeral().An even more streamlined option is auto-deferring, which takes care of these scenarios for you. To learn more, refer to Auto-deferring Responses.
Designing commands within Nexus is straightforward and elegant. However, before we delve into command design, it's essential to grasp some foundational guidelines that Nexus enforces:
- Commands must have distinct names. If you need to reuse a name, you can modify one of the commands with the
@IdentifiableAsannotation for indexing purposes. - You must implement the
NexusHandlerinterface for the command to be recognized by the engine. - Each command must include a name and description field, as Discord requires.
With these principles in mind, you can begin crafting a command by creating a class that implements the NexusHandler interface:
object PingCommand: NexusHandler {
override fun onEvent(event: NexusCommandEvent) {
// Command logic will be added here
}
}However, this alone won't suffice. To make it a recognized command, you need to provide the name and description fields:
object PingCommand: NexusHandler {
val name: String = "ping"
val description: String = "Ping, Pong!"
override fun onEvent(event: NexusCommandEvent) {
// Command logic will be added here
}
}This simple structure creates a basic command. Let's delve deeper and add some functionality:
object PingCommand: NexusHandler {
val name: String = "ping"
val description: String = "Ping, Pong!"
override fun onEvent(event: NexusCommandEvent) {
val server = event.server.orElseThrow()
// Nexus provides two response options: auto-deferring and manual response.
// The example here demonstrates auto-deferred responses.
event.autoDefer(ephemeral = false) {
return@autoDefer NexusMessage.with {
setContent("Hello ${server.name}")
}
}
// Alternatively, you can manually respond like this:
// event.respondNowWith("Hello ${server.name}!")
}
}At this point, the command responds with a simple message like "Hello {server}!" However, there's a challenge: we can't ensure that it's executed within a server context. Not to worry, Nexus has a solution in the form of middlewares and afterwares, concepts commonly found in many web frameworks.
Learn more about different features discussed here:
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: