SetMetadata
function in meocord/common Since 4.0.0 Deprecated
Since 4.1, and removed in the next major version (5.0). Use
createMetadatainstead. Its decorator is typed, and its key cannot collide with another.
SetMetadata<V = any>(
metadataKey: string,
metadataValue: V,
): ClassDecorator & MethodDecoratorAttaches a value to a controller or a handler under a string key of your choosing.
A stage reads the value with ExecutionContext.get(key), the method's first, then the controller's. Using it
logs a warning once.
Parameters
| Name | Type | Description |
|---|---|---|
metadataKey | string | The key to store the value under. |
metadataValue | V | The value to store. |
Returns
Returns a decorator for a class or a method.
ClassDecorator & MethodDecoratorA decorator for a class or a method.
Throws
Error when
metadataKeybegins withmeocord:, where MeoCord keeps its own metadata, or is a key dependency injection reads, as the decorator applies: a value there would replace what the framework stores.
Examples
export const Roles = (...roles: string[]) => SetMetadata('roles', roles)
@Command('ban', CommandType.SLASH)
@Roles('admin', 'moderator')
async ban(interaction: ChatInputCommandInteraction) {
await respond(interaction).send('Banned.')
}
// In a guard that injects ExecutionContext: this.context.get<string[]>('roles')