CommandService constructor

CommandService(
  1. List<MineralCommandContract> commands
)

Implementation

CommandService(List<MineralCommandContract> commands): super(inject: true) {
  register(commands);

  controller.stream.listen((event) async {
    final commandIdentifier = event.interaction.identifier;
    final command = _commands.get(commandIdentifier);

    if (command == null) {
      return;
    }

    String identifier = event.interaction.data['name'];

    void test (List<dynamic> options) {
      for (final option in options) {
        if (option['type'] == CommandType.group.type) {
          identifier += '.' + option['name'];
          test(option['options']);
        } else if (option['type'] == CommandType.subcommand.type) {
          identifier += '.' + option['name'];
        }
      }
    }

    final option = event.interaction.data['options']?[0];

    if (option?['options'] != null) {
      test(event.interaction.data['options']);
    }

    final Scope scope = command.scope ?? Scope.guild;

    if(scope.isGuild) {
      Function function = _guildHandlers.getOrFail(identifier);
      await function(event.getInteraction<GuildCommandInteraction>(Scope.guild));
    } else {
      Function function = _globalHandlers.getOrFail(identifier);
      await function(event.getInteraction<GlobalCommandInteraction>(Scope.global));
    }
  });
}