Get templates from database every time a new interview is started to avoid issues with the templates being passed by reference and then modified accidentally
This commit is contained in:
parent
1ced350789
commit
d9fcef449a
4 changed files with 39 additions and 23 deletions
|
@ -297,7 +297,6 @@ public class AdminCommands
|
||||||
}
|
}
|
||||||
|
|
||||||
Database.SetInterviewTemplates(JsonConvert.SerializeObject(interview, Formatting.Indented));
|
Database.SetInterviewTemplates(JsonConvert.SerializeObject(interview, Formatting.Indented));
|
||||||
Interviewer.Reload();
|
|
||||||
}
|
}
|
||||||
catch (Exception e)
|
catch (Exception e)
|
||||||
{
|
{
|
||||||
|
|
21
Database.cs
21
Database.cs
|
@ -751,7 +751,8 @@ public static class Database
|
||||||
return templates;
|
return templates;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static Dictionary<ulong, Interviewer.InterviewQuestion> GetInterviewTemplates()
|
// Still returns true if there are no templates, returns false if the templates are invalid.
|
||||||
|
public static bool TryGetInterviewTemplates(out Dictionary<ulong, Interviewer.InterviewQuestion> templates)
|
||||||
{
|
{
|
||||||
using MySqlConnection c = GetConnection();
|
using MySqlConnection c = GetConnection();
|
||||||
c.Open();
|
c.Open();
|
||||||
|
@ -762,13 +763,16 @@ public static class Database
|
||||||
// Check if messages exist in the database
|
// Check if messages exist in the database
|
||||||
if (!results.Read())
|
if (!results.Read())
|
||||||
{
|
{
|
||||||
return new Dictionary<ulong, Interviewer.InterviewQuestion>();
|
templates = new Dictionary<ulong, Interviewer.InterviewQuestion>();
|
||||||
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
string templates = results.GetString("interview");
|
string templatesString = results.GetString("interview");
|
||||||
results.Close();
|
results.Close();
|
||||||
|
|
||||||
return JsonConvert.DeserializeObject<Dictionary<ulong, Interviewer.InterviewQuestion>>(templates, new JsonSerializerSettings
|
try
|
||||||
|
{
|
||||||
|
templates = JsonConvert.DeserializeObject<Dictionary<ulong, Interviewer.InterviewQuestion>>(templatesString, new JsonSerializerSettings
|
||||||
{
|
{
|
||||||
Error = delegate (object sender, ErrorEventArgs args)
|
Error = delegate (object sender, ErrorEventArgs args)
|
||||||
{
|
{
|
||||||
|
@ -777,6 +781,13 @@ public static class Database
|
||||||
args.ErrorContext.Handled = false;
|
args.ErrorContext.Handled = false;
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
catch (Exception)
|
||||||
|
{
|
||||||
|
templates = null;
|
||||||
|
return false;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public static bool SetInterviewTemplates(string templates)
|
public static bool SetInterviewTemplates(string templates)
|
||||||
|
@ -833,7 +844,7 @@ public static class Database
|
||||||
}
|
}
|
||||||
catch (Exception e)
|
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);
|
Logger.Debug("Detailed exception:", e);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -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.
|
// The entire interview tree is serialized and stored in the database in order to record responses as they are made.
|
||||||
public class InterviewQuestion
|
public class InterviewQuestion
|
||||||
{
|
{
|
||||||
// TODO: Other selector types.
|
|
||||||
|
|
||||||
// Title of the message embed.
|
// Title of the message embed.
|
||||||
[JsonProperty("title")]
|
[JsonProperty("title")]
|
||||||
public string title;
|
public string title;
|
||||||
|
@ -220,14 +218,10 @@ public static class Interviewer
|
||||||
public Dictionary<string, ValidatedInterviewQuestion> paths;
|
public Dictionary<string, ValidatedInterviewQuestion> paths;
|
||||||
}
|
}
|
||||||
|
|
||||||
private static Dictionary<ulong, InterviewQuestion> interviewTemplates = [];
|
|
||||||
|
|
||||||
private static Dictionary<ulong, InterviewQuestion> activeInterviews = [];
|
private static Dictionary<ulong, InterviewQuestion> activeInterviews = [];
|
||||||
|
|
||||||
// TODO: Maybe split into two functions?
|
public static void ReloadInterviews()
|
||||||
public static void Reload()
|
|
||||||
{
|
{
|
||||||
interviewTemplates = Database.GetInterviewTemplates();
|
|
||||||
activeInterviews = Database.GetAllInterviews();
|
activeInterviews = Database.GetAllInterviews();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -238,11 +232,22 @@ public static class Interviewer
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (interviewTemplates.TryGetValue(channel.Parent.Id, out InterviewQuestion interview))
|
if (!Database.TryGetInterviewTemplates(out Dictionary<ulong, InterviewQuestion> 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);
|
await CreateQuestion(channel, interview);
|
||||||
Database.SaveInterview(channel.Id, 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);
|
Logger.Error("Could not delete interview from database. Channel ID: " + channel.Id);
|
||||||
}
|
}
|
||||||
Reload();
|
ReloadInterviews();
|
||||||
return;
|
return;
|
||||||
case QuestionType.END_WITHOUT_SUMMARY:
|
case QuestionType.END_WITHOUT_SUMMARY:
|
||||||
// TODO: Add command to restart interview.
|
// 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);
|
Logger.Error("Could not delete interview from database. Channel ID: " + channel.Id);
|
||||||
}
|
}
|
||||||
Reload();
|
ReloadInterviews();
|
||||||
break;
|
break;
|
||||||
case QuestionType.ERROR:
|
case QuestionType.ERROR:
|
||||||
default:
|
default:
|
||||||
|
|
|
@ -152,6 +152,7 @@ internal static class SupportChild
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
Logger.Log("Loading interviews from database...");
|
Logger.Log("Loading interviews from database...");
|
||||||
|
Interviewer.ReloadInterviews();
|
||||||
}
|
}
|
||||||
catch (Exception e)
|
catch (Exception e)
|
||||||
{
|
{
|
||||||
|
|
Loading…
Reference in a new issue