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

@@ -56,11 +56,23 @@ namespace Discord.WebSocket
cachedMessageIds = _orderedMessages;
else if (dir == Direction.Before)
cachedMessageIds = _orderedMessages.Where(x => x < fromMessageId.Value);
else
else if (dir == Direction.After)
cachedMessageIds = _orderedMessages.Where(x => x > fromMessageId.Value);
else //Direction.Around
{
if (!_messages.TryGetValue(fromMessageId.Value, out SocketMessage msg))
return ImmutableArray<SocketMessage>.Empty;
int around = limit / 2;
var before = GetMany(fromMessageId, Direction.Before, around);
var after = GetMany(fromMessageId, Direction.After, around).Reverse();
return after.Concat(new SocketMessage[] { msg }).Concat(before).ToImmutableArray();
}
if (dir == Direction.Before)
cachedMessageIds = cachedMessageIds.Reverse();
if (dir == Direction.Around) //Only happens if fromMessageId is null, should only get "around" and itself (+1)
limit = limit / 2 + 1;
return cachedMessageIds
.Select(x =>