@@ -582,29 +582,28 @@ namespace Discord.API
if (args.Limit <= 0) throw new ArgumentOutOfRangeException(nameof(args.Limit));
if (args.Limit <= 0) throw new ArgumentOutOfRangeException(nameof(args.Limit));
int limit = args.Limit;
int limit = args.Limit;
ulong? relativeId = args.RelativeMessageId;
ulong? relativeId = args.RelativeMessageId.IsSpecified ? args.RelativeMessageId.Value : (ulong?)null ;
string relativeDir = args.RelativeDirection == Direction.After ? "after" : "before";
string relativeDir = args.RelativeDirection == Direction.After ? "after" : "before";
int runs = limit / DiscordConfig.MaxMessagesPerBatch;
int runs = limit / DiscordConfig.MaxMessagesPerBatch;
int lastRunCount = limit - runs * DiscordConfig.MaxMessagesPerBatch;
int lastRunCount = limit - ( runs - 1) * DiscordConfig.MaxMessagesPerBatch;
var result = new API.Message[runs][];
var result = new API.Message[runs][];
int i = 0;
int i = 0;
for (; i < runs; i++)
for (; i < runs; i++)
{
{
int runCount = i == (runs - 1) ? lastRunCount : DiscordConfig.MaxMessagesPerBatch;
string endpoint;
string endpoint;
if (relativeId != null)
if (relativeId != null)
endpoint = $"channels/{channelId}/messages?limit={limi t}&{relativeDir}={relativeId}";
endpoint = $"channels/{channelId}/messages?limit={runCoun t}&{relativeDir}={relativeId}";
else
else
endpoint = $"channels/{channelId}/messages?limit={limi t}";
endpoint = $"channels/{channelId}/messages?limit={runCoun t}";
var models = await Send<Message[]>("GET", endpoint).ConfigureAwait(false);
var models = await Send<Message[]>("GET", endpoint).ConfigureAwait(false);
//Was this an empty batch?
//Was this an empty batch?
if (models.Length == 0) break;
if (models.Length == 0) break;
result[i] = models;
limit = (i == runs - 1) ? lastRunCount : DiscordConfig.MaxMessagesPerBatch;
result[i] = models;
relativeId = args.RelativeDirection == Direction.Before ? models[0].Id : models[models.Length - 1].Id;
relativeId = args.RelativeDirection == Direction.Before ? models[0].Id : models[models.Length - 1].Id;
//Was this an incomplete (the last) batch?
//Was this an incomplete (the last) batch?
@@ -612,7 +611,12 @@ namespace Discord.API
}
}
if (i > 1)
if (i > 1)
return result.Take(i).SelectMany(x => x);
{
if (args.RelativeDirection == Direction.Before)
return result.Take(i).SelectMany(x => x);
else
return result.Take(i).Reverse().SelectMany(x => x);
}
else if (i == 1)
else if (i == 1)
return result[0];
return result[0];
else
else