Skip to content

Cooldown

Although you can implement your own cooldown logic, you might wanna try @slipher/cooldown first.

Installing...
npm add @slipher/cooldown

First you need to add cooldown property to client as shown below

import {
class Client<Ready extends boolean = boolean>
Client
, type
(alias) interface UsingClient
import UsingClient
UsingClient
} from "seyfert";
import {
class CooldownManager
CooldownManager
} from "@slipher/cooldown";
const
const client: UsingClient & Client<boolean>
client
= new
new Client<boolean>(options?: ClientOptions): Client<boolean>
Client
() as
(alias) interface UsingClient
import UsingClient
UsingClient
&
class Client<Ready extends boolean = boolean>
Client
;
const client: UsingClient & Client<boolean>
client
.
function start(options?: Pick<DeepPartial<StartOptions>, "langsDir" | "commandsDir" | "connection" | "token" | "componentsDir">): Promise<void> (+1 overload)
start
().
Promise<void>.then<void, never>(onfulfilled?: ((value: void) => void | PromiseLike<void>) | null | undefined, onrejected?: ((reason: any) => PromiseLike<never>) | null | undefined): Promise<...>

Attaches callbacks for the resolution and/or rejection of the Promise.

@paramonfulfilled The callback to execute when the Promise is resolved.

@paramonrejected The callback to execute when the Promise is rejected.

@returnsA Promise for the completion of which ever callback is executed.

then
(() => {
const client: UsingClient & Client<boolean>
client
.
UsingClient.cooldown: CooldownManager
cooldown
= new
new CooldownManager(client: BaseClient): CooldownManager
CooldownManager
(
const client: UsingClient & Client<boolean>
client
);
})
declare module "seyfert" {
interface
interface UsingClient
UsingClient
{
UsingClient.cooldown: CooldownManager
cooldown
:
class CooldownManager
CooldownManager
;
}
}

And then we can use @Cooldown() decorator in our commands, it takes 3 arguments:

PropertyTypeDescription
typeCooldownTypetarget type, can be user, guild or channel
intervalnumbertime to refresh uses
usesnumberhow 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.

@paramtimestamp The timestamp to format.

@paramstyle The style of the timestamp. Defaults to 't'.

@returnsThe formatted timestamp.

timestamp
(new
var Date: DateConstructor
new (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
();
});