From 47725a76245547f3593ace2bd332ade72d3e3e01 Mon Sep 17 00:00:00 2001 From: = Date: Tue, 13 Jul 2021 20:52:54 -0400 Subject: [PATCH] Add weather lookup and reflect command in embed --- commands/airport.js | 2 +- commands/gif.js | 2 +- commands/weather.js | 26 ++++++++++++++++++++++++++ config.json | 3 ++- functions.js | 29 +++++++++++++++++++++++------ 5 files changed, 53 insertions(+), 9 deletions(-) create mode 100644 commands/weather.js diff --git a/commands/airport.js b/commands/airport.js index 54e57be..d0033fa 100644 --- a/commands/airport.js +++ b/commands/airport.js @@ -17,7 +17,7 @@ module.exports = { execute(message, file) { options.params.airport_id = file.name; axios.request(options).then(function (response) { - const embed = functions.createAirportEmbed(response.data, message.author); + const embed = functions.createAirportEmbed(response.data, message.author, `${file.name}.${file.extension}`); message.channel.send(embed).then().catch(err => console.error(err)); }).catch(function (error) { console.error(error); diff --git a/commands/gif.js b/commands/gif.js index 506efa3..d2c4dea 100644 --- a/commands/gif.js +++ b/commands/gif.js @@ -28,7 +28,7 @@ module.exports = { 'embed_url': client.gifs.get(file.name).embed_url, 'author': message.author, }; - message.channel.send(functions.createGifEmbed(gifInfo)); + message.channel.send(functions.createGifEmbed(gifInfo, message.author, `${file.name}.${file.extension}`)); } } } \ No newline at end of file diff --git a/commands/weather.js b/commands/weather.js new file mode 100644 index 0000000..123ad9b --- /dev/null +++ b/commands/weather.js @@ -0,0 +1,26 @@ +const functions = require('../functions.js'); +const axios = require("axios").default; + +var options = { + method: 'GET', + url: 'https://weatherapi-com.p.rapidapi.com/current.json', + params: {q: ''}, + headers: { + 'x-rapidapi-key': '0b3f85bcb7msh1e6e80e963c9914p1d1934jsnc3542fc83520', + 'x-rapidapi-host': 'weatherapi-com.p.rapidapi.com' + } +}; + +module.exports = { + name: 'weather', + description: 'Get the current weather by ZIP code or city name.', + execute(message, file) { + options.params.q = file.name; + axios.request(options).then(function (response) { + const embed = functions.createWeatherEmbed(response.data, message.author, `${file.name}.${file.extension}`); + message.channel.send(embed).then().catch(err => console.error(err)); + }).catch(function (error) { + console.error(error); + }); + } +} \ No newline at end of file diff --git a/config.json b/config.json index 94dbd1e..46c50cc 100644 --- a/config.json +++ b/config.json @@ -16,7 +16,8 @@ "joint", "ping", "strain", - "airport" + "airport", + "weather" ], "emoji": { "joint": "<:joint:862082955902976000>", diff --git a/functions.js b/functions.js index aa4f8c1..7c5cc99 100644 --- a/functions.js +++ b/functions.js @@ -45,13 +45,13 @@ module.exports = { cleanInput(input) { return input.replace(/'/g, '\\\'').replace(/\n/g, '\\n'); }, - createGifEmbed(data) { + createGifEmbed(data, author, command) { return new Discord.MessageEmbed() .setAuthor('NodBot v2 - GIF') - .setTitle(data.name) + .setTitle(command) .setImage(data.embed_url) .setTimestamp() - .setFooter('@' + data.author.username + '#' + data.author.discriminator); + .setFooter(`@${author.username}#${author.discriminator}`); }, saveGif(message, name, embed_url) { fs.writeFile(`./gifs/${name}.js`, `module.exports = {\n\tname: '${name}',\n\tembed_url: '${embed_url}'\n}`, function(err) { @@ -61,10 +61,10 @@ module.exports = { message.client.gifs.set(gif.name, gif); }); }, - createAirportEmbed(data, author) { + createAirportEmbed(data, author, command) { const airport = data.airport[0]; return new Discord.MessageEmbed() - .setAuthor('Airport Information') + .setAuthor(command) .setTitle(airport.airport_name) .addFields( { name: 'Location', value: `${airport.city}, ${airport.state_abbrev}`, inline: true }, @@ -74,5 +74,22 @@ module.exports = { ) .setTimestamp() .setFooter(`@${author.username}#${author.discriminator}`); - } + }, + createWeatherEmbed(data, author, command) { + const loc = data.location; + const weather = data.current; + return new Discord.MessageEmbed() + .setAuthor(command) + .setTitle(`${loc.name}, ${loc.region}, ${loc.country} Weather`) + .setDescription(`The weather is currently ${weather.condition.text}`) + .addFields( + { name: 'Temperature', value: `${weather.temp_f}°F (Feels like: ${weather.feelslike_f}°F)`, inline: true }, + { name: 'Winds', value: `${weather.wind_mph} ${weather.wind_dir}`, inline: true }, + { name: 'Pressure', value: `${weather.pressure_in}inHg`, inline: true }, + { name: 'Relative Humidity', value: `${weather.humidity}%`, inline: true } + ) + .setImage(`https:${weather.condition.icon}`) + .setTimestamp() + .setFooter(`@${author.username}#${author.discriminator}`); + } } \ No newline at end of file