Skip to content

Discord Context Menu Decorators

This file defines two decorators, MessageContext and UserContext, for registering Discord context menu commands in the @sodacore/core Discord integration. These decorators attach metadata to class methods, enabling them to be recognized and registered as Discord context menu commands (either for messages or users).


Features

  • Message Context Menu Registration: Use @MessageContext to register a method as a Discord message context menu command.

  • User Context Menu Registration: Use @UserContext to register a method as a Discord user context menu command.

  • Metadata Management: Stores command metadata (type, unique name, builder, etc.) for later discovery and registration with Discord.


Usage

typescript
import { MessageContext, UserContext } from './decorator/add';

class MyDiscordController {
	@MessageContext('Pin Message')
	async pinMessage(ctx) {
		// Handle pinning a message
	}

	@UserContext('Ban User')
	async banUser(ctx) {
		// Handle banning a user
	}
}

API

MessageContext(name: string): MethodDecorator

Registers a method as a Discord message context menu command.

  • name: The unique name for the context menu command.
  • Returns: A method decorator.

UserContext(name: string): MethodDecorator

Registers a method as a Discord user context menu command.

  • name: The unique name for the context menu command.
  • Returns: A method decorator.

How It Works

  • Sets metadata on the target method for:
    • Command type (add.messagecontext or add.usercontext)
    • Unique command name
    • Discord command builder instance (from discord.js)
  • Updates the methods metadata array for the class, ensuring each context menu command is registered with its type, subtype, and unique name.

Notes

  • These decorators are intended for use within Discord controller classes.
  • The metadata is used by the framework to register and handle context menu commands with Discord's API.
  • Requires discord.js as a peer dependency.

Released under the Apache-2.0 License.