From 34c751d4a2bbf7eb8f59569e290fee6b2d875a8f Mon Sep 17 00:00:00 2001 From: Daniel Odendahl Jr Date: Mon, 26 Feb 2018 12:24:56 +0000 Subject: [PATCH] One function to rule them all --- commands/search/neopets-item.js | 20 +++++++------------- 1 file changed, 7 insertions(+), 13 deletions(-) diff --git a/commands/search/neopets-item.js b/commands/search/neopets-item.js index e6d02039..20625ecd 100644 --- a/commands/search/neopets-item.js +++ b/commands/search/neopets-item.js @@ -23,9 +23,8 @@ module.exports = class NeopetItemCommand extends Command { async run(msg, { item }) { try { - const search = await this.fetchItem(item); - if (!search) return msg.say('Could not find any results'); - const data = await this.fetchItemDetails(search); + const data = await this.fetchItem(item); + if (!data) return msg.say('Could not find any results'); const embed = new MessageEmbed() .setColor(0xFFCE31) .setAuthor('Neopets', 'https://i.imgur.com/BP8qxJH.png', 'http://www.neopets.com/') @@ -51,21 +50,16 @@ module.exports = class NeopetItemCommand extends Command { const id = text.match(/\/item\/([0-9]+)/); if (!id) return null; const price = text.match(/([0-9,]+) (NP|NC)/); + const url = `https://items.jellyneo.net/item/${id[1]}/`; + const details = await snekfetch.get(url); return { id: id[1], - url: `https://items.jellyneo.net/item/${id[1]}/`, + url, + name: details.text.match(/

(.+)<\/h1>/)[1], + details: details.text.match(/(.+)<\/em>/)[1], image: `https://items.jellyneo.net/assets/imgs/items/${id[1]}.gif`, price: price ? Number.parseInt(price[1].replace(/,/g, ''), 10) : null, currency: price ? price[2] : null }; } - - async fetchItemDetails(item) { - const { text } = await snekfetch.get(item.url); - return { - ...item, - name: text.match(/

(.+)<\/h1>/)[1], - details: text.match(/(.+)<\/em>/)[1] - }; - } };