天气命令在我输入位置时捕获错误

问题描述 投票:0回答:0

我正在尝试为我的 Discord 机器人执行天气命令。一切正常。但是当我运行代码并输入位置时,它会捕获错误。我正在使用 Open Weather Map,并且我的 API KEY 是正确的,当我手动输入 url 时,我得到这样的信息

{"coord":{"lon":-0.1257,"lat":51.5085},"weather":[{"id":800,"main":"Clear","description":"clear sky","icon":"01d"}],"base":"stations","main":{"temp":9.87,"feels_like":7.79,"temp_min":8.19,"temp_max":11.25,"pressure":1027,"humidity":55},"visibility":10000,"wind":{"speed":4.12,"deg":100},"clouds":{"all":1},"dt":1680603013,"sys":{"type":2,"id":268730,"country":"GB","sunrise":1680586188,"sunset":1680633422},"timezone":3600,"id":2643743,"name":"London","cod":200}

但是当我尝试使用 /weather London 时,它会出错

我的密码是

const { SlashCommandBuilder } = require('@discordjs/builders');
const { WEATHER_API_KEY } = require('dotenv')
const axios = require('axios');

module.exports = {
    data: new SlashCommandBuilder()
        .setName('weather')
        .setDescription('View the weather for your chosen area')
        .addStringOption(option => option.setName('location').setDescription('The chosen area\'s weather to show')),
    async execute(interaction, MessageEmbed) {
        const target = interaction.options.getString('location')
        axios.get(`https://api.openweathermap.org/data/2.5/weather?q=${target}&units=metric&appid=${WEATHER_API_KEY}`).then(response => {
                    const apiData = response;
                    const currentTemp = Math.ceil(apiData.data.main.temp)
                    const wind = apiData.data.wind.speed;
                    const icon = apiData.data.weather[0].icon
                    const cityName = interaction.options.data[0].value
                    const country = apiData.data.sys.country
                    const cloudness = apiData.data.weather[0].description;
                    const { pressure, humidity, temp_max, temp_min  } = response.data.main;

                    const weatherEmbed = new MessageEmbed()
                        .setTitle(`The temperature is ${currentTemp}\u00B0C in ${cityName}, ${country}`)
                        .addFields(
                            { name: `Maximum Temperature:`, value: `${temp_max}\u00B0C`, inline: true },
                            { name: `Minimum Temperature:`, value: `${temp_min}\u00B0C`, inline: true },
                            { name: `Humidity:`, value: `${humidity} %`, inline: true },
                            { name: `Wind Speed:`, value: `${wind} m/s`, inline: true },
                            { name: `Pressure:`, value: `${pressure} hpa`, inline: true },
                            { name: `Cloudiness:`, value: `${cloudness}`, inline: true },
                        )
                        .setThumbnail(`http://openweathermap.org/img/w/${icon}.png`)
                        .setColor("#FFC0CB")
                interaction.reply({ embeds: [weatherEmbed] })
            }).catch(err => {
                interaction.reply(`Please enter a valid city name`)
             })
    },
};``` My discord bot types ````Please enter a valid city name```, but I type London, Tokyo, they are valid.Please help me
`

I firstly expected that, the problem is in the API KEY. But I checked it, and It was true 
node.js api npm discord.js openweathermap
© www.soinside.com 2019 - 2024. All rights reserved.