feature: Add Direction.Around to GetMessagesAsync (#1526)

* Add Direction.Around to GetMessagesAsync

* Reuse the method

* Reuse GetMany

* Fix limit when getting from cache without message id

* Fix limit when getting from rest without message id

* Change cache return

It will return in a similar way to REST
This commit is contained in:
Paulo
2020-06-18 00:48:45 -03:00
committed by GitHub
parent d5d10d32cf
commit f2130f8513
3 changed files with 47 additions and 23 deletions

View File

@@ -109,12 +109,19 @@ namespace Discord.Rest
public static IAsyncEnumerable<IReadOnlyCollection<RestMessage>> GetMessagesAsync(IMessageChannel channel, BaseDiscordClient client,
ulong? fromMessageId, Direction dir, int limit, RequestOptions options)
{
if (dir == Direction.Around)
throw new NotImplementedException(); //TODO: Impl
var guildId = (channel as IGuildChannel)?.GuildId;
var guild = guildId != null ? (client as IDiscordClient).GetGuildAsync(guildId.Value, CacheMode.CacheOnly).Result : null;
if (dir == Direction.Around && limit > DiscordConfig.MaxMessagesPerBatch)
{
int around = limit / 2;
if (fromMessageId.HasValue)
return GetMessagesAsync(channel, client, fromMessageId.Value + 1, Direction.Before, around + 1, options) //Need to include the message itself
.Concat(GetMessagesAsync(channel, client, fromMessageId, Direction.After, around, options));
else //Shouldn't happen since there's no public overload for ulong? and Direction
return GetMessagesAsync(channel, client, null, Direction.Before, around + 1, options);
}
return new PagedAsyncEnumerable<RestMessage>(
DiscordConfig.MaxMessagesPerBatch,
async (info, ct) =>