commit cd6b8e849cba0f2411cb982436c1ece691a253b5
parent ebb39d99dea33336a185824446c35231f8cbdfa5
Author: Demonstrandum <moi@knutsen.co>
Date: Tue, 24 Mar 2020 16:59:52 +0000
Refactor weather just a lil.
Diffstat:
2 files changed, 59 insertions(+), 33 deletions(-)
diff --git a/lib/commands/weather.ts b/lib/commands/weather.ts
@@ -1,4 +1,4 @@
-import fetch, { Response } from 'node-fetch';
+import fetch from 'node-fetch';
import { MessageEmbed } from 'discord.js';
const DIRECTIONS = [
@@ -21,12 +21,12 @@ const ICONS = {
'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) => {
+export default async (home_scope: HomeScope) => {
const { message, args, SECRETS, CONFIG } = home_scope;
if (args[0] === 'set' && args.length > 1) {
@@ -42,34 +42,60 @@ export default (home_scope: HomeScope) => {
const key = SECRETS.darksky.key;
const geokey = SECRETS.mapbox.key;
- const error = (e: Response) => {
- message.answer(`Error getting weather\n\`\`\`${e}\`\`\``);
+ const error = (e: Error) => {
+ message.answer(`Error getting weather\n\`\`\`${e.message}\`\`\``);
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 && d.currently
- ? 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(MOON_PHASES[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}.`)
-
- message.channel.send(embed);
- }).catch(error);
- })
+ let geocoder_json, weather_info;
+ try {
+ const geocoder = await fetch(
+ `${GEOCODE_URL}/${location}.json`
+ + `?access_token=${geokey}`
+ + `&limit=1&language=en`);
+
+ geocoder_json = await geocoder.json();
+ weather_info = await fetch(
+ `${WEATHER_URL}/${key}/${geocoder_json.features[0].center[0]},`
+ + `${geocoder_json.features[0].center[1]}`
+ +`?exclude=minutely,hourly,alerts,flags&units=si`);
+ } catch (e) {
+ return error(e);
+ }
+
+ const d = await weather_info.json();
+ const date = new Date(d.currently.time);
+
+ let embed = new MessageEmbed()
+ .setTitle(`Cannot get weather information from ${location}.`);
+
+ if (!(d && d.currently))
+ return message.channel.send(embed);
+
+ embed = new MessageEmbed()
+ .setTitle(`${d.currently.temperature}°C`
+ + ` (feels like ${d.currently.apparentTemperature}°C)`)
+ .setAuthor(`${ICONS[d.currently.icon]}`
+ + ` ${date.getHours()}:${date.getMinutes()}`
+ + ` ${geocoder_json.features[0].place_name}`)
+ .setDescription(
+ MOON_PHASES[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)');
+
+ message.channel.send(embed);
};
diff --git a/lib/main.ts b/lib/main.ts
@@ -504,15 +504,15 @@ function on_termination(error_type) {
// Handle exits.
process
- .on('SIGTERM', on_termination.bind(this, 'SIGTERM'))
- .on('SIGINT', on_termination.bind(this, 'SIGINT'))
.on('beforeExit', on_termination.bind(this, 'beforeExit'))
.on('exit', on_termination.bind(this, 'exit'))
+ .on('SIGTERM', on_termination.bind(this, 'SIGTERM'))
+ .on('SIGINT', on_termination.bind(this, 'SIGINT'))
.on('SIGUSR1', on_termination.bind(this, 'SIGUSR1'))
.on('SIGUSR2', on_termination.bind(this, 'SIGUSR2'))
.on('uncaughtException', on_termination.bind(this, 'exception'));
-// CONFIG will eventually update to the online version.
+// GLOBAL_CONFIG will eventually update to the online version.
pastebin_latest().then(res => {
GLOBAL_CONFIG = deep_merge(GLOBAL_CONFIG, res);
// Remove any duplicates.