Skip to content

Commit

Permalink
fix: register cooldown correctly (#304)
Browse files Browse the repository at this point in the history
Co-authored-by: Jeroen Claassens <support@favware.tech>
  • Loading branch information
kyranet and favna committed Oct 17, 2021
1 parent 873c2d5 commit 44bf46e
Showing 1 changed file with 11 additions and 8 deletions.
19 changes: 11 additions & 8 deletions src/lib/structures/Command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -231,18 +231,21 @@ export abstract class Command<T = Args, O extends CommandOptions = CommandOption
* @param options The command options given from the constructor.
*/
protected parseConstructorPreConditionsCooldown(options: CommandOptions) {
const { cooldownLimit, cooldownDelay, cooldownScope, cooldownFilteredUsers } = options;
const { defaultCooldown } = this.container.client.options;

if ((defaultCooldown && !defaultCooldown.filteredCommands?.includes(this.name)) || (cooldownLimit && cooldownDelay)) {
// We will check for whether the command is filtered from the defaults, but we will allow overridden values to
// be set. If an overridden value is passed, it will have priority. Otherwise it will default to 0 if filtered
// (causing the precondition to not be registered) or the default value with a fallback to a single-use cooldown.
const filtered = defaultCooldown?.filteredCommands?.includes(this.name) ?? false;
const limit = options.cooldownLimit ?? (filtered ? 0 : defaultCooldown!.limit ?? 1);
const delay = options.cooldownDelay ?? (filtered ? 0 : defaultCooldown!.delay ?? 0);

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

0 comments on commit 44bf46e

Please sign in to comment.