From a8031ad3aae641138d3b4e37a46ae01dec08e23c Mon Sep 17 00:00:00 2001 From: Lucas LaBuff Date: Sat, 6 Feb 2021 10:00:29 -0500 Subject: [PATCH] Fix crash when parsing position on DM channels (#496) --- .../Commands/GetDirectMessageChannelsCommand.cs | 2 +- .../Discord/Models/Channel.cs | 10 +++++----- .../Discord/Models/ChannelCategory.cs | 8 ++++---- .../Discord/Models/Common/IHasIdAndPosition.cs | 2 +- ...onBasedComparer.cs => PositionBasedComparer.cs} | 14 +++----------- .../Exporting/ExportRequest.cs | 4 ++-- DiscordChatExporter.Gui/Views/RootView.xaml | 2 +- 7 files changed, 17 insertions(+), 25 deletions(-) rename DiscordChatExporter.Domain/Discord/Models/Common/{ChannelPositionBasedComparer.cs => PositionBasedComparer.cs} (58%) diff --git a/DiscordChatExporter.Cli/Commands/GetDirectMessageChannelsCommand.cs b/DiscordChatExporter.Cli/Commands/GetDirectMessageChannelsCommand.cs index 6d03e9679..118d2beee 100644 --- a/DiscordChatExporter.Cli/Commands/GetDirectMessageChannelsCommand.cs +++ b/DiscordChatExporter.Cli/Commands/GetDirectMessageChannelsCommand.cs @@ -16,7 +16,7 @@ public override async ValueTask ExecuteAsync(IConsole console) { var channels = await GetDiscordClient().GetGuildChannelsAsync(Guild.DirectMessages.Id); - foreach (var channel in channels.OrderBy(c => c.Category, PositionBasedComparer.Instance).ThenBy(c => c.Name)) + foreach (var channel in channels.OrderBy(c => c.Name)) console.Output.WriteLine($"{channel.Id} | {channel.Category} / {channel.Name}"); } } diff --git a/DiscordChatExporter.Domain/Discord/Models/Channel.cs b/DiscordChatExporter.Domain/Discord/Models/Channel.cs index 0dfc2993b..78908fc5b 100644 --- a/DiscordChatExporter.Domain/Discord/Models/Channel.cs +++ b/DiscordChatExporter.Domain/Discord/Models/Channel.cs @@ -41,11 +41,11 @@ public partial class Channel : IHasIdAndPosition public string Name { get; } - public int Position { get; } + public int? Position { get; } public string? Topic { get; } - public Channel(Snowflake id, ChannelType type, Snowflake guildId, ChannelCategory? category, string name, int position, string? topic) + public Channel(Snowflake id, ChannelType type, Snowflake guildId, ChannelCategory? category, string name, int? position, string? topic) { Id = id; Type = type; @@ -89,15 +89,15 @@ public static Channel Parse(JsonElement json, ChannelCategory? category = null, json.GetPropertyOrNull("recipients")?.EnumerateArray().Select(User.Parse).Select(u => u.Name).JoinToString(", ") ?? id.ToString(); - position ??= json.GetProperty("position").GetInt32(); - + position ??= json.GetPropertyOrNull("position")?.GetInt32(); + return new Channel( id, type, guildId ?? Guild.DirectMessages.Id, category ?? GetDefaultCategory(type), name, - position.Value, + position, topic ); } diff --git a/DiscordChatExporter.Domain/Discord/Models/ChannelCategory.cs b/DiscordChatExporter.Domain/Discord/Models/ChannelCategory.cs index 8fcdce264..8a8dfaf18 100644 --- a/DiscordChatExporter.Domain/Discord/Models/ChannelCategory.cs +++ b/DiscordChatExporter.Domain/Discord/Models/ChannelCategory.cs @@ -14,9 +14,9 @@ public partial class ChannelCategory : IHasIdAndPosition public string Name { get; } - public int Position { get; } + public int? Position { get; } - public ChannelCategory(Snowflake id, string name, int position) + public ChannelCategory(Snowflake id, string name, int? position) { Id = id; Name = name; @@ -32,7 +32,7 @@ public partial class ChannelCategory public static ChannelCategory Parse(JsonElement json, int? position = null) { var id = json.GetProperty("id").GetString().Pipe(Snowflake.Parse); - position ??= json.GetProperty("position").GetInt32(); + position ??= json.GetPropertyOrNull("position")?.GetInt32(); var name = json.GetPropertyOrNull("name")?.GetString() ?? json.GetPropertyOrNull("recipients")?.EnumerateArray().Select(User.Parse).Select(u => u.Name).JoinToString(", ") ?? @@ -41,7 +41,7 @@ public static ChannelCategory Parse(JsonElement json, int? position = null) return new ChannelCategory( id, name, - position.Value + position ); } diff --git a/DiscordChatExporter.Domain/Discord/Models/Common/IHasIdAndPosition.cs b/DiscordChatExporter.Domain/Discord/Models/Common/IHasIdAndPosition.cs index 6cf41871b..10a4a7260 100644 --- a/DiscordChatExporter.Domain/Discord/Models/Common/IHasIdAndPosition.cs +++ b/DiscordChatExporter.Domain/Discord/Models/Common/IHasIdAndPosition.cs @@ -2,6 +2,6 @@ { public interface IHasIdAndPosition : IHasId { - int Position { get; } + int? Position { get; } } } \ No newline at end of file diff --git a/DiscordChatExporter.Domain/Discord/Models/Common/ChannelPositionBasedComparer.cs b/DiscordChatExporter.Domain/Discord/Models/Common/PositionBasedComparer.cs similarity index 58% rename from DiscordChatExporter.Domain/Discord/Models/Common/ChannelPositionBasedComparer.cs rename to DiscordChatExporter.Domain/Discord/Models/Common/PositionBasedComparer.cs index cd504c458..00d7d161b 100644 --- a/DiscordChatExporter.Domain/Discord/Models/Common/ChannelPositionBasedComparer.cs +++ b/DiscordChatExporter.Domain/Discord/Models/Common/PositionBasedComparer.cs @@ -6,18 +6,10 @@ public partial class PositionBasedComparer : IComparer { public int Compare(IHasIdAndPosition? x, IHasIdAndPosition? y) { - int result; - if (x != null) + int result = Comparer.Default.Compare(x?.Position, y?.Position); + if (result == 0) { - result = x.Position.CompareTo(y?.Position); - if(result == 0) - { - result = x.Id.Value.CompareTo(y?.Id.Value); - } - } - else - { - result = y == null ? 0 : -1; + result = Comparer.Default.Compare(x?.Id.Value, y?.Id.Value); } return result; } diff --git a/DiscordChatExporter.Domain/Exporting/ExportRequest.cs b/DiscordChatExporter.Domain/Exporting/ExportRequest.cs index a04a4be39..1fa1b2c51 100644 --- a/DiscordChatExporter.Domain/Exporting/ExportRequest.cs +++ b/DiscordChatExporter.Domain/Exporting/ExportRequest.cs @@ -94,8 +94,8 @@ private static string GetOutputBaseFilePath( "%T" => channel.Category.Name, "%c" => channel.Id.ToString(), "%C" => channel.Name, - "%p" => channel.Position.ToString(), - "%P" => channel.Category.Position.ToString(), + "%p" => channel.Position?.ToString() ?? "0", + "%P" => channel.Category.Position?.ToString() ?? "0", "%a" => (after ?? Snowflake.Zero).ToDate().ToString("yyyy-MM-dd"), "%b" => (before?.ToDate() ?? DateTime.Now).ToString("yyyy-MM-dd"), "%%" => "%", diff --git a/DiscordChatExporter.Gui/Views/RootView.xaml b/DiscordChatExporter.Gui/Views/RootView.xaml index 841c3ccda..76a454fcd 100644 --- a/DiscordChatExporter.Gui/Views/RootView.xaml +++ b/DiscordChatExporter.Gui/Views/RootView.xaml @@ -27,7 +27,7 @@ - +