From e3984ab9688c5ea382d4a9e362572d457afc8fbd Mon Sep 17 00:00:00 2001
From: Toastie <toastie@toastiet0ast.com>
Date: Fri, 27 Dec 2024 19:36:02 +1300
Subject: [PATCH] Added transcript dir to config

---
 Config.cs          |  2 ++
 SupportChild.cs    |  2 --
 Transcriber.cs     | 38 +++++++++++++++++++++++---------------
 default_config.yml |  3 +++
 4 files changed, 28 insertions(+), 17 deletions(-)

diff --git a/Config.cs b/Config.cs
index 0dd3e51..80a85cd 100644
--- a/Config.cs
+++ b/Config.cs
@@ -20,6 +20,7 @@ internal static class Config
     internal static bool newCommandUsesSelector = false;
     internal static int ticketLimit = 5;
     internal static bool pinFirstMessage = false;
+    internal static string transcriptDir = "";
 
     internal static bool ticketUpdatedNotifications = false;
     internal static double ticketUpdatedNotificationDelay = 0.0;
@@ -91,6 +92,7 @@ internal static class Config
         newCommandUsesSelector = json.SelectToken("bot.new-command-uses-selector")?.Value<bool>() ?? false;
         ticketLimit = json.SelectToken("bot.ticket-limit")?.Value<int>() ?? 5;
         pinFirstMessage = json.SelectToken("bot.pin-first-message")?.Value<bool>() ?? false;
+        transcriptDir = json.SelectToken("bot.transcript-dir")?.Value<string>() ?? "";
 
         ticketUpdatedNotifications = json.SelectToken("notifications.ticket-updated")?.Value<bool>() ?? false;
         ticketUpdatedNotificationDelay = json.SelectToken("notifications.ticket-updated-delay")?.Value<double>() ?? 0.0;
diff --git a/SupportChild.cs b/SupportChild.cs
index 781a397..b719148 100644
--- a/SupportChild.cs
+++ b/SupportChild.cs
@@ -27,7 +27,6 @@ internal static class SupportChild
                 "config",
                 Required = false,
                 HelpText = "Select a config file to use.",
-                Default = "config.yml",
                 MetaValue = "PATH")]
         public string configPath { get; set; }
 
@@ -35,7 +34,6 @@ internal static class SupportChild
                 "transcripts",
                 Required = false,
                 HelpText = "Select directory to store transcripts in.",
-                Default = "./transcripts",
                 MetaValue = "PATH")]
         public string transcriptDir { get; set; }
 
diff --git a/Transcriber.cs b/Transcriber.cs
index c836adb..08e2a26 100644
--- a/Transcriber.cs
+++ b/Transcriber.cs
@@ -13,23 +13,11 @@ namespace SupportChild;
 
 internal static class Transcriber
 {
-    private static string transcriptDir = "./transcripts"; // TODO: Should be local variable (or come from the config class) when added to config to avoid race conditions when reloading
-
     internal static async Task ExecuteAsync(ulong channelID, uint ticketID)
     {
         DiscordClient discordClient = new DiscordClient(Config.token);
         ChannelExporter exporter = new ChannelExporter(discordClient);
 
-        if (!string.IsNullOrEmpty(SupportChild.commandLineArgs.transcriptDir))
-        {
-            transcriptDir = SupportChild.commandLineArgs.transcriptDir;
-        }
-
-        if (!Directory.Exists(transcriptDir))
-        {
-            Directory.CreateDirectory(transcriptDir);
-        }
-
         string htmlPath = GetHtmlPath(ticketID);
         string zipPath = GetZipPath(ticketID);
         string assetDirPath = GetAssetDirPath(ticketID);
@@ -99,19 +87,39 @@ internal static class Transcriber
         }
     }
 
+    private static string GetTranscriptDir()
+    {
+        string transcriptDir = "./transcripts";
+        if (!string.IsNullOrEmpty(SupportChild.commandLineArgs.transcriptDir))
+        {
+            transcriptDir = SupportChild.commandLineArgs.transcriptDir;
+        }
+        else if (!string.IsNullOrEmpty(Config.transcriptDir))
+        {
+            transcriptDir = Config.transcriptDir;
+        }
+
+        if (!Directory.Exists(transcriptDir))
+        {
+            Directory.CreateDirectory(transcriptDir);
+        }
+
+        return transcriptDir;
+    }
+
     internal static string GetHtmlPath(uint ticketNumber)
     {
-        return transcriptDir + "/" + GetHTMLFilename(ticketNumber);
+        return GetTranscriptDir() + "/" + GetHTMLFilename(ticketNumber);
     }
 
     internal static string GetZipPath(uint ticketNumber)
     {
-        return transcriptDir + "/" + GetZipFilename(ticketNumber);
+        return GetTranscriptDir() + "/" + GetZipFilename(ticketNumber);
     }
 
     internal static string GetAssetDirPath(uint ticketNumber)
     {
-        return transcriptDir + "/" + GetAssetDirName(ticketNumber);
+        return GetTranscriptDir() + "/" + GetAssetDirName(ticketNumber);
     }
 
     internal static string GetAssetDirName(uint ticketNumber)
diff --git a/default_config.yml b/default_config.yml
index 0989924..e0ece3b 100644
--- a/default_config.yml
+++ b/default_config.yml
@@ -39,6 +39,9 @@
     # Pins the first message in a ticket to allow for quick navigation to the top in large tickets.
     pin-first-message: true
 
+    # Ticket transcript location, can be overridden using command line arguments.
+    transcript-dir: "./transcripts"
+
 notifications:
     # Notifies the assigned staff member when a new message is posted in a ticket if the ticket has been silent for a configurable amount of time.
     # Other staff members and bots do not trigger this.