Exemple #1
0
        /// <summary>
        /// Tells the <see cref="MessageCollector"/> to begin collecting messages.
        /// </summary>
        /// <param name="filter">The raw filter that will be used when comparing messages.</param>
        /// <param name="options">The options that will be used to set up the <see cref="MessageCollector"/>.</param>
        public async Task <FilterCollection> CollectAsync(FilterCollectionDelegate filter, CollectionOptions options = null)
        {
            options ??= CollectionOptions.Default;
            var matches  = new FilterCollection();
            var timer    = new AsyncTimer(options.Timeout);
            var complete = new TaskCompletionSource <bool>();

            int index = 0;

            async Task HandleAsync(SocketMessage arg)
            {
                bool isSuccess = filter.Invoke(arg, matches, index);

                if (options.IncludeFailedMatches)
                {
                    matches.Add(new FilterMatch(arg, index, isSuccess));
                }
                else if (isSuccess)
                {
                    matches.Add(new FilterMatch(arg, index, isSuccess));
                }

                if (isSuccess && options.ResetTimeoutOnMatch)
                {
                    timer.Reset();
                }

                if (options.Capacity.HasValue && matches.Count == options.Capacity.Value)
                {
                    timer.Stop();
                    complete.SetResult(true);
                }

                index++;
            }

            _client.MessageReceived += HandleAsync;

            if (options.Timeout.HasValue)
            {
                timer.Start();
            }

            await Task.WhenAny(timer.CompletionSource.Task, complete.Task);

            _client.MessageReceived -= HandleAsync;

            ElapsedTime = timer.ElapsedTime;
            return(matches);
        }
Exemple #2
0
 /// <summary>
 /// Tells the <see cref="MessageCollector"/> to begin collecting messages.
 /// </summary>
 /// <param name="filter">The filter that will be used when comparing messages.</param>
 /// <param name="options">The options that will be used to set up the <see cref="MessageCollector"/>.</param>
 public async Task <FilterCollection> CollectAsync(MessageFilter filter, CollectionOptions options = null)
 => await CollectAsync(filter.JudgeMany, options);