From 7ce76aa0d295a6229d855fcd23a3764251132553 Mon Sep 17 00:00:00 2001 From: RTAkland Date: Wed, 4 Sep 2024 20:01:09 +0800 Subject: [PATCH] feat: add weather command --- gradle/libs.versions.toml | 2 +- src/main/kotlin/cn/rtast/fancybot/Const.kt | 3 +- src/main/kotlin/cn/rtast/fancybot/FancyBot.kt | 24 ++++----- .../rtast/fancybot/commands/WeatherCommand.kt | 49 +++++++++++++++++++ .../kotlin/cn/rtast/fancybot/entity/Config.kt | 3 +- .../cn/rtast/fancybot/entity/weather/Geo.kt | 16 ++++++ .../rtast/fancybot/entity/weather/Weather.kt | 23 +++++++++ .../rtast/fancybot/util/file/ConfigManager.kt | 1 + src/main/resources/config.json | 5 +- 9 files changed, 111 insertions(+), 15 deletions(-) create mode 100644 src/main/kotlin/cn/rtast/fancybot/commands/WeatherCommand.kt create mode 100644 src/main/kotlin/cn/rtast/fancybot/entity/weather/Geo.kt create mode 100644 src/main/kotlin/cn/rtast/fancybot/entity/weather/Weather.kt diff --git a/gradle/libs.versions.toml b/gradle/libs.versions.toml index 23e7af2..e413d9f 100644 --- a/gradle/libs.versions.toml +++ b/gradle/libs.versions.toml @@ -9,11 +9,11 @@ motdPingerVersion = "1.0.1" [libraries] rOneBot = { group = "cn.rtast", name = "ROneBot", version.ref = "ronebotVersion" } +motdPinger = { group = "cn.rtast", name = "MotdPinger", version.ref = "motdPingerVersion" } okhttp = { group = "com.squareup.okhttp3", name = "okhttp", version.ref = "okhttpVersion" } exposedCore = { group = "org.jetbrains.exposed", name = "exposed-core", version.ref = "exposedVersion" } exposedJDBC = { group = "org.jetbrains.exposed", name = "exposed-jdbc", version.ref = "exposedVersion" } sqliteJDBC = { group = "org.xerial", name = "sqlite-jdbc", version.ref = "sqliteJDBCVersion" } -motdPinger = { group = "cn.rtast", name = "MotdPinger", version.ref = "motdPingerVersion" } [bundles] diff --git a/src/main/kotlin/cn/rtast/fancybot/Const.kt b/src/main/kotlin/cn/rtast/fancybot/Const.kt index 0319afb..e11b9fc 100644 --- a/src/main/kotlin/cn/rtast/fancybot/Const.kt +++ b/src/main/kotlin/cn/rtast/fancybot/Const.kt @@ -25,7 +25,8 @@ val DEFAULT_CONFIG = Config( wsType = WSType.Client, accessToken = "114514", port = 6760, - listeningGroups = listOf(114514, 1919810) + listeningGroups = listOf(114514, 1919810), + qweatherKey = "114514" ) val ADMINS = listOf( diff --git a/src/main/kotlin/cn/rtast/fancybot/FancyBot.kt b/src/main/kotlin/cn/rtast/fancybot/FancyBot.kt index 97de207..be54375 100644 --- a/src/main/kotlin/cn/rtast/fancybot/FancyBot.kt +++ b/src/main/kotlin/cn/rtast/fancybot/FancyBot.kt @@ -19,6 +19,7 @@ import cn.rtast.fancybot.commands.MyPointCommand import cn.rtast.fancybot.commands.QRCodeCommand import cn.rtast.fancybot.commands.RedeemCommand import cn.rtast.fancybot.commands.SignCommand +import cn.rtast.fancybot.commands.WeatherCommand import cn.rtast.fancybot.entity.enums.WSType import cn.rtast.fancybot.items.BaisiItem import cn.rtast.fancybot.items.HeisiItem @@ -66,14 +67,9 @@ class FancyBot : OBMessage { } } -val commands = listOf( - EchoCommand(), JrrpCommand(), - MusicCommand(), SignCommand(), - RedeemCommand(), MyPointCommand(), - HitokotoCommand(), FKXQSCommand(), - QRCodeCommand(), AntiRevokeCommand(), - MCPingCommand(), HelpCommand() -) +val configManager = ConfigManager() +val itemManager = ItemManager() +val signManager = SignManager() val items = listOf( HeisiItem(), @@ -81,9 +77,15 @@ val items = listOf( SetuItem() ) -val configManager = ConfigManager() -val itemManager = ItemManager() -val signManager = SignManager() +val commands = listOf( + EchoCommand(), JrrpCommand(), + MusicCommand(), SignCommand(), + RedeemCommand(), MyPointCommand(), + HitokotoCommand(), FKXQSCommand(), + QRCodeCommand(), AntiRevokeCommand(), + MCPingCommand(), HelpCommand(), + WeatherCommand() +) suspend fun main() { val fancyBot = FancyBot() diff --git a/src/main/kotlin/cn/rtast/fancybot/commands/WeatherCommand.kt b/src/main/kotlin/cn/rtast/fancybot/commands/WeatherCommand.kt new file mode 100644 index 0000000..df27faf --- /dev/null +++ b/src/main/kotlin/cn/rtast/fancybot/commands/WeatherCommand.kt @@ -0,0 +1,49 @@ +/* + * Copyright © 2024 RTAkland + * Author: RTAkland + * Date: 2024/9/4 + */ + + +package cn.rtast.fancybot.commands + +import cn.rtast.fancybot.configManager +import cn.rtast.fancybot.entity.weather.Geo +import cn.rtast.fancybot.entity.weather.Weather +import cn.rtast.fancybot.util.Http +import cn.rtast.rob.entity.GroupMessage +import cn.rtast.rob.util.BaseCommand +import cn.rtast.rob.util.ob.MessageChain +import cn.rtast.rob.util.ob.OBMessage + +class WeatherCommand : BaseCommand() { + override val commandNames = listOf("/weather", "/天气") + + override suspend fun executeGroup(listener: OBMessage, message: GroupMessage, args: List) { + 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) + } +} \ No newline at end of file diff --git a/src/main/kotlin/cn/rtast/fancybot/entity/Config.kt b/src/main/kotlin/cn/rtast/fancybot/entity/Config.kt index 9462568..734671b 100644 --- a/src/main/kotlin/cn/rtast/fancybot/entity/Config.kt +++ b/src/main/kotlin/cn/rtast/fancybot/entity/Config.kt @@ -15,5 +15,6 @@ data class Config( val wsAddress: String, val accessToken: String, val port: Int, - val listeningGroups: List + val listeningGroups: List, + val qweatherKey: String ) \ No newline at end of file diff --git a/src/main/kotlin/cn/rtast/fancybot/entity/weather/Geo.kt b/src/main/kotlin/cn/rtast/fancybot/entity/weather/Geo.kt new file mode 100644 index 0000000..cd7441c --- /dev/null +++ b/src/main/kotlin/cn/rtast/fancybot/entity/weather/Geo.kt @@ -0,0 +1,16 @@ +/* + * Copyright © 2024 RTAkland + * Author: RTAkland + * Date: 2024/9/4 + */ + + +package cn.rtast.fancybot.entity.weather + +data class Geo( + val location: List +) { + data class Location( + val id: String + ) +} \ No newline at end of file diff --git a/src/main/kotlin/cn/rtast/fancybot/entity/weather/Weather.kt b/src/main/kotlin/cn/rtast/fancybot/entity/weather/Weather.kt new file mode 100644 index 0000000..223b7bf --- /dev/null +++ b/src/main/kotlin/cn/rtast/fancybot/entity/weather/Weather.kt @@ -0,0 +1,23 @@ +/* + * Copyright © 2024 RTAkland + * Author: RTAkland + * Date: 2024/9/4 + */ + + +package cn.rtast.fancybot.entity.weather + +data class Weather( + val now: Now, + val refer: Refer +) { + data class Now( + val temp: String, + val text: String, + val windDir: String + ) + + data class Refer( + val sources: List + ) +} \ No newline at end of file diff --git a/src/main/kotlin/cn/rtast/fancybot/util/file/ConfigManager.kt b/src/main/kotlin/cn/rtast/fancybot/util/file/ConfigManager.kt index 9ed69bd..6c5973d 100644 --- a/src/main/kotlin/cn/rtast/fancybot/util/file/ConfigManager.kt +++ b/src/main/kotlin/cn/rtast/fancybot/util/file/ConfigManager.kt @@ -17,4 +17,5 @@ class ConfigManager : JsonFileHandler("config.json") { val accessToken get() = this.read().accessToken val wsType get() = this.read().wsType val listeningGroups get() = this.read().listeningGroups + val qweatherKey get() = this.read().qweatherKey } \ No newline at end of file diff --git a/src/main/resources/config.json b/src/main/resources/config.json index 6dddf0d..3be76cd 100644 --- a/src/main/resources/config.json +++ b/src/main/resources/config.json @@ -7,5 +7,8 @@ "listeningGroups": [ 114514, 1919810 - ] + ], + "qweatherKey": "114514", + "qweatherPublicId": "1919810", + "qweatherPlan": "Free" } \ No newline at end of file