Cooldown
function in meocord/decorator Since 4.1.0
Cooldown(
options: CooldownOptions & { by?: undefined },
): ClassDecorator & MethodDecorator
Cooldown<P extends object = Record<string, unknown>>(
options: CooldownOptions<P> & { by: NonNullable<CooldownOptions<P>['by']> },
): CooldownByDecorator<P>Limits how often a handler runs, counted per user, server, channel or for everyone.
Use it to rate-limit a command, a component or a message command: an interaction over the limit is answered only
to its caller, with how long to wait, and a message command's is ignored, or told in a direct message with
@MeoCord({ messages: { dmOnCooldown: true } }). For a check that is not about how often, use a Guard.
Where it runs
- Cooldown check — for a cooldown without
by, before a message's params are fetched - Cooldowns — after validation and pipes
Parameters
| Name | Type | Default | Description |
|---|---|---|---|
options | CooldownOptions & { by?: undefined } | The limit, whose calls count together, and how to exempt or tell calls apart. | |
options.seconds | number | The window's length, in seconds: from | |
options.uses? | number | 1 | Calls allowed within the window. A deploy that changes it keeps the calls counted so far, held to the new
number; one that changes |
options.per? | CooldownScope | 'user' | Whose calls are counted together: |
options.bypass? | (
context: ExecutionContext,
) => boolean | Promise<boolean> | Exempts a call, such as one from an owner, without counting it. | |
options.by? | undefined | Counts calls apart by a value of the call, such as the account a button acts on, within the scope |
Returns
Returns a decorator for a class or a method.
ClassDecorator & MethodDecoratorThrows
Error when
secondsis not from0.001to4320000000000,usesis not a whole number of at least 1,peris not a scope orbyis not a function, as the decorator applies;CooldownErrorto a call over the limit.
Examples
@Command('daily', CommandType.SLASH)
@Cooldown({ seconds: 3 })
@Cooldown({ uses: 5, seconds: 60 })
async daily(interaction: ChatInputCommandInteraction) {
await respond(interaction).send('Here are your coins.')
}
@Command('check-in/{uid}', CommandType.BUTTON)
@Cooldown({ seconds: 3600, by: (_context, { uid }: { uid: string }) => uid })
async checkIn(interaction: ButtonInteraction, { uid }: { uid: string }) {
await respond(interaction).send(`Checked in ${uid}.`)
}Parameters
| Name | Type | Default | Description |
|---|---|---|---|
options | CooldownOptions<P> & {
by: NonNullable<CooldownOptions<P>['by']>
} | The limit, whose calls count together, and | |
options.seconds | number | The window's length, in seconds: from | |
options.uses? | number | 1 | Calls allowed within the window. A deploy that changes it keeps the calls counted so far, held to the new
number; one that changes |
options.per? | CooldownScope | 'user' | Whose calls are counted together: |
options.bypass? | (
context: ExecutionContext,
) => boolean | Promise<boolean> | Exempts a call, such as one from an owner, without counting it. | |
options.by | (
context: ExecutionContext,
params: P,
) =>
| CooldownKey
| undefined
| Promise<CooldownKey | undefined> | Counts calls apart by a value of the call, such as the account a button acts on, within the scope |
Returns
CooldownByDecorator<P>