collect method

  1. @override
Future<Map<Snowflake, T>> collect()

It starts listening to the MessageCreateEvent and collects messages that match the filter criteria.

If max messages have been collected, it cancels the subscription, unsubscribes the collector from the CollectorService, and returns a future containing the collected messages.

final collector = MessageCollector<Message>(
  handle: (message) => !message.author.member.isBot,
  max: 2,
  time: Duration(seconds: 10)
);

final Map<Snowflake, T> messages = await collector.collect();

Implementation

@override
Future<Map<Snowflake, T>> collect () async {
  final completer = Completer<Map<Snowflake, T>>();
  _subscription = controller.stream.listen((message) async {
    if (filter(message)) {
      _messages.putIfAbsent(message.id, () => message);
    }

    if (_messages.length == max) {
      await _unsubscribe();
      completer.complete(_messages);
    }
  });

  return completer.future;
}