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

85 lines
3.2 KiB
JavaScript
Raw Normal View History

2020-11-04 06:23:23 +00:00
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 = {
2020-11-04 07:17:31 +00:00
description: 'Get data on a Pokemon ability.',
arguments: '<ability>',
2020-11-04 06:23:23 +00:00
details: '',
2020-11-04 07:17:31 +00:00
examples: '`ability intimidate`\n`ability moxie`'
2020-11-04 06:23:23 +00:00
};
}
2021-07-17 05:55:18 +00:00
async run (client, message, args, data) { //eslint-disable-line no-unused-vars
if (!args[0]) return message.channel.send(
2021-07-15 02:26:42 +00:00
`${client.config.emojis.userError} You didn't give me an ability to look up!`
2020-11-04 06:23:23 +00:00
);
2021-07-17 05:55:18 +00:00
const editMessage = await message.channel.send(`${client.config.emojis.loading} Please wait...`);
2020-11-04 06:23:23 +00:00
const query = args.join(' ').toLowerCase();
fetch('https://graphqlpokemon.favware.tech/', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'User-Agent': client.config.userAgent
2020-11-04 06:23:23 +00:00
},
body: JSON.stringify({ query: `
{
getAbilityDetailsByFuzzy(ability: "${query}") {
name
desc
2020-11-07 00:12:49 +00:00
shortDesc
2020-11-04 06:23:23 +00:00
bulbapediaPage
serebiiPage
smogonPage
2020-11-10 00:50:06 +00:00
isFieldAbility
2020-11-04 06:23:23 +00:00
}
}
`})
})
.then((res) => res.json())
.then((json) => {
if (json.errors) {
json.errors.forEach(error => {
if (error.message.startsWith('Failed to get data for ability')) {
2021-07-17 05:55:18 +00:00
editMessage.edit(
2021-07-15 02:26:42 +00:00
`${client.config.emojis.userError} I couldn't find any abilities with names similar to ${query}. Check your spelling, maybe?`
2020-11-04 06:23:23 +00:00
);
} else {
client.logger.error('POKEMON_API_ERROR', error.message);
2020-11-04 06:23:23 +00:00
}
});
return;
}
const ability = json.data.getAbilityDetailsByFuzzy;
2020-11-10 00:50:06 +00:00
let fieldEffects = '';
if (ability.isFieldAbility) {
fieldEffects = ` Outside of battle, ${ability.isFieldAbility}`;
}
2021-07-17 05:55:18 +00:00
const embed = new client.MessageEmbed()
.setColor(client.functions.embedColor(message.guild))
2020-11-07 00:12:49 +00:00
.setTitle(ability.name.toProperCase());
if (ability.desc) {
2020-11-10 00:50:06 +00:00
embed.setDescription(ability.desc + fieldEffects);
2020-11-07 00:12:49 +00:00
} else {
2020-11-10 00:50:06 +00:00
embed.setDescription(ability.shortDesc + fieldEffects);
2020-11-07 00:12:49 +00:00
}
embed.addField('External Resources:', `[Bulbapedia](${ability.bulbapediaPage}) • [Serebii](${ability.serebiiPage}) • [Smogon](${ability.smogonPage})`);
2021-07-17 05:55:18 +00:00
editMessage.edit({ content: null, embeds: [embed] });
});
2020-11-04 06:23:23 +00:00
}
};