diff --git a/Commands/AdminCommands.cs b/Commands/AdminCommands.cs index 8c1beed..b7fcfeb 100644 --- a/Commands/AdminCommands.cs +++ b/Commands/AdminCommands.cs @@ -297,7 +297,6 @@ public class AdminCommands } Database.SetInterviewTemplates(JsonConvert.SerializeObject(interview, Formatting.Indented)); - Interviewer.Reload(); } catch (Exception e) { diff --git a/Database.cs b/Database.cs index 9808c63..a00323a 100644 --- a/Database.cs +++ b/Database.cs @@ -751,7 +751,8 @@ public static class Database return templates; } - public static Dictionary GetInterviewTemplates() + // Still returns true if there are no templates, returns false if the templates are invalid. + public static bool TryGetInterviewTemplates(out Dictionary templates) { using MySqlConnection c = GetConnection(); c.Open(); @@ -762,21 +763,31 @@ public static class Database // Check if messages exist in the database if (!results.Read()) { - return new Dictionary(); + templates = new Dictionary(); + return true; } - string templates = results.GetString("interview"); + string templatesString = results.GetString("interview"); results.Close(); - return JsonConvert.DeserializeObject>(templates, new JsonSerializerSettings + try { - Error = delegate (object sender, ErrorEventArgs args) + templates = JsonConvert.DeserializeObject>(templatesString, new JsonSerializerSettings { - Logger.Error("Error occured when trying to read interview templates from database: " + args.ErrorContext.Error.Message); - Logger.Debug("Detailed exception:", args.ErrorContext.Error); - args.ErrorContext.Handled = false; - } - }); + Error = delegate (object sender, ErrorEventArgs args) + { + Logger.Error("Error occured when trying to read interview templates from database: " + args.ErrorContext.Error.Message); + Logger.Debug("Detailed exception:", args.ErrorContext.Error); + args.ErrorContext.Handled = false; + } + }); + return true; + } + catch (Exception) + { + templates = null; + return false; + } } public static bool SetInterviewTemplates(string templates) @@ -833,7 +844,7 @@ public static class Database } catch (Exception e) { - Logger.Error("Error occured when trying to read interview from database, it will not be loaded until manually fixed in the database.\nError message: " + e.Message); + Logger.Warn("Error occured when trying to read interview from database, it will not be loaded until manually fixed in the database.\nError message: " + e.Message); Logger.Debug("Detailed exception:", e); } diff --git a/Interviewer.cs b/Interviewer.cs index 875f7a9..3f548d2 100644 --- a/Interviewer.cs +++ b/Interviewer.cs @@ -40,8 +40,6 @@ public static class Interviewer // The entire interview tree is serialized and stored in the database in order to record responses as they are made. public class InterviewQuestion { - // TODO: Other selector types. - // Title of the message embed. [JsonProperty("title")] public string title; @@ -220,14 +218,10 @@ public static class Interviewer public Dictionary paths; } - private static Dictionary interviewTemplates = []; - private static Dictionary activeInterviews = []; - // TODO: Maybe split into two functions? - public static void Reload() + public static void ReloadInterviews() { - interviewTemplates = Database.GetInterviewTemplates(); activeInterviews = Database.GetAllInterviews(); } @@ -238,11 +232,22 @@ public static class Interviewer return; } - if (interviewTemplates.TryGetValue(channel.Parent.Id, out InterviewQuestion interview)) + if (!Database.TryGetInterviewTemplates(out Dictionary templates)) + { + await channel.SendMessageAsync(new DiscordEmbedBuilder + { + Description = "Attempted to create interview from template, but an error occured when reading it from the database.\n\n" + + "Tell a staff member to check the bot log and fix the template.", + Color = DiscordColor.Red + }); + return; + } + + if (templates.TryGetValue(channel.Parent.Id, out InterviewQuestion interview)) { await CreateQuestion(channel, interview); Database.SaveInterview(channel.Id, interview); - Reload(); + activeInterviews = Database.GetAllInterviews(); } } @@ -462,7 +467,7 @@ public static class Interviewer { Logger.Error("Could not delete interview from database. Channel ID: " + channel.Id); } - Reload(); + ReloadInterviews(); return; case QuestionType.END_WITHOUT_SUMMARY: // TODO: Add command to restart interview. @@ -478,7 +483,7 @@ public static class Interviewer { Logger.Error("Could not delete interview from database. Channel ID: " + channel.Id); } - Reload(); + ReloadInterviews(); break; case QuestionType.ERROR: default: diff --git a/SupportChild.cs b/SupportChild.cs index 9c6930f..8bff6e9 100644 --- a/SupportChild.cs +++ b/SupportChild.cs @@ -152,6 +152,7 @@ internal static class SupportChild try { Logger.Log("Loading interviews from database..."); + Interviewer.ReloadInterviews(); } catch (Exception e) {