Add weather command

This commit is contained in:
SileNce5k 2023-12-03 21:12:21 +01:00
parent 0853f97e99
commit b0188483ac
No known key found for this signature in database
GPG key ID: C507260E7F2583AD
2 changed files with 57 additions and 0 deletions

30
util/getWeather.js Normal file
View file

@ -0,0 +1,30 @@
const https = require('https');
module.exports = async function (location) {
const url = `https://wttr.in/${location}?format=4&M` // 4 = one line, M = metric wind speed
let success = false;
let weather = await new Promise((resolve, reject) => {
https.get(url, (res) => {
let data = '';
res.on('data', (chunk) => {
data += chunk;
});
res.on('end', () => {
if(res.statusCode === 404){
reject(`Couldn't find weather for ${location}`);
return;
}else if(res.statusCode != 200){
reject(`Something went wrong while getting the weather for ${location}`);
} else{
resolve(data);
success = true;
}
});
}).on("error", (err) => {
reject(err);
});
})
return {success: success, weather: weather};
}