feat: add weather command
This commit is contained in:
9 files changed
+111
-15
No files matched your search
@@ -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(
|
||||
|
||||
@@ -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()
|
||||
|
||||
@@ -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 accessToken: String,
|
||||
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 wsType get() = this.read<Config>().wsType
|
||||
val listeningGroups get() = this.read<Config>().listeningGroups
|
||||
val qweatherKey get() = this.read<Config>().qweatherKey
|
||||
}
|
||||
Reference in New Issue
Block a user