Cooldown
Although you can implement your own cooldown logic, you might wanna try @slipher/cooldown
first.
First you need to add cooldown
property to client
as shown below
And then we can use @Cooldown()
decorator in our commands, it takes 3 arguments:
Property | Type | Description |
---|---|---|
type | CooldownType | target type, can be user, guild or channel |
interval | number | time to refresh uses |
uses | number | how many uses user can use this command in the given interval |
import { function Declare(declare: CommandDeclareOptions): <T extends { new (...args: any[]): object;}>(target: T) => { new (...args: any[]): { name: string; nsfw: boolean | undefined; props: ExtraProps | undefined; contexts: InteractionContextType[]; integrationTypes: ApplicationIntegrationType[]; defaultMemberPermissions: bigint | undefined; botPermissions: bigint | undefined; description: string; type: ApplicationCommandType; guildId?: string[]; ignore?: IgnoreCommand; aliases?: string[]; handler?: EntryPointCommandHandlerType; };} & T
Declare, class Command
Command, type class CommandContext<T extends OptionsRecord = {}, M extends keyof RegisteredMiddlewares = never>interface CommandContext<T extends OptionsRecord = {}, M extends keyof RegisteredMiddlewares = never>
CommandContext } from "seyfert";import { enum CooldownType
CooldownType, function Cooldown(props: CooldownProps): <T extends { new (...args: any[]): {};}>(target: T) => { new (...args: any[]): { cooldown: CooldownProps; };} & T
Cooldown } from "@slipher/cooldown";
@function Declare(declare: CommandDeclareOptions): <T extends { new (...args: any[]): object;}>(target: T) => { new (...args: any[]): { name: string; nsfw: boolean | undefined; props: ExtraProps | undefined; contexts: InteractionContextType[]; integrationTypes: ApplicationIntegrationType[]; defaultMemberPermissions: bigint | undefined; botPermissions: bigint | undefined; description: string; type: ApplicationCommandType; guildId?: string[]; ignore?: IgnoreCommand; aliases?: string[]; handler?: EntryPointCommandHandlerType; };} & T
Declare({ name: string
name: 'cool', description: string
description: 'Reference command'})@function Cooldown(props: CooldownProps): <T extends { new (...args: any[]): {};}>(target: T) => { new (...args: any[]): { cooldown: CooldownProps; };} & T
Cooldown({ CooldownProps.type: "user" | "guild" | "channel"
target type
type: enum CooldownType
CooldownType.function (enum member) CooldownType.User = "user"
User, CooldownProps.interval: number
interval in ms
interval: 1000 * 60, CooldownProps.uses: UsesProps
refill amount
uses: { UsesProps.default: number
default: 2 },})export default class class Cool
Cool extends class Command
Command { async Cool.run(ctx: CommandContext): Promise<void>
run(ctx: CommandContext<{}, never>
ctx: class CommandContext<T extends OptionsRecord = {}, M extends keyof RegisteredMiddlewares = never>interface CommandContext<T extends OptionsRecord = {}, M extends keyof RegisteredMiddlewares = never>
CommandContext) { await ctx: CommandContext<{}, never>
ctx.CommandContext<{}, never>.write<false>(body: InteractionCreateBodyRequest, withResponse?: false | undefined): Promise<void | WebhookMessage>
write({ content?: string | undefined
The message contents (up to 2000 characters)
content: `A cool command` }); }
Cool.onMiddlewaresError(context: CommandContext, error: string): void
onMiddlewaresError(context: CommandContext<{}, never>
context: class CommandContext<T extends OptionsRecord = {}, M extends keyof RegisteredMiddlewares = never>interface CommandContext<T extends OptionsRecord = {}, M extends keyof RegisteredMiddlewares = never>
CommandContext, error: string
error: string) { context: CommandContext<{}, never>
context.CommandContext<{}, never>.editOrReply<false>(body: InteractionCreateBodyRequest | InteractionMessageUpdateBodyRequest, withResponse?: false | undefined): Promise<...>
editOrReply({ content: string
content: error: string
error }) }}
And then we need to create a middleware for handle when the user is in cooldown:
import { function createMiddleware<T = any, C extends AnyContext = AnyContext>(data: MiddlewareContext<T, C>): MiddlewareContext<T, C>
createMiddleware, const Formatter: { codeBlock(content: string, language?: string): string; inlineCode(content: string): `\`${string}\``; bold(content: string): `**${string}**`; italic(content: string): `*${string}*`; ... 15 more ...; generateOAuth2URL(applicationId: string, { scopes, permissions, disableGuildSelect }: OAuth2URLOptions): string;}
Represents a formatter utility for formatting content.
Formatter } from 'seyfert';import { enum TimestampStyle
Represents timestamp styles.
TimestampStyle } from 'seyfert/lib/common';
export default createMiddleware<void, AnyContext>(data: MiddlewareContext<void, AnyContext>): MiddlewareContext<void, AnyContext>
createMiddleware<void>(async ({ context: AnyContext
context, next: () => void
next, stop: StopFunction
stop }) => { const const inCooldown: ReturnCache<number | true>
inCooldown = context: AnyContext
context.client: UsingClient
client.UsingClient.cooldown: CooldownManager
cooldown.CooldownManager.context(context: AnyContext, use?: keyof UsesProps, guildId?: string): ReturnCache<number | true>
context(context: AnyContext
context);
typeof const inCooldown: ReturnCache<number | true>
inCooldown === 'number' ? stop: (error: string) => void
stop( `You're in cooldown, try again ${const Formatter: { codeBlock(content: string, language?: string): string; inlineCode(content: string): `\`${string}\``; bold(content: string): `**${string}**`; italic(content: string): `*${string}*`; ... 15 more ...; generateOAuth2URL(applicationId: string, { scopes, permissions, disableGuildSelect }: OAuth2URLOptions): string;}
Represents a formatter utility for formatting content.
Formatter.function timestamp(timestamp: Date, style?: TimestampStyle): `<t:${number}:t>` | `<t:${number}:T>` | `<t:${number}:d>` | `<t:${number}:D>` | `<t:${number}:f>` | `<t:${number}:F>` | `<t:${number}:R>`
Formats the given timestamp into discord unix timestamp format.
timestamp(new var Date: DateConstructornew (value: number | string | Date) => Date (+3 overloads)
Date(var Date: DateConstructor
Enables basic storage and retrieval of dates and times.
Date.DateConstructor.now(): number
Returns the number of milliseconds elapsed since midnight, January 1, 1970 Universal Coordinated Time (UTC).
now() + const inCooldown: number
inCooldown), enum TimestampStyle
Represents timestamp styles.
TimestampStyle.function (enum member) TimestampStyle.RelativeTime = "R"
Represents a relative time style.
RelativeTime)}`, ) : next: () => void
next();});