diff --git a/bot.js b/bot.js index bdae409..1bf8fd2 100644 --- a/bot.js +++ b/bot.js @@ -1,26 +1,8 @@ const config = require("./config.json"); const Discord = require("discord.js"); const client = new Discord.Client(); -const request = require("request"); -const fs = require("fs"); - -let callsign; -let miniMapUrl; -let lat; -let lng; -let altitude; -let coords; -let timeUpdated = new Date(); - -let dateOptions = { - weekday: "short", - year: "numeric", - month: "long", - day: "numeric", - hour: "numeric", - minute: "numeric", - timeZone: config.timezone -}; +const getLocationInfo = require("./modules/getLocationInfo"); +const getWeather = require("./modules/getWeather"); client.on("error", e => { console.error(e); @@ -55,52 +37,13 @@ client.on("message", message => { .split(/ +/g); const command = args.shift(); if (command === "call") { - callsign = args.join("").toLowerCase(); - request.get( - `https://api.aprs.fi/api/get?name=${callsign}&what=loc&apikey=${ - config.aprs_token - }&format=json`, - function(error, res, body) { - if (error) { - console.log(error); - return; - } - let data = JSON.parse(body); - if (data.found === 0) { - message.channel.send( - "Sorry, I couldn't find that. Please check the call sign and try again." - ); - return; - } else { - lat = data.entries[0].lat; - lng = data.entries[0].lng; - altitude = data.entries[0].altitude; - coords = `${lat},${lng}`; - timeUpdated = new Date(data.entries[0].time * 1000); - miniMapUrl = `https://maps.googleapis.com/maps/api/staticmap?center=${coords}&zoom=13&size=600x300&maptype=roadmap&markers=color:blue|${coords}&key=${ - config.gmaps_token - }`; - let locationEmbed = new Discord.RichEmbed() - .setColor(config.embed_color) - .setAuthor("APRS Bot") - .addField("Coordinates", coords) - .addField( - "Altitude", - `${altitude}m (${Math.round(altitude * 3.28084)}ft)` - ) - .addField("Time", timeUpdated.toLocaleString("en-US", dateOptions)) - .addField( - "Directions", - `https://www.google.com/maps/search/?api=1&query=${coords}` - ) - .setImage(miniMapUrl) - .setTimestamp() - .setFooter("Data sourced from https://aprs.fi/"); - message.channel.send({ embed: locationEmbed }); - return; - } - } - ); + let callsign = args.join("").toLowerCase(); + getLocationInfo(callsign, message); + return; + } + if (command === "weather" || command === "wx") { + let callsign = args.join("").toLowerCase(); + getWeather(callsign, message); return; } }); diff --git a/modules/getLocationInfo.js b/modules/getLocationInfo.js new file mode 100644 index 0000000..f07f991 --- /dev/null +++ b/modules/getLocationInfo.js @@ -0,0 +1,71 @@ +const request = require("request"); +const config = require("../config.json"); +const Discord = require("discord.js"); + +let dateOptions = { + weekday: "short", + year: "numeric", + month: "long", + day: "numeric", + hour: "numeric", + minute: "numeric", + timeZone: config.timezone +}; + +function titleCase(title) { + if (title.length > 0) { + return title.charAt(0).toUpperCase() + title.slice(1); + } + return; +} + +function getLocationInfo(callsign, message) { + request.get( + `https://api.aprs.fi/api/get?name=${callsign}&what=loc&apikey=${ + config.aprs_token + }&format=json`, + function(error, res, body) { + if (error) { + console.log(error); + return; + } + let data = JSON.parse(body); + if (data.found === 0) { + message.channel.send( + "Sorry, I couldn't find that. Please check the call sign and try again." + ); + return; + } else { + let lat = data.entries[0].lat; + let lng = data.entries[0].lng; + let altitude = data.entries[0].altitude + ? `${data.entries[0].altitude}m (${Math.round( + data.entries[0].altitude * 3.28084 + )}ft)` + : "Not available"; + let coords = `${lat},${lng}`; + let timeUpdated = new Date(data.entries[0].time * 1000); + let miniMapUrl = `https://maps.googleapis.com/maps/api/staticmap?center=${coords}&zoom=13&size=600x300&maptype=roadmap&markers=color:blue|${coords}&key=${ + config.gmaps_token + }`; + let locationEmbed = new Discord.RichEmbed() + .setColor(config.embed_color) + .setAuthor("APRS Bot") + .addField("Coordinates", coords) + .addField("Altitude", altitude) + .addField("Time", timeUpdated.toLocaleString("en-US", dateOptions)) + .addField( + "Gmaps", + `https://www.google.com/maps/search/?api=1&query=${coords}` + ) + .setImage(miniMapUrl) + .setTimestamp() + .setFooter("Data sourced from https://aprs.fi/"); + message.channel.send({ embed: locationEmbed }); + return; + } + } + ); +} + +module.exports = getLocationInfo; diff --git a/modules/getWeather.js b/modules/getWeather.js new file mode 100644 index 0000000..12edf48 --- /dev/null +++ b/modules/getWeather.js @@ -0,0 +1,83 @@ +const request = require("request"); +const config = require("../config.json"); +const Discord = require("discord.js"); + +let miniMapUrl; +let lat; +let lng; +let altitude; +let coords; +let timeUpdated = new Date(); + +let dateOptions = { + weekday: "short", + year: "numeric", + month: "long", + day: "numeric", + hour: "numeric", + minute: "numeric", + timeZone: config.timezone +}; + +function titleCase(title) { + if (title.length > 0) { + return title.charAt(0).toUpperCase() + title.slice(1); + } + return; +} + +function getWeather(callsign, message) { + request.get( + `https://api.aprs.fi/api/get?name=${callsign}&what=wx&apikey=${ + config.aprs_token + }&format=json`, + function(error, res, body) { + if (error) { + console.log(error); + return; + } + let data = JSON.parse(body); + if (data.found === 0) { + message.channel.send( + "Sorry, I couldn't find that. Please check the call sign and try again." + ); + return; + } else { + let temp = data.entries[0].temp || "Not available"; + let pressure = data.entries[0].pressure || "Not available"; + let humidity = data.entries[0].humidity || "Not available"; + let wind_direction = data.entries[0].wind_direction || "Not available"; + let wind_speed = data.entries[0].wind_speed || "Not available"; + let wind_gust = data.entries[0].wind_gust || "Not available"; + let rain_1h = data.entries[0].rain_1h || "Not available"; + let rain_24h = data.entries[0].rain_24h || "Not available"; + let rain_mn = data.entries[0].rain_mn || "Not available"; + let luminosity = data.entries[0].luminosity || "Not available"; + timeUpdated = new Date(data.entries[0].time * 1000); + miniMapUrl = `https://maps.googleapis.com/maps/api/staticmap?center=${coords}&zoom=13&size=600x300&maptype=roadmap&markers=color:blue|${coords}&key=${ + config.gmaps_token + }`; + let locationEmbed = new Discord.RichEmbed() + .setColor(config.embed_color) + .setAuthor("APRS Bot") + .addField("Temp", `${temp}C`) + .addField("Pressure", pressure) + .addField("Humidity", humidity) + .addField("Wind direction", wind_direction) + .addField("Wind speed", wind_speed) + .addField("Wind gust", wind_gust) + .addField("Rainfall past 1hr", `${rain_1h}mm`) + .addField("Rainfall past 24hrs", `${rain_1h}mm`) + .addField("Rainfall since midnight", `${rain_mn}mm`) + .addField("Luminosity", luminosity) + .addField("Time", timeUpdated.toLocaleString("en-US", dateOptions)) + .setTimestamp() + .setFooter("Data sourced from https://aprs.fi/"); + message.channel.send({ embed: locationEmbed }); + return; + } + } + ); +} + +module.exports = getWeather;