From b8567d384f9552bd25b6ae72ee114dba38dcf216 Mon Sep 17 00:00:00 2001 From: Tyrrrz <1935960+Tyrrrz@users.noreply.github.com> Date: Sun, 12 Dec 2021 23:45:52 +0200 Subject: [PATCH] More defensive programming --- DiscordChatExporter.Core/Discord/Data/Channel.cs | 7 ++++++- DiscordChatExporter.Core/Discord/Data/Embeds/Embed.cs | 2 +- DiscordChatExporter.Core/Discord/Data/Message.cs | 8 ++++---- 3 files changed, 11 insertions(+), 6 deletions(-) diff --git a/DiscordChatExporter.Core/Discord/Data/Channel.cs b/DiscordChatExporter.Core/Discord/Data/Channel.cs index f13701e..3268175 100644 --- a/DiscordChatExporter.Core/Discord/Data/Channel.cs +++ b/DiscordChatExporter.Core/Discord/Data/Channel.cs @@ -52,9 +52,14 @@ public partial record Channel var name = // Guild channel json.GetPropertyOrNull("name")?.GetStringOrNull() ?? + // DM channel - json.GetPropertyOrNull("recipients")?.EnumerateArray().Select(User.Parse).Select(u => u.Name) + json.GetPropertyOrNull("recipients")? + .EnumerateArrayOrNull()? + .Select(User.Parse) + .Select(u => u.Name) .Pipe(s => string.Join(", ", s)) ?? + // Fallback id.ToString(); diff --git a/DiscordChatExporter.Core/Discord/Data/Embeds/Embed.cs b/DiscordChatExporter.Core/Discord/Data/Embeds/Embed.cs index 7b72f92..653f8e0 100644 --- a/DiscordChatExporter.Core/Discord/Data/Embeds/Embed.cs +++ b/DiscordChatExporter.Core/Discord/Data/Embeds/Embed.cs @@ -47,7 +47,7 @@ public partial record Embed var footer = json.GetPropertyOrNull("footer")?.Pipe(EmbedFooter.Parse); var fields = - json.GetPropertyOrNull("fields")?.EnumerateArray().Select(EmbedField.Parse).ToArray() ?? + json.GetPropertyOrNull("fields")?.EnumerateArrayOrNull()?.Select(EmbedField.Parse).ToArray() ?? Array.Empty(); return new Embed( diff --git a/DiscordChatExporter.Core/Discord/Data/Message.cs b/DiscordChatExporter.Core/Discord/Data/Message.cs index 1a379b9..55c9807 100644 --- a/DiscordChatExporter.Core/Discord/Data/Message.cs +++ b/DiscordChatExporter.Core/Discord/Data/Message.cs @@ -53,19 +53,19 @@ public record Message( }; var attachments = - json.GetPropertyOrNull("attachments")?.EnumerateArray().Select(Attachment.Parse).ToArray() ?? + json.GetPropertyOrNull("attachments")?.EnumerateArrayOrNull()?.Select(Attachment.Parse).ToArray() ?? Array.Empty(); var embeds = - json.GetPropertyOrNull("embeds")?.EnumerateArray().Select(Embed.Parse).ToArray() ?? + json.GetPropertyOrNull("embeds")?.EnumerateArrayOrNull()?.Select(Embed.Parse).ToArray() ?? Array.Empty(); var reactions = - json.GetPropertyOrNull("reactions")?.EnumerateArray().Select(Reaction.Parse).ToArray() ?? + json.GetPropertyOrNull("reactions")?.EnumerateArrayOrNull()?.Select(Reaction.Parse).ToArray() ?? Array.Empty(); var mentionedUsers = - json.GetPropertyOrNull("mentions")?.EnumerateArray().Select(User.Parse).ToArray() ?? + json.GetPropertyOrNull("mentions")?.EnumerateArrayOrNull()?.Select(User.Parse).ToArray() ?? Array.Empty(); return new Message(