From 1a79ae1d23c6a70f4a4552dbdae096c44067e498 Mon Sep 17 00:00:00 2001 From: Toastie <toastie@toastiet0ast.com> Date: Tue, 4 Feb 2025 20:10:29 +1300 Subject: [PATCH] Fixed setting templates if the existing template is broken --- Commands/InterviewTemplateCommands.cs | 54 +++++++++++++++++---------- Database.cs | 16 ++++---- 2 files changed, 44 insertions(+), 26 deletions(-) diff --git a/Commands/InterviewTemplateCommands.cs b/Commands/InterviewTemplateCommands.cs index 6c833c1..7b821e3 100644 --- a/Commands/InterviewTemplateCommands.cs +++ b/Commands/InterviewTemplateCommands.cs @@ -39,10 +39,14 @@ public class InterviewTemplateCommands return; } - string interviewTemplateJSON = Database.GetInterviewTemplateJSON(category.Id); - if (interviewTemplateJSON == null) + if (Database.TryGetInterviewTemplateJSON(category.Id, out string templateJSON)) { - string defaultTemplate = + MemoryStream stream = new(Encoding.UTF8.GetBytes(templateJSON)); + await command.RespondAsync(new DiscordInteractionResponseBuilder().AddFile("interview-template-" + category.Id + ".json", stream).AsEphemeral()); + return; + } + + string defaultTemplate = "{\n" + " \"category-id\": " + category.Id + ",\n" + " \"interview\":\n" + @@ -60,20 +64,14 @@ public class InterviewTemplateCommands " \n" + " }\n" + "}"; - MemoryStream stream = new(Encoding.UTF8.GetBytes(defaultTemplate)); + MemoryStream defStream = new(Encoding.UTF8.GetBytes(defaultTemplate)); - DiscordInteractionResponseBuilder response = new DiscordInteractionResponseBuilder().AddEmbed(new DiscordEmbedBuilder - { - Color = DiscordColor.Green, - Description = "No interview template found for this category. A default template has been generated." - }).AddFile("interview-template-" + category.Id + ".json", stream).AsEphemeral(); - await command.RespondAsync(response); - } - else + DiscordInteractionResponseBuilder response = new DiscordInteractionResponseBuilder().AddEmbed(new DiscordEmbedBuilder { - MemoryStream stream = new(Encoding.UTF8.GetBytes(interviewTemplateJSON)); - await command.RespondAsync(new DiscordInteractionResponseBuilder().AddFile("interview-template-" + category.Id + ".json", stream).AsEphemeral()); - } + Color = DiscordColor.Green, + Description = "No interview template found for this category. A default template has been generated." + }).AddFile("interview-template-" + category.Id + ".json", defStream).AsEphemeral(); + await command.RespondAsync(response); } [RequireGuild] @@ -199,9 +197,17 @@ public class InterviewTemplateCommands try { - MemoryStream memStream = new(Encoding.UTF8.GetBytes(Database.GetInterviewTemplateJSON(template.categoryID))); - await LogChannel.Success(command.User.Mention + " uploaded a new interview template for the `" + category.Name + "` category.", 0, - new Utilities.File("interview-template-" + template.categoryID + ".json", memStream)); + if (Database.TryGetInterviewTemplateJSON(template.categoryID, out string templateJSON)) + { + MemoryStream memStream = new(Encoding.UTF8.GetBytes(templateJSON)); + await LogChannel.Success(command.User.Mention + " uploaded a new interview template for the `" + category.Name + "` category.", 0, + new Utilities.File("interview-template-" + template.categoryID + ".json", memStream)); + } + else + { + Logger.Error("Unable to get interview template from database after upload."); + } + } catch (Exception e) { @@ -250,7 +256,17 @@ public class InterviewTemplateCommands return; } - MemoryStream memStream = new(Encoding.UTF8.GetBytes(Database.GetInterviewTemplateJSON(category.Id))); + if (!Database.TryGetInterviewTemplateJSON(category.Id, out string templateJSON)) + { + await command.RespondAsync(new DiscordEmbedBuilder + { + Color = DiscordColor.Red, + Description = "Something went wrong reading the interview template from the database." + }, true); + return; + } + + MemoryStream memStream = new(Encoding.UTF8.GetBytes(templateJSON)); if (!Database.TryDeleteInterviewTemplate(category.Id)) { await command.RespondAsync(new DiscordEmbedBuilder diff --git a/Database.cs b/Database.cs index cadfb2d..7f2b610 100644 --- a/Database.cs +++ b/Database.cs @@ -735,7 +735,7 @@ public static class Database } } - public static string GetInterviewTemplateJSON(ulong categoryID) + public static bool TryGetInterviewTemplateJSON(ulong categoryID, out string templateJSON) { using MySqlConnection c = GetConnection(); c.Open(); @@ -746,15 +746,16 @@ public static class Database if (!results.Read()) { - return null; + templateJSON = null; + return false; } - string templates = results.GetString("template"); + templateJSON = results.GetString("template"); results.Close(); - return templates; + return true; } - public static bool TryGetInterviewFromTemplate(ulong categoryID, ulong channelID, out Interviews.Interview interview) + public static bool TryGetInterviewFromTemplate(ulong categoryID, ulong channelID, out Interview interview) { using MySqlConnection c = GetConnection(); c.Open(); @@ -787,8 +788,9 @@ public static class Database interview = new Interview(channelID, template.interview, template.definitions); return true; } - catch (Exception) + catch (Exception e) { + Logger.Warn("Unable to create interview object from the current template for category '" + categoryID + "' in the database.", e); interview = null; return false; } @@ -807,7 +809,7 @@ public static class Database }); string query; - if (TryGetInterviewFromTemplate(template.categoryID, 0, out _)) + if (TryGetInterviewTemplateJSON(template.categoryID, out _)) { query = "UPDATE interview_templates SET template = @template WHERE category_id=@category_id"; }