addCommand method

  1. @override
void addCommand(
  1. covariant CommandRegisterable<CommandContext> command
)
override

Add a command to this group.

A command can be added to a group at most once; trying to do so will result in a CommandsError being thrown.

Implementation

@override
void addCommand(CommandRegisterable<CommandContext> command) {
  if (_attachedClients.isNotEmpty && !_scheduledSync) {
    _scheduledSync = true;
    scheduleMicrotask(() {
      logger.warning(
        'Registering commands after bot is ready might trigger rate limits when syncing commands',
      );
      _attachedClients.forEach(_syncCommands);
      _scheduledSync = false;
    });
  }

  command.parent = this;

  command.onPreCall.listen(_onPreCallController.add);
  command.onPostCall.listen(_onPostCallController.add);

  if (command is ChatCommandComponent) {
    if (_chatCommands.containsKey(command.name)) {
      throw CommandRegistrationError('Command with name "${command.name}" already exists');
    }

    for (final alias in command.aliases) {
      if (_chatCommands.containsKey(alias)) {
        throw CommandRegistrationError('Command with alias "$alias" already exists');
      }
    }

    _chatCommands[command.name] = command;
    for (final alias in command.aliases) {
      _chatCommands[alias] = command;
    }

    for (final child in command.walkCommands()) {
      logger.info('Registered command "${child.fullName}"');
    }
  } else if (command is UserCommand) {
    if (_userCommands.containsKey(command.name)) {
      throw CommandRegistrationError('User Command with name "${command.name}" already exists');
    }

    _userCommands[command.name] = command;

    logger.info('Registered User Command "${command.name}"');
  } else if (command is MessageCommand) {
    if (_messageCommands.containsKey(command.name)) {
      throw CommandRegistrationError(
          'Message Command with name "${command.name}" already exists');
    }

    _messageCommands[command.name] = command;

    logger.info('Registered Message Command "${command.name}"');
  } else {
    logger.warning('Unknown command type "${command.runtimeType}"');
  }
}