addCommand method Null safety

  1. @override
void addCommand(
  1. ICommandRegisterable<IChatContext> command
)
inherited

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(ICommandRegisterable<IChatContext> command) {
  if (command is! IChatCommandComponent) {
    throw CommandsError(
        'All child commands of chat groups or commands must implement IChatCommandComponent');
  }

  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 (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(_onPreCallController.add);
  command.onPostCall.listen(_onPostCallController.add);
}