afterConnect method

  1. @override
Future<void> afterConnect(
  1. NyxxGateway client
)
override

Called after each client this plugin is added to connects.

Implementation

@override
Future<void> afterConnect(NyxxGateway client) async {
  _attachedClients.add(client);

  client.onMessageComponentInteraction
      .map((event) => event.interaction)
      .where((interaction) => interaction.data.type == MessageComponentType.button)
      .listen(
    (interaction) async {
      try {
        await eventManager.processButtonInteraction(interaction);
      } on CommandsException catch (e) {
        _onCommandErrorController.add(e);
      }
    },
  );

  client.onMessageComponentInteraction
      .map((event) => event.interaction)
      .where((interaction) => interaction.data.type == MessageComponentType.stringSelect)
      .listen(
    (interaction) async {
      try {
        await eventManager.processSelectMenuInteraction(interaction);
      } on CommandsException catch (e) {
        _onCommandErrorController.add(e);
      }
    },
  );

  client.onMessageCreate.listen((event) async {
    try {
      await eventManager.processMessageCreateEvent(event);
    } on CommandsException catch (e) {
      _onCommandErrorController.add(e);
    }
  });

  client.onApplicationCommandInteraction.map((event) => event.interaction).listen(
    (interaction) async {
      try {
        final applicationCommand = registeredCommands.singleWhere(
          (command) => command.id == interaction.data.id,
        );

        if (interaction.data.type == ApplicationCommandType.user) {
          await eventManager.processUserInteraction(
            interaction,
            _userCommands[applicationCommand.name]!,
          );
        } else if (interaction.data.type == ApplicationCommandType.message) {
          await eventManager.processMessageInteraction(
            interaction,
            _messageCommands[applicationCommand.name]!,
          );
        } else if (interaction.data.type == ApplicationCommandType.chatInput) {
          final (command, options) = _resolveChatCommand(interaction, applicationCommand);

          await eventManager.processChatInteraction(
            interaction,
            options,
            command,
          );
        }
      } on CommandsException catch (e) {
        _onCommandErrorController.add(e);
      }
    },
  );

  client.onApplicationCommandAutocompleteInteraction
      .map((event) => event.interaction)
      .listen((interaction) async {
    try {
      final applicationCommand = registeredCommands.singleWhere(
        (command) => command.id == interaction.data.id,
      );

      final (command, options) = _resolveChatCommand(interaction, applicationCommand);

      final functionData = loadFunctionData(command.execute);
      final focusedOption = options.singleWhere((element) => element.isFocused == true);
      final focusedParameter = functionData.parametersData
          .singleWhere((element) => element.name == focusedOption.name);

      final converter = focusedParameter.converterOverride ?? getConverter(focusedParameter.type);

      await eventManager.processAutocompleteInteraction(
        interaction,
        (focusedParameter.autocompleteOverride ?? converter?.autocompleteCallback)!,
        command,
      );
    } on CommandsException catch (e) {
      _onCommandErrorController.add(e);
    }
  });

  if (children.isNotEmpty) {
    _syncCommands(client);
  }
}