-
-
Notifications
You must be signed in to change notification settings - Fork 1
Inheritance
Nexus introduces an interesting form of command inheritance to address situations where you desire initial data or properties for commands, yet expect to modify these properties across multiple commands. This solution becomes particularly valuable when dealing with properties that will be adjusted frequently and applying these changes individually to multiple commands becomes cumbersome. This is where command inheritances come to play.
Incorporated in release 1.0.0-beta.02
via Pull Request #11
, command inheritances were introduced to facilitate property sharing among commands. Within this enhancement, two types of inheritances were introduced:
-
Global Inheritance: All commands derive these shared properties, suitable when anticipating future changes to properties like
enabledInDms
. -
Local Inheritance: Enabled through the
@Inherits
annotation, this feature leverages reflection to duplicate properties from the inherited class (modifiable).
It's important to note that both types of inheritances possess lower precedence compared to local properties. This ensures that all properties remain overridable. You can establish inheritances from existing commands or utilize custom objects containing desired properties. The recommended approach is crafting a parent object, like the example below, where all commands will have DMs disabled:
object GlobalCommandProperties {
val enabledInDms = false
}
You can then set up Nexus to adopt this global inheritance object:
Nexus.configure {
global.inheritance = GlobalCommandProperties
}
Consequently, all commands will inherit the enabledInDms
property. Notably, this inheritance mechanism supports @Share
properties. Any @Share
property within GlobalCommandProperties
will be inherited, allowing commands to share properties with interceptors.
To illustrate further, let's assume you wish a single command to inherit properties. You can employ the @Inherits
property as demonstrated below:
@Inherits(GlobalCommandProperties::class)
object SomeCommand {
val name = "ping"
val description = "Hello"
fun onEvent(event: NexusCommandEvent) {}
}
For a further look at the implementation or to explore the details of the pull request, refer to Pull Request #11: Global and Local Inheritance
.
Note!
Inheritance only works one-nest deep, which means that the parent itself does not support inheritance, which is why we recommend using a parent object instead, although Inheritance does support extending superclasses (e.g. abstract classes), so you can use that to your advantage.
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: