commit f23126f2ab03000d0010962ad3ff94b2f9d072ff
parent b94e15055706e57493a3ecc6ddcdad66a89a6131
Author: Demonstrandum <moi@knutsen.co>
Date: Wed, 18 Mar 2020 15:46:12 +0000
Fix weather.
Diffstat:
3 files changed, 17 insertions(+), 7 deletions(-)
diff --git a/lib/commands/weather.ts b/lib/commands/weather.ts
@@ -1,4 +1,4 @@
-import request from 'request';
+const fetch = require('node-fetch');
const WEATHER_URL = 'http://api.openweathermap.org/data/2.5/weather';
@@ -6,7 +6,7 @@ export default home_scope => {
const { message, args, SECRETS, CONFIG } = home_scope;
if (args[0] === 'set'){
- CONFIG.weather_locations[message.author.id] = args.slice(1).join(' ');
+ CONFIG.weather_locations[message.author.id] = args.tail().join(' ');
message.answer(`Your weather location has \
been set to ${args.slice(1).join(' ')}`.squeeze());
} else {
@@ -15,17 +15,26 @@ export default home_scope => {
: CONFIG.weather_locations[message.author.id] || 'Cuckfield';
const key = SECRETS.openweather.key;
- request(`${WEATHER_URL}?q=${location}&appid=${key}&units=metric`,
- (_a, _b, c) => {
- const d = JSON.parse(c);
+ const error = e => message.answer(`Error getting weather\n\`\`\`${e}\`\`\``);
+ fetch(`${WEATHER_URL}?q=${location}&appid=${key}&units=metric`)
+ .catch(error)
+ .then(res => res.json())
+ .then(d => {
const date = new Date();
const hour = (24 + date.getUTCHours() + d.timezone) % 24;
+ const country = !d.sys ? 'somewhere' : d.sys.country;
+ if (d.main) {
message.answer(`${hour}:${date.getMinutes()} ${d.name}, \
- ${d.sys.country}: ${d.main.temp}°C \
+ ${country}: ${d.main.temp}°C \
(feels like ${d.main.feels_like}°C) \
${d.weather[0].description}, \
${d.main.temp_max}°C max, \
${d.main.temp_min}°C min`.squeeze());
+ } else {
+ message.answer(`Cannot get weather information`
+ + ` from ${location}`);
+ }
})
+ .catch(error);
}
};
diff --git a/package.json b/package.json
@@ -40,6 +40,7 @@
"discord.js": "11.6.1",
"google-auth-library": "^5.10.1",
"googleapis": "^48.0.0",
+ "node-fetch": "^2.6.0",
"scrape-youtube": "^0.0.5",
"tslib": "^1.11.1",
"typescript": "^3.8.3",
diff --git a/yarn.lock b/yarn.lock
@@ -874,7 +874,7 @@ ms@^2.1.1:
resolved "https://registry.yarnpkg.com/ms/-/ms-2.1.2.tgz#d09d1f357b443f493382a8eb3ccd183872ae6009"
integrity sha512-sGkPx+VjMtmA6MX27oA4FBFELFCZZ4S4XqeGOXCv68tT+jb3vk/RyaKWP0PTKyWtmLSM0b+adUTEvbs1PEaH2w==
-node-fetch@^2.3.0:
+node-fetch@^2.3.0, node-fetch@^2.6.0:
version "2.6.0"
resolved "https://registry.yarnpkg.com/node-fetch/-/node-fetch-2.6.0.tgz#e633456386d4aa55863f676a7ab0daa8fdecb0fd"
integrity sha512-8dG4H5ujfvFiqDmVu9fQ5bOHUC15JMjMY/Zumv26oOvvVJjM67KF8koCWIabKQ1GJIa9r2mMZscBq/TbdOcmNA==