CooldownCheck class

A check that succeeds if a command is not on cooldown for a given context.

Every context can be sorted into "buckets", determined by type. See getKey for more information on how these buckets are created.

Each bucket is allowed to execute commands a certain number of times before being put on cooldown. This number is determined by tokensPer, and the cooldown for a bucket starts as soon as the bucket uses its first token. Once the cooldown is over, the number of tokens for that bucket is reset to tokensPer.

For example, a cooldown that puts individual users on cooldown for 2 minutes:

commands.check(CooldownCheck(
  CooldownType.user,
  Duration(minutes: 2),
));

commands.addCommand(ChatCommand(
  'test',
  'A test command',
  (IChatContext context) => context.respond(MessageBuilder.content('Hi there!')),
));

commands.onCommandError.listen((error) {
  if (error is CheckFailedException) {
    AbstractCheck failed = error.failed;

    if (failed is CooldownCheck) {
      error.context.respond(MessageBuilder.content(
        'Wait ${failed.remaining(error.context).inSeconds} '
        'seconds before using that command again!',
      ));
    }
  }
});

Notice the times at which the commands were executed, and that other users are not put on cooldown

You might also be interested in:

  • CooldownType, for determining how to sort contexts into buckets.
Inheritance

Constructors

CooldownCheck(Flags<CooldownType> type, Duration duration, {int tokensPer = 1, String? name})
Create a new CooldownCheck with a given type and duration.

Properties

allowsDm bool
Whether this check will allow commands to be executed in DM channels.
no setteroverride
duration Duration
The duration of the cooldown.
getter/setter pair
hashCode int
The hash code for this object.
no setterinherited
name String
The name of this check.
finalinherited
postCallHooks Iterable<void Function(CommandContext p1)>
An iterable of callbacks executed after a command is executed.
no setteroverride
preCallHooks Iterable<void Function(CommandContext)>
An iterable of callbacks executed before a command is executed but after all the checks for that command have succeeded.
getter/setter pairoverride-getter
requiredPermissions Flags<Permissions>?
The permissions required from members to pass this check.
no setteroverride
runtimeType Type
A representation of the runtime type of the object.
no setterinherited
tokensPer int
The number of times a bucket can execute commands before this check fails.
getter/setter pair
type Flags<CooldownType>
The cooldown type, used to sort contexts into buckets.
final

Methods

check(CommandContext context) FutureOr<bool>
Validate context against this check.
override
getKey(CommandContext context) int
Returns an ID that uniquely represents the bucket context was sorted on, based on type.
noSuchMethod(Invocation invocation) → dynamic
Invoked when a nonexistent method or property is accessed.
inherited
remaining(CommandContext context) Duration
Return the remaining cooldown time for a given context.
toString() String
A string representation of this object.
inherited

Operators

operator ==(Object other) bool
The equality operator.
inherited