From 26a88f8525e445fe9e31fa245238f94db62707a1 Mon Sep 17 00:00:00 2001 From: Raven Scott Date: Sun, 20 Aug 2023 19:57:09 -0400 Subject: [PATCH] update --- commands/main/weather.js | 22 +++++++++++++++++----- package.json | 3 ++- 2 files changed, 19 insertions(+), 6 deletions(-) diff --git a/commands/main/weather.js b/commands/main/weather.js index 45631a1..41bc2d5 100644 --- a/commands/main/weather.js +++ b/commands/main/weather.js @@ -1,4 +1,5 @@ const { EmbedBuilder } = require('discord.js'); +const fetch = require('node-fetch'); // Adding the fetch library for HTTP requests module.exports = { name: "weather", @@ -8,18 +9,29 @@ module.exports = { name: "location", description: "The location you would like to check", required: true, - type: 3 // 6 is type USER + type: 3 // 3 is type STRING }], run: async (client, interaction) => { - let encodedLocation = encodeURIComponent(interaction.options._hoistedOptions[0].value); - let location = interaction.options._hoistedOptions[0].value; + const location = interaction.options.getString('location'); + + // Check if the image exists + const imageUrl = `https://wttr.in/${encodeURIComponent(location)}.png`; + try { + const response = await fetch(imageUrl); + if (!response.ok) { + throw new Error("Image not found"); + } + } catch (error) { + return interaction.editReply("Error: The weather image could not be found."); + } const embed = new EmbedBuilder() .setColor("#FF0000") .setTitle(`The weather for: ${location}`) .setTimestamp() - .setImage(`https://wttr.in/${encodedLocation}.png`) + .setImage(imageUrl) .setFooter({ text: `Provided by wttr.in`, iconURL: `${interaction.user.displayAvatarURL()}` }); + await interaction.editReply({ embeds: [embed] }); }, -}; +}; \ No newline at end of file diff --git a/package.json b/package.json index 1f1e990..3a3021a 100644 --- a/package.json +++ b/package.json @@ -12,6 +12,7 @@ "dependencies": { "discord.js": "^14.0.3", "dotenv": "^16.0.0", - "glob": "^7.2.0" + "glob": "^7.2.0", + "node-fetch": "^2.6.13" } }