addCommand method

  1. @override
void addCommand(
  1. CommandBuilder command
)
override

Implementation

@override
void addCommand(CommandBuilder command) {
  if (commands.contains(command)) {
    throw InvalidCommandException('Command $command already exists');
  }

  final name = switch (command) {
    final CommandDeclarationBuilder command => command.name,
    final CommandDefinitionBuilder definition => definition.command.name,
    final UserCommandBuilder b => b.name,
    final MessageCommandBuilder b => b.name,
    final _ => throw InvalidCommandException('Unknown command type')
  };

  if (name == null) {
    throw MissingPropertyException('Command name is required');
  }

  final handlers = switch (command) {
    final CommandDeclarationBuilder command =>
      command.reduceHandlers(command.name!),
    final CommandDefinitionBuilder definition =>
      definition.command.reduceHandlers(definition.command.name!),
    final UserCommandBuilder b => [
        if (b.handle != null)
          CommandRegistration(
              name: b.name!, handler: b.handle!, declaredOptions: const [])
        else
          throw InvalidCommandException(
              'User command "${b.name}" has no handler')
      ],
    final MessageCommandBuilder b => [
        if (b.handle != null)
          CommandRegistration(
              name: b.name!, handler: b.handle!, declaredOptions: const [])
        else
          throw InvalidCommandException(
              'Message command "${b.name}" has no handler')
      ],
    final _ => throw InvalidCommandException('Unknown command type')
  };

  commands.add(command);
  commandsHandler.addAll(handlers);
}