Skip to content

Commit

Permalink
feat(client): added option to set a default cooldown for all commands (
Browse files Browse the repository at this point in the history
…#294)

Co-authored-by: Kovacs Alex <[email protected]>
Co-authored-by: Jeroen Claassens <[email protected]>
  • Loading branch information
3 people authored Oct 16, 2021
1 parent 750c25c commit 7734d59
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 6 deletions.
16 changes: 15 additions & 1 deletion src/lib/SapphireClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { ArgumentStore } from './structures/ArgumentStore';
import { CommandStore } from './structures/CommandStore';
import { ListenerStore } from './structures/ListenerStore';
import { PreconditionStore } from './structures/PreconditionStore';
import { PluginHook } from './types/Enums';
import { BucketScope, PluginHook } from './types/Enums';
import { Events } from './types/Events';
import { ILogger, LogLevel } from './utils/logger/ILogger';
import { Logger } from './utils/logger/Logger';
Expand Down Expand Up @@ -114,6 +114,12 @@ export interface SapphireClientOptions {
* @default false
*/
typing?: boolean;

/**
* Sets the default cooldown time for all commands.
* @default "No cooldown options"
*/
defaultCooldown?: CooldownOptions;
}

/**
Expand Down Expand Up @@ -295,6 +301,14 @@ export interface ClientLoggerOptions {
instance?: ILogger;
}

export interface CooldownOptions {
scope?: BucketScope;
delay: number;
limit?: number;
filteredUsers?: Snowflake[];
filteredCommands?: string[];
}

declare module 'discord.js' {
interface Client {
id: Snowflake | null;
Expand Down
14 changes: 9 additions & 5 deletions src/lib/structures/Command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -231,14 +231,18 @@ export abstract class Command<T = Args> extends AliasPiece {
* @param options The command options given from the constructor.
*/
protected parseConstructorPreConditionsCooldown(options: CommandOptions) {
const limit = options.cooldownLimit ?? 1;
const delay = options.cooldownDelay ?? 0;
const filteredUsers = options.cooldownFilteredUsers;
const { cooldownLimit, cooldownDelay, cooldownScope, cooldownFilteredUsers } = options;
const { defaultCooldown } = this.container.client.options;

if (limit && delay) {
if ((defaultCooldown && !defaultCooldown.filteredCommands?.includes(this.name)) || (cooldownLimit && cooldownDelay)) {
this.preconditions.append({
name: CommandPreConditions.Cooldown,
context: { scope: options.cooldownScope ?? BucketScope.User, limit, delay, filteredUsers }
context: {
scope: cooldownScope ?? defaultCooldown?.scope ?? BucketScope.User,
limit: cooldownLimit ?? defaultCooldown?.limit ?? 1,
delay: cooldownDelay ?? defaultCooldown?.delay ?? 0,
filteredUsers: cooldownFilteredUsers ?? defaultCooldown?.filteredUsers
}
});
}
}
Expand Down

0 comments on commit 7734d59

Please sign in to comment.