Fixed setting templates if the existing template is broken
This commit is contained in:
parent
91652eb8fb
commit
1a79ae1d23
2 changed files with 44 additions and 26 deletions
|
@ -39,10 +39,14 @@ public class InterviewTemplateCommands
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
string interviewTemplateJSON = Database.GetInterviewTemplateJSON(category.Id);
|
if (Database.TryGetInterviewTemplateJSON(category.Id, out string templateJSON))
|
||||||
if (interviewTemplateJSON == null)
|
|
||||||
{
|
{
|
||||||
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" +
|
"{\n" +
|
||||||
" \"category-id\": " + category.Id + ",\n" +
|
" \"category-id\": " + category.Id + ",\n" +
|
||||||
" \"interview\":\n" +
|
" \"interview\":\n" +
|
||||||
|
@ -60,20 +64,14 @@ public class InterviewTemplateCommands
|
||||||
" \n" +
|
" \n" +
|
||||||
" }\n" +
|
" }\n" +
|
||||||
"}";
|
"}";
|
||||||
MemoryStream stream = new(Encoding.UTF8.GetBytes(defaultTemplate));
|
MemoryStream defStream = new(Encoding.UTF8.GetBytes(defaultTemplate));
|
||||||
|
|
||||||
DiscordInteractionResponseBuilder response = new DiscordInteractionResponseBuilder().AddEmbed(new DiscordEmbedBuilder
|
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
|
|
||||||
{
|
{
|
||||||
MemoryStream stream = new(Encoding.UTF8.GetBytes(interviewTemplateJSON));
|
Color = DiscordColor.Green,
|
||||||
await command.RespondAsync(new DiscordInteractionResponseBuilder().AddFile("interview-template-" + category.Id + ".json", stream).AsEphemeral());
|
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]
|
[RequireGuild]
|
||||||
|
@ -199,9 +197,17 @@ public class InterviewTemplateCommands
|
||||||
|
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
MemoryStream memStream = new(Encoding.UTF8.GetBytes(Database.GetInterviewTemplateJSON(template.categoryID)));
|
if (Database.TryGetInterviewTemplateJSON(template.categoryID, out string 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));
|
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)
|
catch (Exception e)
|
||||||
{
|
{
|
||||||
|
@ -250,7 +256,17 @@ public class InterviewTemplateCommands
|
||||||
return;
|
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))
|
if (!Database.TryDeleteInterviewTemplate(category.Id))
|
||||||
{
|
{
|
||||||
await command.RespondAsync(new DiscordEmbedBuilder
|
await command.RespondAsync(new DiscordEmbedBuilder
|
||||||
|
|
16
Database.cs
16
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();
|
using MySqlConnection c = GetConnection();
|
||||||
c.Open();
|
c.Open();
|
||||||
|
@ -746,15 +746,16 @@ public static class Database
|
||||||
|
|
||||||
if (!results.Read())
|
if (!results.Read())
|
||||||
{
|
{
|
||||||
return null;
|
templateJSON = null;
|
||||||
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
string templates = results.GetString("template");
|
templateJSON = results.GetString("template");
|
||||||
results.Close();
|
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();
|
using MySqlConnection c = GetConnection();
|
||||||
c.Open();
|
c.Open();
|
||||||
|
@ -787,8 +788,9 @@ public static class Database
|
||||||
interview = new Interview(channelID, template.interview, template.definitions);
|
interview = new Interview(channelID, template.interview, template.definitions);
|
||||||
return true;
|
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;
|
interview = null;
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
@ -807,7 +809,7 @@ public static class Database
|
||||||
});
|
});
|
||||||
|
|
||||||
string query;
|
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";
|
query = "UPDATE interview_templates SET template = @template WHERE category_id=@category_id";
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue