Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add path placeholders for threads #1192

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# User-specific files
.vs/
.vscode/
.idea/
*.suo
*.user
Expand All @@ -9,4 +10,4 @@ bin/
obj/

# Test results
TestResults/
TestResults/
48 changes: 43 additions & 5 deletions DiscordChatExporter.Core/Exporting/ExportRequest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -154,27 +154,39 @@ public partial class ExportRequest
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)
Expand All @@ -194,6 +206,32 @@ m.Value switch
)
);

// 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,
@"\\[^\\]*%[xyz][^\\]*\\",
m =>
channel.IsThread
? Regex.Replace(
m.Value,
"%[xyz]",
n =>
n.Value switch
{
"%x" => channel.Parent?.Id.ToString() ?? "",
"%y"
=> channel
.Parent?.Position
?.ToString(CultureInfo.InvariantCulture) ?? "",
"%z" => channel.Parent?.Name ?? "",
_ => n.Value
}
)
: "\\"
);
return formattedPath;
}

private static string GetOutputBaseFilePath(
Guild guild,
Channel channel,
Expand Down