addCommand method Null safety

void addCommand(
  1. GroupMixin command
)

Add a child to this group.

If any of its name or aliases confict with already registered commands, a CommandRegistrationError is thrown.

If child already has a parent, an CommandRegistrationError is thrown.

Implementation

void addCommand(GroupMixin command) {
  if (childrenMap.containsKey(command.name)) {
    throw CommandRegistrationError(
        'Command with name "$fullName ${command.name}" already exists');
  }

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

  if (command._parent != null) {
    throw CommandRegistrationError('Cannot register command "${command.fullName}" again');
  }

  if (_parent != null) {
    _logger.warning('Registering commands to a group after it is registered might cause slash '
        'commands to have incomplete definitions');
  }

  command._parent = this;

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

  command.onPreCall.listen(preCallController.add);
  command.onPostCall.listen(postCallController.add);
}