Simp-O-Matic

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

commit ebb39d99dea33336a185824446c35231f8cbdfa5
parent cb5b352ea2a05238015f99c15735c76ccfe117a1
Author: Demonstrandum <moi@knutsen.co>
Date:   Mon, 23 Mar 2020 21:20:21 +0000

Fix small copy-paste blunder.

Diffstat:
Mlib/commands/weather.ts | 31+++++++++++++++----------------
1 file changed, 15 insertions(+), 16 deletions(-)

diff --git a/lib/commands/weather.ts b/lib/commands/weather.ts @@ -1,20 +1,16 @@ import fetch, { Response } from 'node-fetch'; import { MessageEmbed } from 'discord.js'; -const directions = [ - 'north', - 'north east', - 'east', - 'south east', - 'south', - 'south west', - 'west', - 'north west' +const DIRECTIONS = [ + 'north', 'north east', + 'east', 'south east', + 'south', 'south west', + 'west', 'north west' ]; -const moonPhases = ['🌑', '🌒️', '🌓', '🌔️', '🌕', '🌖️', '🌗', '🌘️']; +const MOON_PHASES = ['🌑', '🌒️', '🌓', '🌔️', '🌕', '🌖️', '🌗', '🌘️']; -const icons = { +const ICONS = { 'clear-day': '🌞', 'clear-night': '🌚', 'rain': '🌧️', @@ -42,12 +38,15 @@ export default (home_scope: HomeScope) => { const location = args[0] ? args.join(' ') : CONFIG.weather_locations[message.author.id] || 'Cuckfield'; + 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()) @@ -57,17 +56,17 @@ export default (home_scope: HomeScope) => { .then(res => res.json()) .then(d => { const date = new Date(d.currently.time); - const embed = d.main + 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(moonPhases[Math.round(d.daily.data[0].moonPhase * 7)] + d.currently.summary) + .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)') + { 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);