mirror of
https://github.com/arthur-pbty/xiao.git
synced 2026-06-26 14:19:11 +02:00
Fix recipe
This commit is contained in:
@@ -31,10 +31,7 @@ module.exports = class RecipeCommand extends Command {
|
|||||||
|
|
||||||
async run(msg, { query }) {
|
async run(msg, { query }) {
|
||||||
try {
|
try {
|
||||||
const { text } = await request
|
const body = await this.fetchRecipe(query);
|
||||||
.get('http://www.recipepuppy.com/api/')
|
|
||||||
.query({ q: query });
|
|
||||||
const body = JSON.parse(text);
|
|
||||||
if (!body.results.length) return msg.say('Could not find any results.');
|
if (!body.results.length) return msg.say('Could not find any results.');
|
||||||
const recipe = body.results[Math.floor(Math.random() * body.results.length)];
|
const recipe = body.results[Math.floor(Math.random() * body.results.length)];
|
||||||
const embed = new MessageEmbed()
|
const embed = new MessageEmbed()
|
||||||
@@ -46,8 +43,20 @@ module.exports = class RecipeCommand extends Command {
|
|||||||
.setThumbnail(recipe.thumbnail);
|
.setThumbnail(recipe.thumbnail);
|
||||||
return msg.embed(embed);
|
return msg.embed(embed);
|
||||||
} catch (err) {
|
} 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!`);
|
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('<!DOCTYPE html')[0]);
|
||||||
|
}
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|||||||
Reference in New Issue
Block a user