Skip to content

Context Menus

Miu edited this page Oct 3, 2023 · 2 revisions

Context menus are an incredible tool to quickly perform an action upon a message or a user, but for awhile, Nexus didn't support it and developers had their context menus overwritten by Nexus due to it using bulkOverwrite. Although that changes in the recent 1.0.0-next change which added context menus natively into Nexus like regular commands.

Unlike slash commands, Context menus do not support the following:

  1. Middlewares and afterwares: There is currently no common interface between user and message context menus, so we held off supporting this and the current middleware and afterware system is heavily based for slash commands, so changing it to support context menus requires more effort and many breaking changes.
  2. Auto-defer: Currently, auto-defer isn't supported, but this may change in the future versions.

Creating a context menu

To create a context menu, you can extend upon the NexusUserContextMenu or NexusMessageContextMenu abstract classes (we use abstract classes as the abstract classes implements necessary methods underneath):

object TestUserContextMenu: NexusUserContextMenu() {
    val name = "test"
    override fun onEvent(event: NexusContextMenuEvent<UserContextMenuCommandEvent, UserContextMenuInteraction>) {
        event.respondNowEphemerallyWith("Hello")
    }
}

object TestMessageContextMenu: NexusMessageContextMenu() {
    val name = "test"
    override fun onEvent(event: NexusContextMenuEvent<MessageContextMenuCommandEvent, MessageContextMenuInteraction>) {
        event.respondNowEphemerallyWith("Hello")
    }
}

A message and a user context menu can have the same name, and a context menu must have the name property otherwise the framework will complain when you try to add the context menu using the following method:

Nexus.contextMenus(TestUserContextMenu, TestMessageContextMenu)

After adding the context menus to the registry, everything should work (naturally, after synchronizing them using the same method in Command Synchronization).

For more information about what properties to override, refer to:

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