Skip to content

Commit 7734d59

Browse files
sawa-koalexthemasterfavna
authored
feat(client): added option to set a default cooldown for all commands (#294)
Co-authored-by: Kovacs Alex <[email protected]> Co-authored-by: Jeroen Claassens <[email protected]>
1 parent 750c25c commit 7734d59

File tree

2 files changed

+24
-6
lines changed

2 files changed

+24
-6
lines changed

src/lib/SapphireClient.ts

+15-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import { ArgumentStore } from './structures/ArgumentStore';
88
import { CommandStore } from './structures/CommandStore';
99
import { ListenerStore } from './structures/ListenerStore';
1010
import { PreconditionStore } from './structures/PreconditionStore';
11-
import { PluginHook } from './types/Enums';
11+
import { BucketScope, PluginHook } from './types/Enums';
1212
import { Events } from './types/Events';
1313
import { ILogger, LogLevel } from './utils/logger/ILogger';
1414
import { Logger } from './utils/logger/Logger';
@@ -114,6 +114,12 @@ export interface SapphireClientOptions {
114114
* @default false
115115
*/
116116
typing?: boolean;
117+
118+
/**
119+
* Sets the default cooldown time for all commands.
120+
* @default "No cooldown options"
121+
*/
122+
defaultCooldown?: CooldownOptions;
117123
}
118124

119125
/**
@@ -295,6 +301,14 @@ export interface ClientLoggerOptions {
295301
instance?: ILogger;
296302
}
297303

304+
export interface CooldownOptions {
305+
scope?: BucketScope;
306+
delay: number;
307+
limit?: number;
308+
filteredUsers?: Snowflake[];
309+
filteredCommands?: string[];
310+
}
311+
298312
declare module 'discord.js' {
299313
interface Client {
300314
id: Snowflake | null;

src/lib/structures/Command.ts

+9-5
Original file line numberDiff line numberDiff line change
@@ -231,14 +231,18 @@ export abstract class Command<T = Args> extends AliasPiece {
231231
* @param options The command options given from the constructor.
232232
*/
233233
protected parseConstructorPreConditionsCooldown(options: CommandOptions) {
234-
const limit = options.cooldownLimit ?? 1;
235-
const delay = options.cooldownDelay ?? 0;
236-
const filteredUsers = options.cooldownFilteredUsers;
234+
const { cooldownLimit, cooldownDelay, cooldownScope, cooldownFilteredUsers } = options;
235+
const { defaultCooldown } = this.container.client.options;
237236

238-
if (limit && delay) {
237+
if ((defaultCooldown && !defaultCooldown.filteredCommands?.includes(this.name)) || (cooldownLimit && cooldownDelay)) {
239238
this.preconditions.append({
240239
name: CommandPreConditions.Cooldown,
241-
context: { scope: options.cooldownScope ?? BucketScope.User, limit, delay, filteredUsers }
240+
context: {
241+
scope: cooldownScope ?? defaultCooldown?.scope ?? BucketScope.User,
242+
limit: cooldownLimit ?? defaultCooldown?.limit ?? 1,
243+
delay: cooldownDelay ?? defaultCooldown?.delay ?? 0,
244+
filteredUsers: cooldownFilteredUsers ?? defaultCooldown?.filteredUsers
245+
}
242246
});
243247
}
244248
}

0 commit comments

Comments
 (0)