Switch to always reading interviews directly from database

This commit is contained in:
Toastie 2024-12-26 23:33:32 +13:00
parent b2113695f8
commit 729e84fdce
Signed by: toastie_t0ast
GPG key ID: 27F3B6855AFD40A4
2 changed files with 3 additions and 25 deletions

View file

@ -323,13 +323,6 @@ public static class Interviewer
} }
} }
private static Dictionary<ulong, InterviewQuestion> activeInterviews = [];
public static void ReloadInterviews()
{
activeInterviews = Database.GetAllInterviews();
}
public static async void StartInterview(DiscordChannel channel) public static async void StartInterview(DiscordChannel channel)
{ {
if (channel.Parent == null) if (channel.Parent == null)
@ -352,20 +345,18 @@ public static class Interviewer
{ {
await CreateQuestion(channel, interview); await CreateQuestion(channel, interview);
Database.SaveInterview(channel.Id, interview); Database.SaveInterview(channel.Id, interview);
activeInterviews = Database.GetAllInterviews();
} }
} }
public static async Task RestartInterview(SlashCommandContext command) public static async Task RestartInterview(SlashCommandContext command)
{ {
if (activeInterviews.TryGetValue(command.Channel.Id, out InterviewQuestion interviewRoot)) if (Database.TryGetInterview(command.Channel.Id, out InterviewQuestion interviewRoot))
{ {
await DeletePreviousMessages(interviewRoot, command.Channel); await DeletePreviousMessages(interviewRoot, command.Channel);
if (!Database.TryDeleteInterview(command.Channel.Id)) if (!Database.TryDeleteInterview(command.Channel.Id))
{ {
Logger.Error("Could not delete interview from database. Channel ID: " + command.Channel.Id); Logger.Error("Could not delete interview from database. Channel ID: " + command.Channel.Id);
} }
ReloadInterviews();
} }
StartInterview(command.Channel); StartInterview(command.Channel);
} }
@ -385,7 +376,7 @@ public static class Interviewer
} }
// Return if there is no active interview in this channel // Return if there is no active interview in this channel
if (!activeInterviews.TryGetValue(interaction.Channel.Id, out InterviewQuestion interviewRoot)) if (!Database.TryGetInterview(interaction.Channel.Id, out InterviewQuestion interviewRoot))
{ {
await interaction.CreateResponseAsync(DiscordInteractionResponseType.ChannelMessageWithSource, new DiscordInteractionResponseBuilder() await interaction.CreateResponseAsync(DiscordInteractionResponseType.ChannelMessageWithSource, new DiscordInteractionResponseBuilder()
.AddEmbed(new DiscordEmbedBuilder() .AddEmbed(new DiscordEmbedBuilder()
@ -515,7 +506,7 @@ public static class Interviewer
} }
// The channel does not have an active interview. // The channel does not have an active interview.
if (!activeInterviews.TryGetValue(answerMessage.ReferencedMessage.Channel.Id, out InterviewQuestion interviewRoot)) if (!Database.TryGetInterview(answerMessage.ReferencedMessage.Channel.Id, out InterviewQuestion interviewRoot))
{ {
return; return;
} }
@ -638,7 +629,6 @@ 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);
} }
ReloadInterviews();
return; return;
case QuestionType.END_WITHOUT_SUMMARY: case QuestionType.END_WITHOUT_SUMMARY:
await channel.SendMessageAsync(new DiscordEmbedBuilder() await channel.SendMessageAsync(new DiscordEmbedBuilder()
@ -653,7 +643,6 @@ 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);
} }
ReloadInterviews();
break; break;
case QuestionType.ERROR: case QuestionType.ERROR:
default: default:

View file

@ -149,17 +149,6 @@ internal static class SupportChild
return false; return false;
} }
try
{
Logger.Log("Loading interviews from database...");
Interviewer.ReloadInterviews();
}
catch (Exception e)
{
Logger.Fatal("Could not load interviews from database. Error: ", e);
return false;
}
Logger.Log("Setting up Discord client..."); Logger.Log("Setting up Discord client...");
DiscordClientBuilder clientBuilder = DiscordClientBuilder.CreateDefault(Config.token, DiscordIntents.All) DiscordClientBuilder clientBuilder = DiscordClientBuilder.CreateDefault(Config.token, DiscordIntents.All)
.SetReconnectOnFatalGatewayErrors() .SetReconnectOnFatalGatewayErrors()