afterConnect method

  1. @override
void afterConnect(
  1. NyxxGateway client
)

Called after each client this plugin is added to connects.

Implementation

@override
void afterConnect(NyxxGateway client) async {
  client.onMessageComponentInteraction.listen((event) async {
    final interaction = event.interaction;
    final data = interaction.data;

    if (data.type != MessageComponentType.button) {
      return;
    }

    final state = _states[data.customId];

    if (state == null) {
      if (data.customId.startsWith('nyxx_pagination/')) {
        _unhandledInteractionsController.add(event);
      }
      return;
    }

    if (state.userId != null && (interaction.user?.id ?? interaction.member!.id) != state.userId) {
      _disallowedUseController.add(event);
      return;
    }

    if (data.customId == state.jumpToStartId) {
      state.currentIndex = 0;
    } else if (data.customId == state.previousId) {
      state.currentIndex--;
    } else if (data.customId == state.nextId) {
      state.currentIndex++;
    } else if (data.customId == state.jumpToEndId) {
      state.currentIndex = state.builders.length - 1;
    }

    await interaction.respond(
      updateMessage: true,
      _PaginationMessageUpdateBuilder(await state.builderForIndex(state.currentIndex)),
    );
  });

  client.onMessageCreate.listen((event) {
    final rows = (event.message.components?.cast<ActionRowComponent>()) ?? <ActionRowComponent>[];
    final components = rows.expand((element) => element.components);

    for (final component in components.whereType<ButtonComponent>()) {
      final state = _states[component.customId];

      if (state == null) continue;

      (_clientStates[event.gateway.client] ??= {}).add(state);
      state.message = event.message;

      final timeout = state.options?.timeout ?? options.timeout;
      if (timeout != null) {
        state.disableTimer = Timer(timeout, () async {
          state.isDisabled = true;

          await event.message.update(_PaginationMessageUpdateBuilder(
            await state.builderForIndex(state.currentIndex),
          ));

          _unregisterPagination(state);
        });
      }

      break;
    }
  });
}