getOptions method Null safety

  1. @override
Iterable<CommandOptionBuilder> getOptions(
  1. CommandsPlugin commands
)
override

Build the options for registering this group to the Discord API for slash commands.

Implementation

@override
Iterable<CommandOptionBuilder> getOptions(CommandsPlugin commands) {
  if (type != CommandType.textOnly) {
    List<CommandOptionBuilder> options = [];

    for (final mirror in _arguments) {
      Iterable<Name> names = mirror.metadata
          .where((element) => element.reflectee is Name)
          .map((nameMirror) => nameMirror.reflectee)
          .cast<Name>();

      String name;
      if (names.isNotEmpty) {
        name = names.first.name;
      } else {
        String rawArgumentName = MirrorSystem.getName(mirror.simpleName);

        name = convertToKebabCase(rawArgumentName);
      }

      Converter<dynamic>? argumentConverter = _mappedConverterOverrides[name]?.converter ??
          commands.converterFor(mirror.type.reflectedType);

      Iterable<ArgChoiceBuilder>? choices = _mappedChoices[name]?.builders;

      choices ??= argumentConverter?.choices;

      options.add(CommandOptionBuilder(
        argumentConverter?.type ??
            discordTypes[mirror.type.reflectedType] ??
            CommandOptionType.string,
        name,
        _mappedDescriptions[name]!.value,
        required: !mirror.isOptional,
        choices: choices?.toList(),
      ));
    }

    return options;
  } else {
    // Text-only commands might have children which are slash commands
    return super.getOptions(commands);
  }
}