Files
FancyBot/src/main/kotlin/cn/rtast/fancybot/commands/lookup/DomainPriceCommand.kt
T

112 lines
4.2 KiB
Kotlin
Raw Normal View History

2024-09-17 11:27:20 +08:00
/*
* Copyright © 2024 RTAkland
* Author: RTAkland
* Date: 2024/9/17
*/
package cn.rtast.fancybot.commands.lookup
import cn.rtast.fancybot.annotations.CommandDescription
2024-09-17 11:27:20 +08:00
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.OneBotListener
2024-09-17 11:27:20 +08:00
import java.awt.Color
2024-09-17 12:29:36 +08:00
import java.awt.Font
2024-09-17 11:27:20 +08:00
import java.awt.image.BufferedImage
@CommandDescription("查询域名价格")
2024-09-17 11:27:20 +08:00
class DomainPriceCommand : BaseCommand() {
override val commandNames = listOf("/domain")
2024-09-17 12:41:09 +08:00
private val canvasWidth = 400
private val canvasHeight = 300
2024-09-17 12:33:02 +08:00
private val backgroundColor = Color(255, 250, 200)
2024-09-17 11:27:20 +08:00
private val secondLayerColor = Color(240, 255, 240)
private val textColor = Color.BLACK
2024-09-17 11:57:33 +08:00
private val domainApi = "https://dnspod.cloud.tencent.com"
2024-09-17 12:41:09 +08:00
private val font = Font("Serif", Font.PLAIN, 20)
2024-09-17 11:27:20 +08:00
2024-09-17 11:57:33 +08:00
private fun createCommonCanvas(): BufferedImage {
2024-09-17 11:27:20 +08:00
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)
2024-09-17 11:57:33 +08:00
g2d.dispose()
return canvas
}
private fun createDomainPriceCard(suffix: String, domainPrice: Pair<Int, Int>): String {
val canvas = this.createCommonCanvas()
val g2d = canvas.createGraphics()
2024-09-17 12:29:36 +08:00
g2d.font = font
2024-09-17 11:27:20 +08:00
g2d.color = textColor
2024-09-17 12:41:09 +08:00
g2d.drawString("域名后缀: $suffix 首年注册价格: ${domainPrice.first}元", 50, 130)
2024-09-17 12:45:43 +08:00
g2d.drawString("续费价格: ${domainPrice.second}元", 50, 160)
g2d.drawString("数据来源: 腾讯云", 50, 190)
2024-09-17 11:27:20 +08:00
g2d.dispose()
return canvas.toByteArray().encodeToBase64()
}
2024-09-17 11:57:33 +08:00
private fun createRegisterCard(
domain: String,
available: Boolean,
premium: Boolean,
domainPrice: Pair<Int, Int>
): String {
val canvas = this.createCommonCanvas()
val g2d = canvas.createGraphics()
2024-09-17 12:29:36 +08:00
g2d.font = font
2024-09-17 11:57:33 +08:00
g2d.color = textColor
2024-09-17 12:41:09 +08:00
g2d.drawString("域名$domain${if (available) "可以注册" else "已被注册"}", 50, 110)
2024-09-17 12:45:43 +08:00
g2d.drawString("${if (premium) "是" else "不是"}白金域名", 50, 140)
g2d.drawString("首年年注册价格: ${domainPrice.first}元", 50, 170)
g2d.drawString("续费价格: ${domainPrice.second}元", 50, 200)
g2d.drawString("数据来源: 腾讯云", 50, 230)
2024-09-17 11:57:33 +08:00
g2d.dispose()
return canvas.toByteArray().encodeToBase64()
}
2024-09-17 11:27:20 +08:00
override suspend fun executeGroup(listener: OneBotListener, message: GroupMessage, args: List<String>) {
2024-09-17 11:27:20 +08:00
if (args.isEmpty()) {
val msg = MessageChain.Builder()
.addAt(message.sender.userId)
2024-09-17 11:57:33 +08:00
.addText("发送`/domain <域名>`即可查询对应的注册和续费价格~")
2024-09-17 11:27:20 +08:00
.build()
listener.sendGroupMessage(message.groupId, msg)
return
}
val suffix = args.first()
2024-09-17 11:57:33 +08:00
val domain = if (suffix.contains(".")) args.first() else "rtast.$suffix"
2024-09-17 11:27:20 +08:00
val response = Http.post<PriceResponse>(
2024-09-17 16:56:44 +08:00
"$domainApi/cgi/capi", PricePayload(listOf(domain)).toJson(), params = mapOf("action" to "BatchCheckDomain")
2024-09-17 11:27:20 +08:00
).data.response.domainList.first()
2024-09-17 11:57:33 +08:00
val cardImage =
if (suffix.contains(".")) {
this.createRegisterCard(
domain,
response.available,
response.premium,
response.realPrice to response.price
)
} else {
this.createDomainPriceCard(suffix, response.realPrice to response.price)
}
2024-09-17 11:27:20 +08:00
val msg = MessageChain.Builder()
.addAt(message.sender.userId)
.addImage(cardImage, true)
.build()
listener.sendGroupMessage(message.groupId, msg)
}
}