From bcf652edbe26753789fa1f1fb2a7516f99e7f1ff Mon Sep 17 00:00:00 2001 From: Tyrrrz <1935960+Tyrrrz@users.noreply.github.com> Date: Sun, 23 Jul 2023 16:04:53 +0300 Subject: [PATCH] Don't throw when parent channel cannot be retrieved Closes #1108 --- .../Discord/Data/Channel.cs | 5 +++-- .../Discord/DiscordClient.cs | 18 ++++++++++++++---- 2 files changed, 17 insertions(+), 6 deletions(-) diff --git a/DiscordChatExporter.Core/Discord/Data/Channel.cs b/DiscordChatExporter.Core/Discord/Data/Channel.cs index 625a8374..fd4403c1 100644 --- a/DiscordChatExporter.Core/Discord/Data/Channel.cs +++ b/DiscordChatExporter.Core/Discord/Data/Channel.cs @@ -20,8 +20,9 @@ public partial record Channel( Snowflake? LastMessageId) : IHasId { // Used for visual backwards-compatibility with old exports, where - // channels without a parent (i.e. mostly DM channels) had a fallback - // category created for them. + // channels without a parent (i.e. mostly DM channels) or channels + // with an inaccessible parent (i.e. inside private categories) had + // a fallback category created for them. public string Category => Parent?.Name ?? Kind switch { ChannelKind.GuildCategory => "Category", diff --git a/DiscordChatExporter.Core/Discord/DiscordClient.cs b/DiscordChatExporter.Core/Discord/DiscordClient.cs index b112ec2c..0b5f181b 100644 --- a/DiscordChatExporter.Core/Discord/DiscordClient.cs +++ b/DiscordChatExporter.Core/Discord/DiscordClient.cs @@ -368,11 +368,21 @@ public class DiscordClient .GetNonWhiteSpaceStringOrNull()? .Pipe(Snowflake.Parse); - var parent = parentId is not null - ? await GetChannelAsync(parentId.Value, cancellationToken) - : null; + try + { + var parent = parentId is not null + ? await GetChannelAsync(parentId.Value, cancellationToken) + : null; - return Channel.Parse(response, parent); + return Channel.Parse(response, parent); + } + // It's possible for the parent channel to be inaccessible, despite the + // child channel being accessible. + // https://github.com/Tyrrrz/DiscordChatExporter/issues/1108 + catch (DiscordChatExporterException) + { + return Channel.Parse(response); + } } private async ValueTask TryGetLastMessageAsync(