From f0b744cbb6435a0399d7a57ea98f840191669589 Mon Sep 17 00:00:00 2001 From: Dragon Fire Date: Wed, 16 Jun 2021 21:18:11 -0400 Subject: [PATCH] Fix recipe --- commands/search/recipe.js | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) diff --git a/commands/search/recipe.js b/commands/search/recipe.js index ecc6f90c..7f9d23ac 100644 --- a/commands/search/recipe.js +++ b/commands/search/recipe.js @@ -31,10 +31,7 @@ module.exports = class RecipeCommand extends Command { async run(msg, { query }) { try { - const { text } = await request - .get('http://www.recipepuppy.com/api/') - .query({ q: query }); - const body = JSON.parse(text); + const body = await this.fetchRecipe(query); if (!body.results.length) return msg.say('Could not find any results.'); const recipe = body.results[Math.floor(Math.random() * body.results.length)]; const embed = new MessageEmbed() @@ -46,8 +43,20 @@ module.exports = class RecipeCommand extends Command { .setThumbnail(recipe.thumbnail); return msg.embed(embed); } catch (err) { - if (err.status === 500) return msg.say('Could not find any results.'); return msg.reply(`Oh no, an error occurred: \`${err.message}\`. Try again later!`); } } + + async fetchRecipe(query) { + let result; + try { + const { text } = await request + .get('http://www.recipepuppy.com/api/') + .query({ q: query }); + result = text; + return JSON.parse(result); + } catch { + return JSON.parse(result.split('