Skip to content

Inheritance

Miu edited this page Sep 28, 2023 · 2 revisions

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.

To get started with Nexus, we recommend reading the following in chronological:

  1. Installation & Preparing Nexus
  2. Designing Commands
  3. Command Interceptors
  4. Additional Features (Subcommand Router, Option Validation)
  5. Context Menus
  6. 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:

You can read about synchronizing commands to Discord:

For more additional performance:

Additional configurations:

Clone this wiki locally