From 65314622201ccb25f17ac2942e2f556cb5054066 Mon Sep 17 00:00:00 2001 From: Alexey Golub Date: Sun, 10 Feb 2019 17:26:18 +0200 Subject: [PATCH] Fix getting last message when there's no date range Fixes #137 --- DiscordChatExporter.Core/Services/DataService.cs | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/DiscordChatExporter.Core/Services/DataService.cs b/DiscordChatExporter.Core/Services/DataService.cs index 99da855c..3a9e6f8e 100644 --- a/DiscordChatExporter.Core/Services/DataService.cs +++ b/DiscordChatExporter.Core/Services/DataService.cs @@ -41,10 +41,15 @@ namespace DiscordChatExporter.Core.Services : new AuthenticationHeaderValue(token.Value); // Add parameters - foreach (var parameter in parameters) + foreach (var parameter in parameters.ExceptBlank()) { var key = parameter.SubstringUntil("="); var value = parameter.SubstringAfter("="); + + // Skip empty values + if (value.IsBlank()) + continue; + request.RequestUri = request.RequestUri.SetQueryParameter(key, value); } @@ -123,13 +128,9 @@ namespace DiscordChatExporter.Core.Services { var result = new List(); - // Get the snowflakes for the selected range - var firstId = from != null ? from.Value.ToSnowflake() : "0"; - var lastId = to != null ? to.Value.ToSnowflake() : DateTime.MaxValue.ToSnowflake(); - // Get the last message var response = await GetApiResponseAsync(token, "channels", $"{channelId}/messages", - "limit=1", $"before={lastId}"); + "limit=1", $"before={to?.ToSnowflake()}"); var lastMessage = response.Select(ParseMessage).FirstOrDefault(); // If the last message doesn't exist or it's outside of range - return @@ -140,7 +141,7 @@ namespace DiscordChatExporter.Core.Services } // Get other messages - var offsetId = firstId; + var offsetId = from?.ToSnowflake() ?? "0"; while (true) { // Get message batch