Files
xiao/commands/search/weather.js
T
Daniel Odendahl Jr 6b567bac26 Changes are fun
2017-10-11 16:55:28 +00:00

67 lines
2.2 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('discord.js-commando');
const { MessageEmbed } = require('discord.js');
const snekfetch = require('snekfetch');
const { OWM_KEY } = process.env;
module.exports = class WeatherCommand extends Command {
constructor(client) {
super(client, {
name: 'weather',
aliases: ['open-weather-map'],
group: 'search',
memberName: 'weather',
description: 'Responds with weather information for a specified location.',
clientPermissions: ['EMBED_LINKS'],
args: [
{
key: 'query',
prompt: 'What location would you like to get the weather of?',
type: 'string'
}
]
});
}
async run(msg, { query }) {
try {
const { body } = await snekfetch
.get('http://api.openweathermap.org/data/2.5/weather')
.query({
q: query,
units: 'metric',
appid: OWM_KEY
});
const embed = new MessageEmbed()
.setColor(0xFF7A09)
.setAuthor('OpenWeatherMap', 'https://i.imgur.com/tUd1MYB.png')
.setURL(`https://openweathermap.org/city/${body.id}`)
.setThumbnail(body.weather[0].icon ? `http://openweathermap.org/img/w/${body.weather[0].icon}.png` : null)
.setTimestamp()
.addField(' City',
body.name, true)
.addField(' Country',
body.sys.country, true)
.addField(' Condition',
body.weather.map(cond => `${cond.main} (${cond.description})`).join('\n'), true)
.addField(' Temperature',
body.main.temp ? `${body.main.temp}°C` : 'N/A', true)
.addField(' Humidity',
body.main.humidity ? `${body.main.humidity}%` : 'N/A', true)
.addField(' Pressure',
body.main.pressure ? `${body.main.pressure} hPa` : 'N/A', true)
.addField(' Visibility',
body.visibility ? `${body.visibility}m` : 'N/A', true)
.addField(' Cloudiness',
body.clouds && body.clouds.all ? `${body.clouds.all}%` : 'N/A', true)
.addField(' Wind Direction',
body.wind && body.wind.deg ? `${body.wind.deg}°` : 'N/A', true)
.addField(' Wind Speed',
body.wind && body.wind.speed ? `${body.wind.speed}m/s` : 'N/A', true);
return msg.embed(embed);
} catch (err) {
if (err.status === 404) return msg.say('Could not find any results.');
return msg.say(`Oh no, an error occurred: \`${err.message}\`. Try again later!`);
}
}
};