[Fix] GetMessagesAsync with Direction.After not working as expected #2744 (#2745)

* Fixed GetMessagesAsync with Direction.After issue #2744

* Fixed GetMessagesAsync with Direction.After issue #2744
This commit is contained in:
José Santos Garrido
2023-08-10 15:33:56 +02:00
committed by GitHub
parent 6bb3777bf9
commit c92249702b

View File

@@ -33,14 +33,31 @@ namespace Discord.WebSocket
} }
else if (dir == Direction.After) else if (dir == Direction.After)
{ {
limit -= cachedMessages.Count; if (mode == CacheMode.CacheOnly)
if (mode == CacheMode.CacheOnly || limit <= 0)
return result; return result;
bool ignoreCache = false;
// We can find two cases:
// 1. fromMessageId is not null and corresponds to a message that is not in the cache,
// so we have to make a request and ignore the cache
if (fromMessageId.HasValue && messages?.Get(fromMessageId.Value) == null)
{
ignoreCache = true;
}
// 2. fromMessageId is null or already in the cache, so we start from the cache
else if (cachedMessages.Count > 0)
{
fromMessageId = cachedMessages.Max(x => x.Id);
limit -= cachedMessages.Count;
if (limit <= 0)
return result;
}
//Download remaining messages //Download remaining messages
ulong maxId = cachedMessages.Count > 0 ? cachedMessages.Max(x => x.Id) : fromMessageId.Value; var downloadedMessages = ChannelHelper.GetMessagesAsync(channel, discord, fromMessageId, dir, limit, options);
var downloadedMessages = ChannelHelper.GetMessagesAsync(channel, discord, maxId, dir, limit, options); if (!ignoreCache && cachedMessages.Count != 0)
if (cachedMessages.Count != 0)
return result.Concat(downloadedMessages); return result.Concat(downloadedMessages);
else else
return downloadedMessages; return downloadedMessages;