Skip to content

Commit 8261770

Browse files
authored
feat(cooldown): add cooldownFilteredUsers to exempt users from the cooldown precondition (#249)
1 parent 21c768d commit 8261770

File tree

3 files changed

+19
-9
lines changed

3 files changed

+19
-9
lines changed

src/lib/structures/Command.ts

+12-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { AliasPiece, AliasPieceOptions, PieceContext } from '@sapphire/pieces';
22
import { Awaited, isNullish } from '@sapphire/utilities';
3-
import { Message, PermissionResolvable, Permissions } from 'discord.js';
3+
import { Message, PermissionResolvable, Permissions, Snowflake } from 'discord.js';
44
import * as Lexure from 'lexure';
55
import { Args } from '../parsers/Args';
66
import { BucketScope } from '../types/Enums';
@@ -154,10 +154,12 @@ export abstract class Command<T = Args> extends AliasPiece {
154154
protected parseConstructorPreConditionsCooldown(options: CommandOptions) {
155155
const limit = options.cooldownLimit ?? 1;
156156
const delay = options.cooldownDelay ?? 0;
157+
const filteredUsers = options.cooldownFilteredUsers;
158+
157159
if (limit && delay) {
158160
this.preconditions.append({
159161
name: CommandPreConditions.Cooldown,
160-
context: { scope: options.cooldownScope ?? BucketScope.User, limit, delay }
162+
context: { scope: options.cooldownScope ?? BucketScope.User, limit, delay, filteredUsers }
161163
});
162164
}
163165
}
@@ -344,6 +346,14 @@ export interface CommandOptions extends AliasPieceOptions, FlagStrategyOptions {
344346
*/
345347
cooldownScope?: BucketScope;
346348

349+
/**
350+
* The users that are exempt from the Cooldown precondition.
351+
* Use this to filter out someone like a bot owner
352+
* @since 2.0.0
353+
* @default undefined
354+
*/
355+
cooldownFilteredUsers?: Snowflake[];
356+
347357
/**
348358
* The required permissions for the client.
349359
* @since 2.0.0

src/lib/structures/Precondition.ts

+2-6
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
import { Piece, PieceContext, PieceOptions } from '@sapphire/pieces';
22
import type { Awaited } from '@sapphire/utilities';
33
import type { Message, Permissions } from 'discord.js';
4+
import type { CooldownContext } from '../../preconditions/Cooldown';
45
import { PreconditionError } from '../errors/PreconditionError';
56
import type { UserError } from '../errors/UserError';
67
import { err, ok, Result } from '../parsers/Result';
7-
import type { BucketScope } from '../types/Enums';
88
import type { Command } from './Command';
99

1010
export type PreconditionResult = Awaited<Result<unknown, UserError>>;
@@ -81,11 +81,7 @@ export abstract class Precondition extends Piece {
8181
* ```
8282
*/
8383
export interface Preconditions {
84-
Cooldown: {
85-
scope?: BucketScope;
86-
delay: number;
87-
limit?: number;
88-
};
84+
Cooldown: CooldownContext;
8985
DMOnly: never;
9086
Enabled: never;
9187
GuildNewsOnly: never;

src/preconditions/Cooldown.ts

+5-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { RateLimitManager } from '@sapphire/ratelimits';
2-
import type { Message } from 'discord.js';
2+
import type { Message, Snowflake } from 'discord.js';
33
import { Identifiers } from '../lib/errors/Identifiers';
44
import type { Command } from '../lib/structures/Command';
55
import { Precondition, PreconditionContext } from '../lib/structures/Precondition';
@@ -9,6 +9,7 @@ export interface CooldownContext extends PreconditionContext {
99
scope?: BucketScope;
1010
delay: number;
1111
limit?: number;
12+
filteredUsers?: Snowflake[];
1213
}
1314

1415
export class CorePrecondition extends Precondition {
@@ -21,6 +22,9 @@ export class CorePrecondition extends Precondition {
2122
// If there is no delay (undefined, null, 0), return ok:
2223
if (!context.delay) return this.ok();
2324

25+
// If the user has provided any filtered users and the message author is in that array, return ok:
26+
if (context.filteredUsers?.includes(message.author.id)) return this.ok();
27+
2428
const ratelimit = this.getManager(command, context).acquire(this.getId(message, context));
2529
if (ratelimit.limited) {
2630
const remaining = ratelimit.remainingTime;

0 commit comments

Comments
 (0)