Simp-O-Matic

Dumb Discord bot in TS.
git clone git://git.knutsen.co/Simp-O-Matic
Log | Files | Refs | README | LICENSE

commit b0e340fb2423e4f169057e55c955a3de8b127849
parent 067735a4b0732c6fb2423f905bfda9f678d483b6
Author: danyisill <danyisill@users.noreply.github.com>
Date:   Mon, 23 Mar 2020 21:35:13 +0300

u

Diffstat:
Mlib/commands/weather.ts | 91+++++++++++++++++++++++++++++++++++++++++++++++--------------------------------
1 file changed, 54 insertions(+), 37 deletions(-)

diff --git a/lib/commands/weather.ts b/lib/commands/weather.ts @@ -1,10 +1,34 @@ import fetch, { Response } from 'node-fetch'; import { MessageEmbed } from 'discord.js'; -import countries from '../resources/countries.json'; -const directions = ['north', 'north-east', 'east', 'south-east', - 'south', 'south-west', 'west', 'north-west', 'north']; -const WEATHER_URL = 'http://api.openweathermap.org/data/2.5/weather'; +const directions = [ + 'north', + 'north east', + 'east', + 'south east', + 'south', + 'south west', + 'west', + 'north west' +]; + +const moonPhases = ['🌑', '🌒️', '🌓', '🌔️', '🌕', '🌖️', '🌗', '🌘️']; + +const icons = { + 'clear-day': '🌞', + 'clear-night': '🌚', + 'rain': '🌧️', + 'snow': '❄️', + 'sleet': '🌨️', + 'wind': '💨', + 'fog': '🌫️', + 'cloudy': '🌥️', + 'partly-cloudy-day': '⛅', + 'partly-cloudy-night': '⛅' +} + +const WEATHER_URL = 'https://api.darksky.net/forecast'; +const GEOCODE_URL = 'https://api.mapbox.com/geocoding/v5/mapbox.places'; export default (home_scope: HomeScope) => { const { message, args, SECRETS, CONFIG } = home_scope; @@ -18,42 +42,35 @@ export default (home_scope: HomeScope) => { const location = args[0] ? args.join(' ') : CONFIG.weather_locations[message.author.id] || 'Cuckfield'; - const key = SECRETS.openweather.key; - + const key = SECRETS.darksky.key; + const geokey = SECRETS.mapbox.key; const error = (e: Response) => { message.answer(`Error getting weather\n\`\`\`${e}\`\`\``); return e; }; + fetch(`${GEOCODE_URL}/${location}.json?access_token=${geokey}&limit=1&language=en`) + .catch(error) + .then(res => res.json()) + .then(c => { + fetch(`${WEATHER_URL}/${key}/${c.features[0].center[0]},${c.features[0].center[1]}?exclude=minutely,hourly,alerts,flags&units=si`) + .catch(error) + .then(res => res.json()) + .then(d => { + const date = new Date(d.currently.time); + const embed = d.main + ? new MessageEmbed() + .setTitle(`${d.currently.temperature}°C (feels like ${d.currently.apparentTemperature}°C)`) + .setAuthor(`${icons[d.currently.icon]} ${date.getHours()}:${date.getMinutes()} ${c.features[0].place_name}`) + .setDescription(moonPhases[Math.round(d.daily.data[0].moonPhase * 7)] + d.currently.summary) + .addFields( + { name: 'daytime', value: d.daily.data[0].temperatureHigh + '°C', inline: true }, + { name: 'nighttime', value: d.daily.data[0].temperatureLow + '°C', inline: true }, + { name: 'humidity', value: d.currently.humidity + '%', inline: true}, + { name: 'wind', value: `${directions[Math.round(d.currently.windBearing / 45) % 8]} ${d.currently.windSpeed}㎧`, inline: true }) + .setFooter('Powered by Dark Sky(R)'); + : new MessageEmbed().setTitle(`Cannot get weather information from ${location}.`) - fetch(`${WEATHER_URL}?q=${location}&appid=${key}&units=metric`) - .catch(error) - .then(res => res.json()) - .then(d => { - const date = new Date(); - const tz = d.timezone / 3600; // Now in hours. - const tz_frac = tz % 1; // Fractional part in hours. - - date.setMinutes(date.getMinutes() + tz_frac * 60); - - const hour = ((24 + date.getUTCHours() + tz - tz_frac) % 24) - .toFixed().padStart(2, '0'); - const minutes = date.getMinutes().toFixed().padStart(2, '0'); - const country = !d.sys ? 'somewhere' : d.sys.country; - - const embed = d.main - ? new MessageEmbed() - .setTitle(`${d.main.temp}°C (feels ${d.main.feels_like}°C)`) - .setAuthor(`${hour}:${minutes} ${d.name}, ${countries[country].toLowerCase()}`) - .setDescription(d.weather[0].description) - .setThumbnail(`https://openweathermap.org/img/wn/${d.weather[0].icon}@2x.png`) - .addFields( - { name: 'daytime', value: d.main.temp_max + '°C', inline: true }, - { name: 'nighttime', value: d.main.temp_min + '°C', inline: true }, - { name: 'humidity', value: d.main.humidity + '%' }, - { name: 'wind', value: `${directions[Math.round(d.wind.deg / 45)]} ${d.wind.speed} ㎧`, inline: true }) - : new MessageEmbed() - .setTitle(`Cannot get weather information from ${location}.`); - - message.channel.send(embed); - }).catch(error); + message.channel.send(embed); + }).catch(error); + }) };