Simp-O-Matic

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

commit f1dcb6727051bb98c20937418d2effb700573333
parent cd6b8e849cba0f2411cb982436c1ece691a253b5
Author: Demonstrandum <moi@knutsen.co>
Date:   Tue, 24 Mar 2020 17:18:02 +0000

Merge weather.ts... Hope nothing breaks...

Diffstat:
Mlib/commands/weather.ts | 48+++++++++++++++++++++++++++---------------------
1 file changed, 27 insertions(+), 21 deletions(-)

diff --git a/lib/commands/weather.ts b/lib/commands/weather.ts @@ -24,7 +24,7 @@ const ICONS = { }; const WEATHER_URL = 'https://api.darksky.net/forecast'; -const GEOCODE_URL = 'https://api.mapbox.com/geocoding/v5/mapbox.places'; +const GEOCODE_URL = 'https://geocode-maps.yandex.ru/1.x/?format=json'; export default async (home_scope: HomeScope) => { const { message, args, SECRETS, CONFIG } = home_scope; @@ -40,47 +40,52 @@ export default async (home_scope: HomeScope) => { : CONFIG.weather_locations[message.author.id] || 'Cuckfield'; const key = SECRETS.darksky.key; - const geokey = SECRETS.mapbox.key; + const geokey = SECRETS.yandex.geocoder.key; const error = (e: Error) => { message.answer(`Error getting weather\n\`\`\`${e.message}\`\`\``); return e; }; - let geocoder_json, weather_info; + let geocoder_json, weather_info, geo_object; try { - const geocoder = await fetch( - `${GEOCODE_URL}/${location}.json` - + `?access_token=${geokey}` - + `&limit=1&language=en`); + const geocoder = await fetch(`${GEOCODE_URL}&apikey=${geokey}` + +`&geocode=${location}&lang=en-US`); geocoder_json = await geocoder.json(); - weather_info = await fetch( - `${WEATHER_URL}/${key}/${geocoder_json.features[0].center[0]},` - + `${geocoder_json.features[0].center[1]}` + geo_object = geocoder_json.response + .GeoObjectCollection + .featureMember[0].GeoObject; + + const lat_lon = geo_object.Point.pos + .split(' ').reverse(); + + weather_info = await fetch(`${WEATHER_URL}/${key}/${lat_lon}` +`?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); + const date = new Date(d.currently.time * 1000); + const date_string = date.toLocaleTimeString('en-GB', { + hour: '2-digit', + minute: '2-digit', + timeZone: d.timezone + }); let embed = new MessageEmbed() .setTitle(`Cannot get weather information from ${location}.`); - if (!(d && d.currently)) - return message.channel.send(embed); - - embed = new MessageEmbed() + if (d && d.currently) 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}`) + .setAuthor(`${ICONS[d.currently.icon]} ${date_string}` + +` ${geo_object.name},` + +` ${geo_object.description}`) .setDescription( MOON_PHASES[Math.round(d.daily.data[0].moonPhase * 7)] - + d.currently.summary) + + ' ' + d.currently.summary + '.') .addFields( { name: 'daytime', value: d.daily.data[0].temperatureHigh + '°C', @@ -89,13 +94,14 @@ export default async (home_scope: HomeScope) => { value: d.daily.data[0].temperatureLow + '°C', inline: true }, { name: 'humidity', - value: d.currently.humidity + '%', + value: d.currently.humidity.toString().substring(2) + '%', inline: true }, { name: 'wind', value: `${DIRECTIONS[Math.round(d.currently.windBearing / 45) % 8]}` + ` ${d.currently.windSpeed} ㎧`, inline: true }) - .setFooter('Powered by Dark Sky (R)'); + .setFooter('Powered by Dark Sky', + 'https://darksky.net/images/darkskylogo.png'); message.channel.send(embed); };