One function to rule them all

This commit is contained in:
Daniel Odendahl Jr
2018-02-26 12:24:56 +00:00
parent 525bd16bcc
commit 34c751d4a2
+7 -13
View File
@@ -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>(.+)<\/h1>/)[1],
details: details.text.match(/<em>(.+)<\/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>(.+)<\/h1>/)[1],
details: text.match(/<em>(.+)<\/em>/)[1]
};
}
};