CooldownCheck class Null safety

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(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.
read-only, override
duration Duration
The duration of the cooldown.
read / write
hashCode int
The hash code for this object.
read-only, inherited
name String
The name of this check.
final, inherited
permissions Future<Iterable<CommandPermissionBuilderAbstract>>
The set of Discord Slash Command Permissions this check represents.
@Deprecated('Use allowsDm and requiredPermissions instead'), final, inherited
postCallHooks Iterable<void Function(IContext p1)>
An iterable of callbacks executed after a command is executed.
read-only, override
preCallHooks Iterable<void Function(IContext)>
An iterable of callbacks executed before a command is executed but after all the checks for that command have succeeded.
read / write, override-getter
requiredPermissions int?
The permissions required from members to pass this check.
read-only, override
runtimeType Type
A representation of the runtime type of the object.
read-only, inherited
tokensPer int
The number of times a bucket can execute commands before this check fails.
read / write
type CooldownType
The cooldown type, used to sort contexts into buckets.
final

Methods

check(IContext context) FutureOr<bool>
Validate context against this check.
override
getKey(IContext context) int
Returns an ID that uniquely represents the bucket context was sorted on, based on type.
noSuchMethod(Invocation invocation) → dynamic
Invoked when a non-existent method or property is accessed.
inherited
remaining(IContext 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