woomy-v2/bot/commands/Pokemon/item.js

82 lines
3.1 KiB
JavaScript

const fetch = require('node-fetch');
module.exports = class {
constructor (name, category) {
this.name = name,
this.category = category,
this.enabled = true,
this.devOnly = false,
this.aliases = [],
this.userPerms = [],
this.botPerms = [],
this.cooldown = 2000,
this.help = {
description: 'Gets information on a held item.',
arguments: '<item>',
details: '',
examples: 'item life orb\nitem griseous orb'
};
}
async run (client, message, args, data) { //eslint-disable-line no-unused-vars
if (!args[0]) return message.channel.send(
`${client.config.emojis.userError} You didn't give me an item to look up!`
);
const editMessage = await message.channel.send(`${client.config.emojis.loading} Please wait...`);
const query = args.join(' ').toLowerCase();
fetch('https://graphqlpokemon.favware.tech/', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'User-Agent': client.config.userAgent
},
body: JSON.stringify({ query: `
{
getItemDetailsByFuzzy(item: "${query}") {
name
desc
shortDesc
sprite
generationIntroduced
bulbapediaPage
serebiiPage
smogonPage
}
}
`})
})
.then((res) => res.json())
.then((json) => {
if (json.errors) {
json.errors.forEach(error => {
if (error.message.startsWith('Failed to get data for item')) {
message.channel.send(
`${client.config.emojis.userError} I couldn't find any items with names similar to ${query}. Check your spelling, maybe?`
);
} else {
client.logger.error('POKEMON_API_ERROR', error.message);
}
});
return;
}
const item = json.data.getItemDetailsByFuzzy;
const embed = new client.MessageEmbed()
.setColor(client.functions.embedColor(message.guild))
.setTitle(item.name)
.setThumbnail(item.sprite)
.addField('External Resources:', `[Bulbapedia](${item.bulbapediaPage}) • [Serebii](${item.serebiiPage}) • [Smogon](${item.smogonPage})`);
if (item.desc) {
embed.setDescription(`${item.desc} Added in Generation ${item.generationIntroduced}.`);
} else {
embed.setDescription(`${item.shortDesc} Added in Generation ${item.generationIntroduced}.`);
}
editMessage.edit({ content: null, embeds: [embed] });
});
}
};