From 7bfb1c6fcce5f4e5d74bae4789c1e817eafc369f Mon Sep 17 00:00:00 2001 From: "Jacob P." Date: Wed, 24 Jan 2024 01:24:29 +0100 Subject: [PATCH 1/2] Add path placeholders for threads --- .gitignore | 3 +- .../Exporting/ExportRequest.cs | 47 +++++++++++++++++-- 2 files changed, 44 insertions(+), 6 deletions(-) diff --git a/.gitignore b/.gitignore index ef8150c0e..d967f8b06 100644 --- a/.gitignore +++ b/.gitignore @@ -1,5 +1,6 @@ # User-specific files .vs/ +.vscode/ .idea/ *.suo *.user @@ -9,4 +10,4 @@ bin/ obj/ # Test results -TestResults/ \ No newline at end of file +TestResults/ diff --git a/DiscordChatExporter.Core/Exporting/ExportRequest.cs b/DiscordChatExporter.Core/Exporting/ExportRequest.cs index 30a7cb285..d5af1f0b3 100644 --- a/DiscordChatExporter.Core/Exporting/ExportRequest.cs +++ b/DiscordChatExporter.Core/Exporting/ExportRequest.cs @@ -154,27 +154,39 @@ private static string FormatPath( Channel channel, Snowflake? after, Snowflake? before - ) => - Regex.Replace( + ) + { + string preFormattedPath = Regex.Replace( path, "%.", m => PathEx.EscapeFileName( m.Value switch { + // On %T and %P, we have to make sure that we still get name and position of the category if the channel is a thread "%g" => guild.Id.ToString(), "%G" => guild.Name, "%t" => channel.Parent?.Id.ToString() ?? "", - "%T" => channel.Parent?.Name ?? "", + "%T" + => channel.IsThread + ? (channel.Parent?.Parent?.Name ?? "") + : channel.Parent?.Name ?? "", "%c" => channel.Id.ToString(), "%C" => channel.Name, "%p" => channel.Position?.ToString(CultureInfo.InvariantCulture) ?? "0", "%P" - => channel.Parent?.Position?.ToString(CultureInfo.InvariantCulture) - ?? "0", + => channel.IsThread + ? ( + channel + .Parent?.Parent + ?.Position + ?.ToString(CultureInfo.InvariantCulture) ?? "" + ) + : channel.Parent?.Position?.ToString(CultureInfo.InvariantCulture) + ?? "0", "%a" => after?.ToDate().ToString("yyyy-MM-dd", CultureInfo.InvariantCulture) @@ -194,6 +206,31 @@ private static string FormatPath( ) ); + // We are looking for any structure in the path which contains either %y or %z. If the channel is a thread, we can resolve the placeholders, otherwise, that whole structure needs to be removed. + string formattedPath = Regex.Replace( + preFormattedPath, + @"\\[^\\]*%[yz][^\\]*\\", + m => + channel.IsThread + ? Regex.Replace( + m.Value, + "%[yz]", + n => + n.Value switch + { + "%y" + => channel + .Parent?.Position + ?.ToString(CultureInfo.InvariantCulture) ?? "", + "%z" => channel.Parent?.Name ?? "", + _ => n.Value + } + ) + : "\\" + ); + return formattedPath; + } + private static string GetOutputBaseFilePath( Guild guild, Channel channel, From 0792ca769118d9793e7c5f9df67276581a0ed37a Mon Sep 17 00:00:00 2001 From: "Jacob P." Date: Wed, 24 Jan 2024 01:34:41 +0100 Subject: [PATCH 2/2] Add third parameter for ID of parent channel --- DiscordChatExporter.Core/Exporting/ExportRequest.cs | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/DiscordChatExporter.Core/Exporting/ExportRequest.cs b/DiscordChatExporter.Core/Exporting/ExportRequest.cs index d5af1f0b3..411379e92 100644 --- a/DiscordChatExporter.Core/Exporting/ExportRequest.cs +++ b/DiscordChatExporter.Core/Exporting/ExportRequest.cs @@ -209,15 +209,16 @@ private static string FormatPath( // We are looking for any structure in the path which contains either %y or %z. If the channel is a thread, we can resolve the placeholders, otherwise, that whole structure needs to be removed. string formattedPath = Regex.Replace( preFormattedPath, - @"\\[^\\]*%[yz][^\\]*\\", + @"\\[^\\]*%[xyz][^\\]*\\", m => channel.IsThread ? Regex.Replace( m.Value, - "%[yz]", + "%[xyz]", n => n.Value switch { + "%x" => channel.Parent?.Id.ToString() ?? "", "%y" => channel .Parent?.Position