Inheritance - ShindouMihou/Nexus GitHub Wiki
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.
Exploring Inheritances
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.