Fixed setting templates if the existing template is broken

This commit is contained in:
Toastie 2025-02-04 20:10:29 +13:00
parent 91652eb8fb
commit 1a79ae1d23
Signed by: toastie_t0ast
GPG key ID: 0861BE54AD481DC7
2 changed files with 44 additions and 26 deletions

View file

@ -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

View file

@ -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";
}