feat: add weather command

This commit is contained in:
2024-09-04 20:01:09 +08:00
parent bba8a99658
commit 7ce76aa0d2
9 files changed
+111 -15

No files matched your search

+1 -1
View File
@@ -9,11 +9,11 @@ motdPingerVersion = "1.0.1"
[libraries] [libraries]
rOneBot = { group = "cn.rtast", name = "ROneBot", version.ref = "ronebotVersion" } 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" } okhttp = { group = "com.squareup.okhttp3", name = "okhttp", version.ref = "okhttpVersion" }
exposedCore = { group = "org.jetbrains.exposed", name = "exposed-core", version.ref = "exposedVersion" } exposedCore = { group = "org.jetbrains.exposed", name = "exposed-core", version.ref = "exposedVersion" }
exposedJDBC = { group = "org.jetbrains.exposed", name = "exposed-jdbc", version.ref = "exposedVersion" } exposedJDBC = { group = "org.jetbrains.exposed", name = "exposed-jdbc", version.ref = "exposedVersion" }
sqliteJDBC = { group = "org.xerial", name = "sqlite-jdbc", version.ref = "sqliteJDBCVersion" } sqliteJDBC = { group = "org.xerial", name = "sqlite-jdbc", version.ref = "sqliteJDBCVersion" }
motdPinger = { group = "cn.rtast", name = "MotdPinger", version.ref = "motdPingerVersion" }
[bundles] [bundles]
+2 -1
View File
@@ -25,7 +25,8 @@ val DEFAULT_CONFIG = Config(
wsType = WSType.Client, wsType = WSType.Client,
accessToken = "114514", accessToken = "114514",
port = 6760, port = 6760,
listeningGroups = listOf(114514, 1919810) listeningGroups = listOf(114514, 1919810),
qweatherKey = "114514"
) )
val ADMINS = listOf( val ADMINS = listOf(
+13 -11
View File
@@ -19,6 +19,7 @@ import cn.rtast.fancybot.commands.MyPointCommand
import cn.rtast.fancybot.commands.QRCodeCommand import cn.rtast.fancybot.commands.QRCodeCommand
import cn.rtast.fancybot.commands.RedeemCommand import cn.rtast.fancybot.commands.RedeemCommand
import cn.rtast.fancybot.commands.SignCommand import cn.rtast.fancybot.commands.SignCommand
import cn.rtast.fancybot.commands.WeatherCommand
import cn.rtast.fancybot.entity.enums.WSType import cn.rtast.fancybot.entity.enums.WSType
import cn.rtast.fancybot.items.BaisiItem import cn.rtast.fancybot.items.BaisiItem
import cn.rtast.fancybot.items.HeisiItem import cn.rtast.fancybot.items.HeisiItem
@@ -66,14 +67,9 @@ class FancyBot : OBMessage {
} }
} }
val commands = listOf( val configManager = ConfigManager()
EchoCommand(), JrrpCommand(), val itemManager = ItemManager()
MusicCommand(), SignCommand(), val signManager = SignManager()
RedeemCommand(), MyPointCommand(),
HitokotoCommand(), FKXQSCommand(),
QRCodeCommand(), AntiRevokeCommand(),
MCPingCommand(), HelpCommand()
)
val items = listOf( val items = listOf(
HeisiItem(), HeisiItem(),
@@ -81,9 +77,15 @@ val items = listOf(
SetuItem() SetuItem()
) )
val configManager = ConfigManager() val commands = listOf(
val itemManager = ItemManager() EchoCommand(), JrrpCommand(),
val signManager = SignManager() MusicCommand(), SignCommand(),
RedeemCommand(), MyPointCommand(),
HitokotoCommand(), FKXQSCommand(),
QRCodeCommand(), AntiRevokeCommand(),
MCPingCommand(), HelpCommand(),
WeatherCommand()
)
suspend fun main() { suspend fun main() {
val fancyBot = FancyBot() val fancyBot = FancyBot()
@@ -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<String>) {
val locationName = args.first()
val lookupLocationId = Http.get<Geo>(
"https://geoapi.qweather.com/v2/city/lookup",
mapOf(
"location" to locationName,
"key" to configManager.qweatherKey
)
).location.first().id
val weather = Http.get<Weather>(
"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)
}
}
@@ -15,5 +15,6 @@ data class Config(
val wsAddress: String, val wsAddress: String,
val accessToken: String, val accessToken: String,
val port: Int, val port: Int,
val listeningGroups: List<Long> val listeningGroups: List<Long>,
val qweatherKey: String
) )
@@ -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<Location>
) {
data class Location(
val id: String
)
}
@@ -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<String>
)
}
@@ -17,4 +17,5 @@ class ConfigManager : JsonFileHandler<Config>("config.json") {
val accessToken get() = this.read<Config>().accessToken val accessToken get() = this.read<Config>().accessToken
val wsType get() = this.read<Config>().wsType val wsType get() = this.read<Config>().wsType
val listeningGroups get() = this.read<Config>().listeningGroups val listeningGroups get() = this.read<Config>().listeningGroups
val qweatherKey get() = this.read<Config>().qweatherKey
} }
+4 -1
View File
@@ -7,5 +7,8 @@
"listeningGroups": [ "listeningGroups": [
114514, 114514,
1919810 1919810
] ],
"qweatherKey": "114514",
"qweatherPublicId": "1919810",
"qweatherPlan": "Free"
} }