Skip to content
GitHub

createMetadata

function in meocord/common Since 4.1.0

createMetadata<T>(description?: string): MetadataDecorator<T>

Creates a typed decorator for facts about a handler that guards and other stages read.

Use it for what a handler requires, such as the roles a command needs, which any guard can read. To configure one use of a guard instead, give it { provide, params }.

Parameters

NameTypeDescription
description?string

A name for the key, shown when the key is logged.

Returns

MetadataDecorator<T>

A decorator that attaches its value, and the key stages read it by.

Examples

TypeScript
export const Roles = createMetadata<string[]>('roles')

@Guard()
export class RolesGuard implements GuardInterface {
  constructor(private readonly context: ExecutionContext) {}

  canActivate(interaction: ChatInputCommandInteraction): boolean {
    const roles = interaction.inCachedGuild() ? interaction.member.roles.cache : undefined
    return (this.context.get(Roles) ?? []).every(id => roles?.has(id))
  }
}
// On a handler or a controller: @Roles(['123456789012345678'])