diff --git a/src/Discord.Net.Core/Utils/Paging/PagedEnumerator.cs b/src/Discord.Net.Core/Utils/Paging/PagedEnumerator.cs index 730f9517a..96059bb4f 100644 --- a/src/Discord.Net.Core/Utils/Paging/PagedEnumerator.cs +++ b/src/Discord.Net.Core/Utils/Paging/PagedEnumerator.cs @@ -1,6 +1,5 @@ using System; using System.Collections.Generic; -using System.Linq; using System.Threading; using System.Threading.Tasks; @@ -13,9 +12,9 @@ namespace Discord private readonly ulong? _start; private readonly int? _count; private readonly Func>> _getPage; - private readonly Action> _nextPage; + private readonly Func, bool> _nextPage; - public PagedAsyncEnumerable(int pageSize, Func>> getPage, Action> nextPage = null, + public PagedAsyncEnumerable(int pageSize, Func>> getPage, Func, bool> nextPage = null, ulong? start = null, int? count = null) { PageSize = pageSize; @@ -64,7 +63,10 @@ namespace Discord _info.PageSize = _info.Remaining != null ? (int)Math.Min(_info.Remaining.Value, _source.PageSize) : _source.PageSize; if (_info.Remaining != 0) - _source?._nextPage(_info, data); + { + if (!_source._nextPage(_info, data)) + _info.Remaining = 0; + } return true; } diff --git a/src/Discord.Net.Rest/Entities/Channels/ChannelHelper.cs b/src/Discord.Net.Rest/Entities/Channels/ChannelHelper.cs index c41ba5da6..080ad9779 100644 --- a/src/Discord.Net.Rest/Entities/Channels/ChannelHelper.cs +++ b/src/Discord.Net.Rest/Entities/Channels/ChannelHelper.cs @@ -86,16 +86,17 @@ namespace Discord.Rest if (info.Position != null) args.RelativeMessageId = info.Position.Value; var models = await client.ApiClient.GetChannelMessagesAsync(channel.Id, args, options).ConfigureAwait(false); - return models.Select(x => RestMessage.Create(client, guild, x)).ToImmutableArray(); ; + return models.Select(x => RestMessage.Create(client, guild, x)).ToImmutableArray(); }, nextPage: (info, lastPage) => { + if (lastPage.Count != DiscordConfig.MaxMessagesPerBatch) + return false; if (dir == Direction.Before) info.Position = lastPage.Min(x => x.Id); else info.Position = lastPage.Max(x => x.Id); - if (lastPage.Count != DiscordConfig.MaxMessagesPerBatch) - info.Remaining = 0; + return true; }, start: fromMessageId, count: limit @@ -196,9 +197,10 @@ namespace Discord.Rest }, nextPage: (info, lastPage) => { - info.Position = lastPage.Max(x => x.Id); if (lastPage.Count != DiscordConfig.MaxMessagesPerBatch) - info.Remaining = 0; + return false; + info.Position = lastPage.Max(x => x.Id); + return true; }, start: fromUserId, count: limit diff --git a/src/Discord.Net.Rest/Entities/Guilds/GuildHelper.cs b/src/Discord.Net.Rest/Entities/Guilds/GuildHelper.cs index 664e75ec8..f91f131d9 100644 --- a/src/Discord.Net.Rest/Entities/Guilds/GuildHelper.cs +++ b/src/Discord.Net.Rest/Entities/Guilds/GuildHelper.cs @@ -187,9 +187,10 @@ namespace Discord.Rest }, nextPage: (info, lastPage) => { - info.Position = lastPage.Max(x => x.Id); if (lastPage.Count != DiscordConfig.MaxMessagesPerBatch) - info.Remaining = 0; + return false; + info.Position = lastPage.Max(x => x.Id); + return true; }, start: fromUserId, count: limit