feat: add the cat command

This commit is contained in:
2024-10-08 15:08:27 +08:00
parent 97e62d5422
commit e4d6e683d3
5 files changed
+69 -1

No files matched your search

+1 -1
View File
@@ -121,5 +121,5 @@ val commands = listOf(
GithubLatestCommitCommand(), RandomMusicCommand(), GithubLatestCommitCommand(), RandomMusicCommand(),
ZDJDCommand(), ScanQRCodeCommand(), ZDJDCommand(), ScanQRCodeCommand(),
TTSCommand(), SlimeChunkHelperCommand(), TTSCommand(), SlimeChunkHelperCommand(),
SupportMeCommand() SupportMeCommand(), TheCatCommand(),
) )
@@ -0,0 +1,33 @@
/*
* Copyright © 2024 RTAkland
* Author: RTAkland
* Date: 2024/10/8
*/
package cn.rtast.fancybot.commands.lookup
import cn.rtast.fancybot.entity.thecat.Cat
import cn.rtast.fancybot.util.Http
import cn.rtast.fancybot.util.str.encodeToBase64
import cn.rtast.fancybot.util.str.fromArrayJson
import cn.rtast.fancybot.util.str.proxy
import cn.rtast.fancybot.util.toURL
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
class TheCatCommand : BaseCommand() {
override val commandNames = listOf("随机猫咪", "随机猫猫")
private val theCatApiUrl = "https://api.thecatapi.com/v1/images/search"
override suspend fun executeGroup(listener: OneBotListener, message: GroupMessage, args: List<String>) {
val imageBase64= Http.get(theCatApiUrl)
.fromArrayJson<List<Cat>>().first().url
.proxy().toURL().readBytes().encodeToBase64()
val msg = MessageChain.Builder().addImage(imageBase64, true).build()
message.reply(msg)
}
}
@@ -0,0 +1,12 @@
/*
* Copyright © 2024 RTAkland
* Author: RTAkland
* Date: 2024/10/8
*/
package cn.rtast.fancybot.entity.thecat
data class Cat(
val url: String,
)
@@ -0,0 +1,19 @@
/*
* Copyright © 2024 RTAkland
* Author: RTAkland
* Date: 2024/10/8
*/
package cn.rtast.fancybot.util
import java.net.URI
import java.net.URL
fun String.toURL(): URL {
return URI(this).toURL()
}
fun String.toURI(): URI {
return URI(this)
}
@@ -46,3 +46,7 @@ fun Int.formatToMinutes(): String {
val remainingSeconds = this % 60 val remainingSeconds = this % 60
return "$minutes:${if (remainingSeconds < 10) "0" else ""}$remainingSeconds" return "$minutes:${if (remainingSeconds < 10) "0" else ""}$remainingSeconds"
} }
fun String.proxy(): String {
return this.replace("https://", "https://proxy.rtast.cn/https/")
}