fix: fix weather null pointer tip

This commit is contained in:
2024-09-06 15:28:48 +08:00
parent cbca4323b4
commit 010f9fa466
1 file changed
+27 -19
@@ -28,24 +28,32 @@ class WeatherCommand : BaseCommand() {
listener.sendGroupMessage(message.groupId, msg) listener.sendGroupMessage(message.groupId, msg)
return return
} }
val locationName = args.first() try {
val lookupLocationId = Http.get<Geo>( val locationName = args.first()
"https://geoapi.qweather.com/v2/city/lookup", val lookupLocation = Http.get<Geo>(
mapOf("location" to locationName, "key" to configManager.qweatherKey) "https://geoapi.qweather.com/v2/city/lookup",
).location.first().id mapOf("location" to locationName, "key" to configManager.qweatherKey)
val weather = Http.get<Weather>( ).location.first()
"https://devapi.qweather.com/v7/weather/now", val weather = Http.get<Weather>(
mapOf("location" to lookupLocationId, "key" to configManager.qweatherKey) "https://devapi.qweather.com/v7/weather/now",
) mapOf("location" to lookupLocation.id, "key" to configManager.qweatherKey)
val msg = MessageChain.Builder() )
.addAt(message.sender.userId) val msg = MessageChain.Builder()
.addNewLine() .addAt(message.sender.userId)
.addText("城市: $locationName, 温度: ${weather.now.temp}") .addNewLine()
.addNewLine() .addText("城市: ${lookupLocation.name}, 温度: ${weather.now.temp}")
.addText("${weather.now.text}/${weather.now.windDir}") .addNewLine()
.addNewLine() .addText("${weather.now.text}/${weather.now.windDir}")
.addText("数据来源: ${weather.refer.sources.joinToString(",")}") .addNewLine()
.build() .addText("数据来源: ${weather.refer.sources.joinToString(",")}")
listener.sendGroupMessage(message.groupId, msg) .build()
listener.sendGroupMessage(message.groupId, msg)
} catch (_: NullPointerException) {
val msg = MessageChain.Builder()
.addAt(message.sender.userId)
.addText("没有查询到该城市的天气信息哦~")
.build()
listener.sendGroupMessage(message.groupId, msg)
}
} }
} }