Skip to content

Discord Context Parameter Decorators

This file provides a set of parameter decorators for Discord controller methods in the @sodacore/core Discord integration. These decorators allow you to inject specific Discord context objects (such as interaction, user, guild, channel, client, query, options, and fields) directly into your handler method parameters by attaching metadata for later resolution.


Features

  • Context Injection: Injects Discord-specific objects (interaction, user, guild, channel, client, query, option, field) into controller method parameters.
  • Flexible Parameter Selection: Supports injecting entire objects or specific named options/fields.
  • Metadata Management: Stores parameter metadata for each method, enabling the framework to resolve and inject the correct values at runtime.

Usage

typescript
import {
	Interaction,
	User,
	Guild,
	Channel,
	Client,
	Query,
	Option,
	Field
} from './decorator/context';

class MyDiscordController {
	async handleInteraction(
		@Interaction() interaction,
		@User() user,
		@Guild() guild,
		@Channel() channel,
		@Client() client,
		@Query() query,
		@Option('role') roleOption,
		@Field('customField') customField
	) {
		// Your logic here
	}
}

API

Interaction(): ParameterDecorator

Injects the Discord interaction object into the parameter.

User(): ParameterDecorator

Injects the user object associated with the interaction.

Guild(): ParameterDecorator

Injects the guild object associated with the interaction.

Channel(): ParameterDecorator

Injects the channel object associated with the interaction.

Client(): ParameterDecorator

Injects the Discord client instance.

Query(): ParameterDecorator

Injects the query object (if applicable).

Option(name: string, sendAll = false): ParameterDecorator

Injects a specific option by name from the interaction.

  • name: The name of the option to inject.
  • sendAll: If true, injects all options.

Field(name: string): ParameterDecorator

Injects a specific field by name from the interaction.

  • name: The name of the field to inject.

How It Works

  • Each decorator retrieves or creates the method's parameter metadata array.
  • It pushes an object describing the parameter's index, type, and any additional options (name, format).
  • The metadata is stored using Utils.setMeta and is later used by the framework to resolve and inject the correct values when the controller method is called.

Notes

  • These decorators are intended for use within Discord controller classes.
  • The framework will automatically resolve and inject the correct context objects based on the metadata at runtime.
  • Useful for building clean, declarative Discord bot handlers.

Released under the Apache-2.0 License.