Skip to content
GitHub

OwnerGuard

class in meocord/common Since 4.3.0

class OwnerGuard implements GuardInterface

Lets only the user whose id a component's customId carries use it.

Use it on a button, select menu or modal opened for one user, such as a confirmation, a paginator or a settings card, whose route carries that user's id: card/{ownerId:snowflake}. Anyone else who clicks is told privately that it is not theirs, and the handler does not run. On a select menu, their pick is also put back, so the menu shows what it did before they picked.

Where it runs

  • Guards — where @UseGuard applies it

Examples

TypeScript
@Command('card/{ownerId:snowflake}', CommandType.BUTTON)
@UseGuard(OwnerGuard)
async card(interaction: ButtonInteraction) {
  await respond(interaction).send('Your card.')
}

@Command('poll/{pollId}/{authorId:snowflake}', CommandType.BUTTON)
@UseGuard({ provide: OwnerGuard, params: { param: 'authorId', message: 'Only the author can close this poll.' } })
async close(interaction: ButtonInteraction) {
  await respond(interaction).send('Poll closed.')
}

Members

constructor

new OwnerGuard()

params

readonly params?: { param?: string; message?: string }

param, the route param holding the owner's id, ownerId unless given; message, what anyone else is told.

canActivate

canActivate(
  interaction: MessageComponentInteraction | ModalSubmitInteraction,
  route?: Readonly<Record<string, unknown>>,
): boolean

Lets the call through when the user who clicked or submitted is the one the route param names.

Parameters

NameType
interaction| MessageComponentInteraction | ModalSubmitInteraction

The component or modal interaction.

route?Readonly<Record<string, unknown>>

The params read from its customId.

Returns

boolean

true for the owner.

Throws

  • GuardDeniedError for anyone else.

See also