Check class Null safety

A simple, stateless check for commands.

See AbstractCheck for a description of what a check is.

A Check is a simple check with no state, which validates IContexts with a single callback. The check succeeds if the callback returns true and fails if the callback returns false.

For example, to only allow users with "evrything" in their name to execute a command:

Check check = Check(
  (context) => context.user.username.contains('evrything'),
);

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

commands.onCommandError.listen((error) {
  if (error is CheckFailedException) {
    error.context.respond(MessageBuilder.content("Sorry, you can't use that command!"));
  }
});

Since some checks are so common, nyxx_commands provides a set of in-built checks that also integrate with the Discord Slash Command Permissions API:

  • GuildCheck, for checking if a command was invoked in a specific guild.

You might also be interested in:

Inheritance
Implementers

Constructors

Check(FutureOr<bool> _check(IContext), [String name = 'Check', FutureOr<bool> allowsDm = true, FutureOr<int?> requiredPermissions])
Create a new Check.

Properties

allowsDm FutureOr<bool>
Whether this check will allow commands to be executed in DM channels.
final
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 context)>
An iterable of callbacks executed after a command is executed.
read-only, override
preCallHooks Iterable<void Function(IContext context)>
An iterable of callbacks executed before a command is executed but after all the checks for that command have succeeded.
read-only, override
requiredPermissions FutureOr<int?>
The permissions required from members to pass this check.
final
runtimeType Type
A representation of the runtime type of the object.
read-only, inherited

Methods

check(IContext context) FutureOr<bool>
Validate context against this check.
override
noSuchMethod(Invocation invocation) → dynamic
Invoked when a non-existent method or property is accessed.
inherited
toString() String
A string representation of this object.
inherited

Operators

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

Static Methods

all(Iterable<AbstractCheck> checks, [String? name]) AbstractCheck
Creates a check that succeeds if all of checks succeed.
any(Iterable<AbstractCheck> checks, [String? name]) AbstractCheck
Creates a check that succeeds if any of checks succeeds.
deny(AbstractCheck check, [String? name]) AbstractCheck
Creates a check that succeeds if check fails.