Improve neopets item code

This commit is contained in:
Daniel Odendahl Jr
2018-02-26 12:21:48 +00:00
parent 3a68ec1029
commit 525bd16bcc
2 changed files with 37 additions and 16 deletions
+36 -15
View File
@@ -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>(.+)<\/h1>/)[1])
.setDescription(text.match(/<em>(.+)<\/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>(.+)<\/h1>/)[1],
details: text.match(/<em>(.+)<\/em>/)[1]
};
}
};