Change Weather to use OpenWeatherMap

This commit is contained in:
Daniel Odendahl Jr
2017-09-06 16:08:14 +00:00
parent 60011f59a0
commit e5eeef91f7
3 changed files with 37 additions and 111 deletions
-87
View File
@@ -1,87 +0,0 @@
const Command = require('../../structures/Command');
const { stripIndents } = require('common-tags');
const { MessageEmbed } = require('discord.js');
const snekfetch = require('snekfetch');
module.exports = class ForecastCommand extends Command {
constructor(client) {
super(client, {
name: 'forecast',
group: 'search',
memberName: 'forecast',
description: 'Responds with the seven-day forecast for a specified location.',
clientPermissions: ['EMBED_LINKS'],
args: [
{
key: 'query',
prompt: 'What location would you like to get the forecast for?',
type: 'string'
}
]
});
}
async run(msg, args) {
const { query } = args;
try {
const { body } = await snekfetch
.get('https://query.yahooapis.com/v1/public/yql')
.query({
q: `select * from weather.forecast where u='f' AND woeid in (select woeid from geo.places(1) where text="${query}")`, // eslint-disable-line max-len
format: 'json'
});
if (!body.query.count) return msg.say('Could not find any results.');
const forecasts = body.query.results.channel.item.forecast;
const embed = new MessageEmbed()
.setColor(0x0000FF)
.setAuthor(body.query.results.channel.title, 'https://i.imgur.com/2MT0ViC.png')
.setURL(body.query.results.channel.link)
.setTimestamp()
.addField(` ${forecasts[0].day} - ${forecasts[0].date}`,
stripIndents`
**High:** ${forecasts[0].high}°F
**Low:** ${forecasts[0].low}°F
**Condition:** ${forecasts[0].text}
`)
.addField(` ${forecasts[1].day} - ${forecasts[1].date}`,
stripIndents`
**High:** ${forecasts[1].high}°F
**Low:** ${forecasts[1].low}°F
**Condition:** ${forecasts[1].text}
`)
.addField(` ${forecasts[2].day} - ${forecasts[2].date}`,
stripIndents`
**High:** ${forecasts[2].high}°F
**Low:** ${forecasts[2].low}°F
**Condition:** ${forecasts[2].text}
`)
.addField(` ${forecasts[3].day} - ${forecasts[3].date}`,
stripIndents`
**High:** ${forecasts[3].high}°F
**Low:** ${forecasts[3].low}°F
**Condition:** ${forecasts[3].text}
`)
.addField(` ${forecasts[4].day} - ${forecasts[4].date}`,
stripIndents`
**High:** ${forecasts[4].high}°F
**Low:** ${forecasts[4].low}°F
**Condition:** ${forecasts[4].text}
`)
.addField(` ${forecasts[5].day} - ${forecasts[5].date}`,
stripIndents`
**High:** ${forecasts[5].high}°F
**Low:** ${forecasts[5].low}°F
**Condition:** ${forecasts[5].text}
`)
.addField(` ${forecasts[6].day} - ${forecasts[6].date}`,
stripIndents`
**High:** ${forecasts[6].high}°F
**Low:** ${forecasts[6].low}°F
**Condition:** ${forecasts[6].text}
`);
return msg.embed(embed);
} catch (err) {
return msg.say(`Oh no, an error occurred: \`${err.message}\`. Try again later!`);
}
}
};
+36 -23
View File
@@ -1,6 +1,9 @@
const Command = require('../../structures/Command');
const { MessageEmbed } = require('discord.js');
const snekfetch = require('snekfetch');
const { list } = require('../../structures/Util');
const { OWM_KEY } = process.env;
const types = ['zip', 'name', 'lat/lon'];
module.exports = class WeatherCommand extends Command {
constructor(client) {
@@ -11,6 +14,16 @@ module.exports = class WeatherCommand extends Command {
description: 'Responds with weather information for a specified location.',
clientPermissions: ['EMBED_LINKS'],
args: [
{
key: 'type',
prompt: `What type should the search be? Either ${list(types, 'or')}.`,
type: 'string',
validate: type => {
if (types.includes(type)) return true;
return `Invalid type, please enter either ${list(types, 'or')}.`;
},
parse: type => type.toLowerCase()
},
{
key: 'query',
prompt: 'What location would you like to get the weather of?',
@@ -21,46 +34,46 @@ module.exports = class WeatherCommand extends Command {
}
async run(msg, args) {
const { query } = args;
const { type, query } = args;
try {
const { body } = await snekfetch
.get('https://query.yahooapis.com/v1/public/yql')
.get('http://api.openweathermap.org/data/2.5/weather')
.query({
q: `select * from weather.forecast where u='f' AND woeid in (select woeid from geo.places(1) where text="${query}")`, // eslint-disable-line max-len
format: 'json'
q: type === 'name' ? query : '',
zip: type === 'zip' ? query : '',
lat: type === 'lat/lon' ? query.split('/')[0] : '',
lon: type === 'lat/lon' ? query.split('/')[1] : '',
units: 'metric',
appid: OWM_KEY
});
if (!body.query.count) return msg.say('Could not find any results.');
const embed = new MessageEmbed()
.setColor(0x0000FF)
.setAuthor(body.query.results.channel.title, 'https://i.imgur.com/2MT0ViC.png')
.setURL(body.query.results.channel.link)
.setColor(0xFF7A09)
.setAuthor('OpenWeatherMap', 'https://i.imgur.com/S5MHmeY.png')
.setThumbnail(body.weather[0].icon ? `http://openweathermap.org/img/w/${body.weather[0].icon}.png` : null)
.setTimestamp()
.addField(' City',
body.query.results.channel.location.city, true)
body.name, true)
.addField(' Country',
body.query.results.channel.location.country, true)
.addField(' Region',
body.query.results.channel.location.region, true)
body.sys.country, true)
.addField(' Condition',
body.query.results.channel.item.condition.text, true)
body.weather.map(cond => `${cond.main} (${cond.description})`).join('\n'), true)
.addField(' Temperature',
`${body.query.results.channel.item.condition.temp}°F`, true)
`${body.main.temp}°C`, true)
.addField(' Humidity',
body.query.results.channel.atmosphere.humidity, true)
`${body.main.humidity}%`, true)
.addField(' Pressure',
body.query.results.channel.atmosphere.pressure, true)
.addField(' Rising',
body.query.results.channel.atmosphere.rising, true)
`${body.main.pressure} hPa`, true)
.addField(' Visibility',
body.query.results.channel.atmosphere.visibility, true)
.addField(' Wind Chill',
body.query.results.channel.wind.chill, true)
`${body.visibility}m`, true)
.addField(' Cloudiness',
`${body.clouds.all}%`, true)
.addField(' Wind Direction',
body.query.results.channel.wind.direction, true)
`${body.wind.deg}°`, true)
.addField(' Wind Speed',
body.query.results.channel.wind.speed, true);
`${body.wind.speed}m/s`, 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!`);
}
}
+1 -1
View File
@@ -1,6 +1,6 @@
{
"name": "xiaobot",
"version": "36.1.2",
"version": "37.0.0",
"description": "Your personal server companion.",
"main": "Shard.js",
"scripts": {