#weather command doesnt take any option as input

1 messages · Page 1 of 1 (latest)

queen fiber

So I created a weather.js file for the weather slash command that should take the city as option and display the humidity and temperature :

const { SlashCommandBuilder } = require('discord.js');
const axios = require('axios');
const WEATHER_API_KEY = 'my-api-key';

module.exports = {
    data: new SlashCommandBuilder()
      .setName('weather')
      .setDescription('Get the current temperature and humidity in a city')
      .addStringOption(option => option.setName('city').setDescription('The name of the city to get weather information for').setRequired(true)),
    async execute(interaction) {
          // Retrieve the city name from the interaction
          const city = interaction.options.getString('city');

        // Retrieve the weather data from the API
        const response = await axios.get(`http://api.weatherapi.com/v1/current.json?key=${WEATHER_API_KEY}&q=${city}&aqi=yes`);
        const data = response.data;
  
        // Extract the temperature and humidity from the response
        const temperature = data.current.temp_c;
        const humidity = data.current.humidity;
  
        // Send the temperature and humidity as a message
        await interaction.reply(`The current temperature in ${city} is ${temperature}°C with ${humidity}% humidity`);
    }
  };

but it doesn't seem to take any option as input and instead outputs the temperature and humidity of "null". How do I fix this?