Skip to content
GitHub

SetMetadata

function in meocord/common Since 4.0.0 Deprecated

Since 4.1, and removed in the next major version (5.0). Use createMetadata instead. Its decorator is typed, and its key cannot collide with another.

SetMetadata<V = any>(
  metadataKey: string,
  metadataValue: V,
): ClassDecorator & MethodDecorator

Attaches 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

NameTypeDescription
metadataKeystring

The key to store the value under.

metadataValueV

The value to store.

Returns

Returns a decorator for a class or a method.

ClassDecorator & MethodDecorator

A decorator for a class or a method.

Throws

  • Error when metadataKey begins with meocord:, 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

TypeScript
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')