From a59bfaaeff704aaecbfa1e3cc4496063e881ccf3 Mon Sep 17 00:00:00 2001 From: RogueException Date: Sun, 15 May 2016 15:13:31 -0300 Subject: [PATCH] Several GetMessages fixes --- src/Discord.Net/API/DiscordRawClient.cs | 20 ++++++++++++-------- src/Discord.Net/API/Rest/CreateGuildParams.cs | 3 +-- 2 files changed, 13 insertions(+), 10 deletions(-) diff --git a/src/Discord.Net/API/DiscordRawClient.cs b/src/Discord.Net/API/DiscordRawClient.cs index 64055db84..9be604b86 100644 --- a/src/Discord.Net/API/DiscordRawClient.cs +++ b/src/Discord.Net/API/DiscordRawClient.cs @@ -582,29 +582,28 @@ namespace Discord.API if (args.Limit <= 0) throw new ArgumentOutOfRangeException(nameof(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"; int runs = limit / DiscordConfig.MaxMessagesPerBatch; - int lastRunCount = limit - runs * DiscordConfig.MaxMessagesPerBatch; + int lastRunCount = limit - (runs - 1) * DiscordConfig.MaxMessagesPerBatch; var result = new API.Message[runs][]; int i = 0; for (; i < runs; i++) { + int runCount = i == (runs - 1) ? lastRunCount : DiscordConfig.MaxMessagesPerBatch; string endpoint; if (relativeId != null) - endpoint = $"channels/{channelId}/messages?limit={limit}&{relativeDir}={relativeId}"; + endpoint = $"channels/{channelId}/messages?limit={runCount}&{relativeDir}={relativeId}"; else - endpoint = $"channels/{channelId}/messages?limit={limit}"; + endpoint = $"channels/{channelId}/messages?limit={runCount}"; var models = await Send("GET", endpoint).ConfigureAwait(false); //Was this an empty batch? 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; //Was this an incomplete (the last) batch? @@ -612,7 +611,12 @@ namespace Discord.API } 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) return result[0]; else diff --git a/src/Discord.Net/API/Rest/CreateGuildParams.cs b/src/Discord.Net/API/Rest/CreateGuildParams.cs index 6cf0ed149..09c294b5e 100644 --- a/src/Discord.Net/API/Rest/CreateGuildParams.cs +++ b/src/Discord.Net/API/Rest/CreateGuildParams.cs @@ -1,5 +1,4 @@ -using Discord.Net.Converters; -using Newtonsoft.Json; +using Newtonsoft.Json; using System.IO; namespace Discord.API.Rest