From 5c3a0dc39311810da1a8ad34a6e26ba466bc902f Mon Sep 17 00:00:00 2001 From: RTAkland Date: Tue, 17 Sep 2024 11:27:20 +0800 Subject: [PATCH] feat: add domain price query command --- src/main/kotlin/cn/rtast/fancybot/FancyBot.kt | 3 +- .../commands/lookup/DomainPriceCommand.kt | 78 +++++++++++++++++++ .../fancybot/entity/domain/PricePayload.kt | 28 +++++++ .../fancybot/entity/domain/PriceResponse.kt | 31 ++++++++ 4 files changed, 139 insertions(+), 1 deletion(-) create mode 100644 src/main/kotlin/cn/rtast/fancybot/commands/lookup/DomainPriceCommand.kt create mode 100644 src/main/kotlin/cn/rtast/fancybot/entity/domain/PricePayload.kt create mode 100644 src/main/kotlin/cn/rtast/fancybot/entity/domain/PriceResponse.kt diff --git a/src/main/kotlin/cn/rtast/fancybot/FancyBot.kt b/src/main/kotlin/cn/rtast/fancybot/FancyBot.kt index c837fa5..e0790fb 100644 --- a/src/main/kotlin/cn/rtast/fancybot/FancyBot.kt +++ b/src/main/kotlin/cn/rtast/fancybot/FancyBot.kt @@ -10,6 +10,7 @@ package cn.rtast.fancybot import cn.rtast.fancybot.commands.* import cn.rtast.fancybot.commands.CompilerCommand import cn.rtast.fancybot.commands.lookup.CigaretteCommand +import cn.rtast.fancybot.commands.lookup.DomainPriceCommand import cn.rtast.fancybot.commands.lookup.GPTCommand import cn.rtast.fancybot.commands.lookup.GithubUserCommand import cn.rtast.fancybot.commands.lookup.MCPingCommand @@ -151,7 +152,7 @@ val commands = listOf( JiJianCommand(), LikeMeCommand(), CompilerCommand(), GPTCommand(), GithubUserCommand(), AboutCommand(), - MusicPlayUrlCommand() + MusicPlayUrlCommand(), DomainPriceCommand() ) suspend fun main() { diff --git a/src/main/kotlin/cn/rtast/fancybot/commands/lookup/DomainPriceCommand.kt b/src/main/kotlin/cn/rtast/fancybot/commands/lookup/DomainPriceCommand.kt new file mode 100644 index 0000000..c26853f --- /dev/null +++ b/src/main/kotlin/cn/rtast/fancybot/commands/lookup/DomainPriceCommand.kt @@ -0,0 +1,78 @@ +/* + * Copyright © 2024 RTAkland + * Author: RTAkland + * Date: 2024/9/17 + */ + + +package cn.rtast.fancybot.commands.lookup + +import cn.rtast.fancybot.entity.domain.PricePayload +import cn.rtast.fancybot.entity.domain.PriceResponse +import cn.rtast.fancybot.util.Http +import cn.rtast.fancybot.util.str.encodeToBase64 +import cn.rtast.fancybot.util.str.toJson +import cn.rtast.fancybot.util.toByteArray +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 +import java.awt.Color +import java.awt.image.BufferedImage + +class DomainPriceCommand : BaseCommand() { + override val commandNames = listOf("/domain") + + private val canvasWidth = 300 + private val canvasHeight = 200 + private val backgroundColor = Color(255, 250, 205) + private val secondLayerColor = Color(240, 255, 240) + private val textColor = Color.BLACK + + private fun createDomainCard(suffix: String, domainPrice: Pair): String { + val canvas = BufferedImage(canvasWidth, canvasHeight, BufferedImage.TYPE_INT_RGB) + val g2d = canvas.createGraphics() + g2d.color = backgroundColor + g2d.fillRect(0, 0, canvasWidth, canvasHeight) + g2d.color = secondLayerColor + g2d.fillRect(20, 20, canvasWidth - 40, canvasHeight - 40) + g2d.color = textColor + g2d.drawString("域名后缀: $suffix 首年注册价格: ${domainPrice.first}元", 50, 80) + g2d.drawString("续费价格: ${domainPrice.second}元", 50, 100) + g2d.drawString("数据来源: 腾讯云", 50, 120) + g2d.dispose() + return canvas.toByteArray().encodeToBase64() + } + + private val domainApi = "https://dnspod.cloud.tencent.com" + + override suspend fun executeGroup(listener: OBMessage, message: GroupMessage, args: List) { + if (args.isEmpty()) { + val msg = MessageChain.Builder() + .addAt(message.sender.userId) + .addText("发送`/domain <域名后缀>`即可查询对应的注册和续费价格~") + .build() + listener.sendGroupMessage(message.groupId, msg) + return + } + val suffix = args.first() + val response = Http.post( + "$domainApi/cgi/capi", + PricePayload(listOf("rtast.$suffix")).toJson(), + params = mapOf( + "action" to "BatchCheckDomain", + "from" to "domain_buy", + "csrfCode" to "", + "uin" to 0, + "_" to "1726541082487", + "notUseInnerMark" to 1 + ) + ).data.response.domainList.first() + val cardImage = this.createDomainCard(suffix, response.realPrice to response.price) + val msg = MessageChain.Builder() + .addAt(message.sender.userId) + .addImage(cardImage, true) + .build() + listener.sendGroupMessage(message.groupId, msg) + } +} \ No newline at end of file diff --git a/src/main/kotlin/cn/rtast/fancybot/entity/domain/PricePayload.kt b/src/main/kotlin/cn/rtast/fancybot/entity/domain/PricePayload.kt new file mode 100644 index 0000000..d86a335 --- /dev/null +++ b/src/main/kotlin/cn/rtast/fancybot/entity/domain/PricePayload.kt @@ -0,0 +1,28 @@ +/* + * Copyright © 2024 RTAkland + * Author: RTAkland + * Date: 2024/9/17 + */ + + +package cn.rtast.fancybot.entity.domain + +import com.google.gson.annotations.SerializedName + +data class PricePayload( + @SerializedName("DomainList") + val domainList: List, + @SerializedName("Filter") + val filter: Int = 0, + @SerializedName("Period") + val period: Int = 1, + @SerializedName("HashId") + val hashId: String = "1726541001813_9", + @SerializedName("SaveDomainSearch") + val saveDomainSearch: Boolean = true, + val serviceType: String = "domain", + @SerializedName("Version") + val version: String = "2018-08-08", + @SerializedName("Action") + val action: String = "BatchCheckDomain" +) \ No newline at end of file diff --git a/src/main/kotlin/cn/rtast/fancybot/entity/domain/PriceResponse.kt b/src/main/kotlin/cn/rtast/fancybot/entity/domain/PriceResponse.kt new file mode 100644 index 0000000..a980595 --- /dev/null +++ b/src/main/kotlin/cn/rtast/fancybot/entity/domain/PriceResponse.kt @@ -0,0 +1,31 @@ +/* + * Copyright © 2024 RTAkland + * Author: RTAkland + * Date: 2024/9/17 + */ + + +package cn.rtast.fancybot.entity.domain + +import com.google.gson.annotations.SerializedName + +data class PriceResponse( + val data: Data +) { + data class Data( + @SerializedName("Response") + val response: Response + ) + + data class Response( + @SerializedName("DomainList") + val domainList: List + ) + + data class Domain( + @SerializedName("RealPrice") + val realPrice: Int, + @SerializedName("Price") + val price: Int + ) +} \ No newline at end of file