Files
xiao/commands/search/country.js
T
Dragon Fire 6581912013 Fix
2020-03-27 12:12:38 -04:00

55 lines
1.8 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
const Command = require('../../structures/Command');
const { MessageEmbed } = require('discord.js');
const request = require('node-superfetch');
const { formatNumber } = require('../../util/Util');
module.exports = class CountryCommand extends Command {
constructor(client) {
super(client, {
name: 'country',
group: 'search',
memberName: 'country',
description: 'Responds with information on a country.',
clientPermissions: ['EMBED_LINKS'],
credit: [
{
name: 'Rest Countries',
url: 'https://restcountries.eu/',
reason: 'API'
}
],
args: [
{
key: 'query',
prompt: 'What country would you like to search for?',
type: 'string',
parse: query => encodeURIComponent(query)
}
]
});
}
async run(msg, { query }) {
try {
const { body } = await request.get(`https://restcountries.eu/rest/v2/name/${query}`);
const data = body[0];
const embed = new MessageEmbed()
.setColor(0x00AE86)
.setTitle(data.name)
.setThumbnail(`https://www.countryflags.io/${data.alpha2Code}/flat/64.png`)
.addField(' Population', formatNumber(data.population), true)
.addField(' Capital', data.capital, true)
.addField(' Currency', data.currencies[0].symbol, true)
.addField(' Location', data.subregion || data.region, true)
.addField(' Demonym', data.demonym, true)
.addField(' Native Name', data.nativeName, true)
.addField(' Area', `${formatNumber(data.area)}km`, true)
.addField(' Languages', data.languages.map(lang => lang.name).join('/'));
return msg.embed(embed);
} catch (err) {
if (err.status === 404) return msg.say('Could not find any results.');
return msg.reply(`Oh no, an error occurred: \`${err.message}\`. Try again later!`);
}
}
};