Skip to content

Commit

Permalink
[Fix] GetMessagesAsync with Direction.After not working as expected #…
Browse files Browse the repository at this point in the history
…2744 (#2745)

* Fixed GetMessagesAsync with Direction.After issue #2744

* Fixed GetMessagesAsync with Direction.After issue #2744
  • Loading branch information
josago97 committed Aug 10, 2023
1 parent 6bb3777 commit c922497
Showing 1 changed file with 22 additions and 5 deletions.
27 changes: 22 additions & 5 deletions src/Discord.Net.WebSocket/Entities/Channels/SocketChannelHelper.cs
Expand Up @@ -33,14 +33,31 @@ internal static class SocketChannelHelper
}
else if (dir == Direction.After)
{
limit -= cachedMessages.Count;
if (mode == CacheMode.CacheOnly || limit <= 0)
if (mode == CacheMode.CacheOnly)
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
ulong maxId = cachedMessages.Count > 0 ? cachedMessages.Max(x => x.Id) : fromMessageId.Value;
var downloadedMessages = ChannelHelper.GetMessagesAsync(channel, discord, maxId, dir, limit, options);
if (cachedMessages.Count != 0)
var downloadedMessages = ChannelHelper.GetMessagesAsync(channel, discord, fromMessageId, dir, limit, options);
if (!ignoreCache && cachedMessages.Count != 0)
return result.Concat(downloadedMessages);
else
return downloadedMessages;
Expand Down

0 comments on commit c922497

Please sign in to comment.