Skip to content

Commit

Permalink
the embedbuilder can fail cause of bad recipes, catch it and remove t…
Browse files Browse the repository at this point in the history
…hem from the db
  • Loading branch information
xiaoxiao921 committed Sep 20, 2021
1 parent 1052979 commit d86700b
Showing 1 changed file with 20 additions and 1 deletion.
21 changes: 20 additions & 1 deletion CHEF/Components/Commands/Cooking/CookModule.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,12 +50,31 @@ private async Task ListRecipesInternal(SocketUser owner = null, int page = 1, st
{
(recipes, totalRecipeCount) = await context.GetRecipes(cmdName, page - 1, owner);
}

var badRecipes = new List<Recipe>();
var embedBuilder = new EmbedBuilder();
foreach (var recipe in recipes)
{
var recipeText = recipe.Text;
var recipeTextLength = recipeText.Length > descMaxLength ? descMaxLength : recipeText.Length;
embedBuilder.AddField($"{recipe.Name}", $"{recipeText.Substring(0, recipeTextLength)}");

try
{
embedBuilder.AddField($"{recipe.Name}", $"{recipeText.Substring(0, recipeTextLength)}");
}
catch (Exception)
{
badRecipes.Add(recipe);
}
}

using (var context = new RecipeContext())
{
context.RemoveRange(badRecipes);

await context.SaveChangesAsync();

Logger.Log($"Removed {badRecipes.Count} bad recipes.");
}

if (recipes.Count > 0)
Expand Down

0 comments on commit d86700b

Please sign in to comment.