diff --git a/src/main/kotlin/cn/rtast/fancybot/commands/WeatherCommand.kt b/src/main/kotlin/cn/rtast/fancybot/commands/WeatherCommand.kt index 9cebc23..ca55369 100644 --- a/src/main/kotlin/cn/rtast/fancybot/commands/WeatherCommand.kt +++ b/src/main/kotlin/cn/rtast/fancybot/commands/WeatherCommand.kt @@ -28,24 +28,32 @@ class WeatherCommand : BaseCommand() { listener.sendGroupMessage(message.groupId, msg) return } - val locationName = args.first() - val lookupLocationId = Http.get( - "https://geoapi.qweather.com/v2/city/lookup", - mapOf("location" to locationName, "key" to configManager.qweatherKey) - ).location.first().id - val weather = Http.get( - "https://devapi.qweather.com/v7/weather/now", - mapOf("location" to lookupLocationId, "key" to configManager.qweatherKey) - ) - val msg = MessageChain.Builder() - .addAt(message.sender.userId) - .addNewLine() - .addText("城市: $locationName, 温度: ${weather.now.temp} ℃") - .addNewLine() - .addText("${weather.now.text}/${weather.now.windDir}") - .addNewLine() - .addText("数据来源: ${weather.refer.sources.joinToString(",")}") - .build() - listener.sendGroupMessage(message.groupId, msg) + try { + val locationName = args.first() + val lookupLocation = Http.get( + "https://geoapi.qweather.com/v2/city/lookup", + mapOf("location" to locationName, "key" to configManager.qweatherKey) + ).location.first() + val weather = Http.get( + "https://devapi.qweather.com/v7/weather/now", + mapOf("location" to lookupLocation.id, "key" to configManager.qweatherKey) + ) + val msg = MessageChain.Builder() + .addAt(message.sender.userId) + .addNewLine() + .addText("城市: ${lookupLocation.name}, 温度: ${weather.now.temp} ℃") + .addNewLine() + .addText("${weather.now.text}/${weather.now.windDir}") + .addNewLine() + .addText("数据来源: ${weather.refer.sources.joinToString(",")}") + .build() + listener.sendGroupMessage(message.groupId, msg) + } catch (_: NullPointerException) { + val msg = MessageChain.Builder() + .addAt(message.sender.userId) + .addText("没有查询到该城市的天气信息哦~") + .build() + listener.sendGroupMessage(message.groupId, msg) + } } } \ No newline at end of file