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, option, or field) directly into your handler method parameters by attaching metadata for later resolution.
Features
- Context Injection: Injects Discord-specific objects (interaction, user, guild, channel, client) into controller method parameters.
- Query and Option Support: Allows injection of query parameters, command options, and custom fields.
- Metadata Management: Stores parameter metadata for each method, enabling the framework to resolve and inject the correct values at runtime.
Usage
import {
Interaction,
User,
Guild,
Channel,
Client,
Query,
Option,
Field
} from './decorator/context';
class MyDiscordController {
async handleCommand(
@Interaction() interaction,
@User() user,
@Guild() guild,
@Channel() channel,
@Client() client,
@Query() query,
@Option('optionName') optionValue,
@Field('fieldName') fieldValue
) {
// Your logic here
}
}
API
Interaction(): ParameterDecorator
Injects the Discord interaction object into the parameter.
User(): ParameterDecorator
Injects the Discord user object into the parameter.
Guild(): ParameterDecorator
Injects the Discord guild object into the parameter.
Channel(): ParameterDecorator
Injects the Discord channel object into the parameter.
Client(): ParameterDecorator
Injects the Discord client object into the parameter.
Query(): ParameterDecorator
Injects the query parameters into the parameter.
Option(name: string, sendAll = false): ParameterDecorator
Injects a specific command option value (by name) into the parameter.
- name: The name of the option to inject.
- sendAll: If
true
, injects all options as an object.
Field(name: string): ParameterDecorator
Injects a custom field value (by name) into the parameter.
How It Works
- Each decorator retrieves or creates the method's parameter metadata array.
- It pushes an object describing the parameter's index and type (and name/format if applicable).
- 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 command handlers.