From 525bd16bcc7f7cf53b33459615933caf1198656c Mon Sep 17 00:00:00 2001 From: Daniel Odendahl Jr Date: Mon, 26 Feb 2018 12:21:48 +0000 Subject: [PATCH] Improve neopets item code --- commands/events/doomsday-clock.js | 2 +- commands/search/neopets-item.js | 51 ++++++++++++++++++++++--------- 2 files changed, 37 insertions(+), 16 deletions(-) diff --git a/commands/events/doomsday-clock.js b/commands/events/doomsday-clock.js index ab70e49a..d27110f0 100644 --- a/commands/events/doomsday-clock.js +++ b/commands/events/doomsday-clock.js @@ -15,7 +15,7 @@ module.exports = class DoomsdayClockCommand extends Command { async run(msg) { try { const { text } = await snekfetch.get('https://thebulletin.org/timeline'); - const time = text.match(/IT IS(?: STILL)? (.+) MINUTES TO MIDNIGHT/)[0]; + const time = text.match(/IT IS (.+) MINUTES TO MIDNIGHT/)[0]; const desc = text.match(/
(.+)<\/span>: (.+)<\/div>/); return msg.say(stripIndents` **${time}** diff --git a/commands/search/neopets-item.js b/commands/search/neopets-item.js index cd6a6cf1..e6d02039 100644 --- a/commands/search/neopets-item.js +++ b/commands/search/neopets-item.js @@ -23,28 +23,49 @@ module.exports = class NeopetItemCommand extends Command { async run(msg, { item }) { try { - const search = await snekfetch - .get('https://items.jellyneo.net/search/') - .query({ - name: item, - name_type: 3 - }); - const id = search.text.match(/\/item\/([0-9]+)/); - if (!id) return msg.say('Could not find any results.'); - const { text } = await snekfetch.get(`https://items.jellyneo.net/item/${id[1]}/`); - const price = search.text.match(/[0-9,]+ (NP|NC)/); + const search = await this.fetchItem(item); + if (!search) return msg.say('Could not find any results'); + const data = await this.fetchItemDetails(search); const embed = new MessageEmbed() .setColor(0xFFCE31) .setAuthor('Neopets', 'https://i.imgur.com/BP8qxJH.png', 'http://www.neopets.com/') - .setTitle(text.match(/

(.+)<\/h1>/)[1]) - .setDescription(text.match(/(.+)<\/em>/)[1]) - .setURL(`https://items.jellyneo.net/item/${id[1]}/`) - .setThumbnail(`https://items.jellyneo.net/assets/imgs/items/${id[1]}.gif`) + .setTitle(data.name) + .setDescription(data.details) + .setURL(data.url) + .setThumbnail(data.image) .addField('❯ Price', - price ? price[0] : 'Unavailable'); + data.price ? `${data.price} ${data.currency}` : 'Not for Sale'); return msg.embed(embed); } catch (err) { return msg.reply(`Oh no, an error occurred: \`${err.message}\`. Try again later!`); } } + + async fetchItem(query) { + const { text } = await snekfetch + .get('https://items.jellyneo.net/search/') + .query({ + name: query, + name_type: 3 + }); + const id = text.match(/\/item\/([0-9]+)/); + if (!id) return null; + const price = text.match(/([0-9,]+) (NP|NC)/); + return { + id: id[1], + url: `https://items.jellyneo.net/item/${id[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] + }; + } };