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

82 lines
3.2 KiB
Kotlin
Raw Normal View History

2024-09-05 18:29:54 +08:00
/*
* Copyright © 2024 RTAkland
* Author: RTAkland
* Date: 2024/9/5
*/
2024-09-13 12:44:10 +08:00
package cn.rtast.fancybot.commands.lookup
2024-09-05 18:29:54 +08:00
import cn.rtast.fancybot.annotations.CommandDescription
2024-09-28 16:31:55 +08:00
import cn.rtast.fancybot.configManager
2024-09-05 18:29:54 +08:00
import cn.rtast.fancybot.entity.pixiv.Ranking
import cn.rtast.fancybot.util.Http
2024-09-23 20:24:15 +08:00
import cn.rtast.fancybot.util.str.encodeToBase64
2024-09-05 18:29:54 +08:00
import cn.rtast.rob.entity.GroupMessage
import cn.rtast.rob.util.BaseCommand
import cn.rtast.rob.util.ob.MessageChain
2024-09-27 23:31:01 +08:00
import cn.rtast.rob.util.ob.NodeMessageChain
import cn.rtast.rob.util.ob.OneBotListener
2024-09-23 20:24:15 +08:00
import java.net.URI
2024-09-05 18:29:54 +08:00
@CommandDescription("获取Pixiv上的图片")
2024-09-05 18:29:54 +08:00
class PixivCommand : BaseCommand() {
override val commandNames = listOf("/p")
2024-09-05 18:29:54 +08:00
private val pixivRankingURL = "https://proxy.rtast.cn/https/www.pixiv.net/ranking.php?format=json&mode=daily&p=1"
2024-09-23 20:24:15 +08:00
private val imageProxyURL = "https://pixiv.re"
2024-09-05 18:29:54 +08:00
override suspend fun executeGroup(listener: OneBotListener, message: GroupMessage, args: List<String>) {
2024-09-05 18:29:54 +08:00
if (args.isEmpty()) {
val msg = MessageChain.Builder()
2024-09-27 23:31:01 +08:00
.addText("发送`/pixiv <id> | rank` 来进行操作哦~")
2024-09-05 18:29:54 +08:00
.build()
2024-09-27 23:31:01 +08:00
message.reply(msg)
2024-09-05 18:29:54 +08:00
return
}
2024-09-23 20:24:15 +08:00
when (val keyword = args.first()) {
2024-09-05 18:29:54 +08:00
"rank", "排名", "r" -> {
2024-09-27 23:31:01 +08:00
val node = NodeMessageChain.Builder()
.addMessageChain(
MessageChain.Builder().addText("今日Pixiv Ranking~").build(),
message.sender.userId
)
2024-09-05 18:29:54 +08:00
val result = Http.get<Ranking>(pixivRankingURL)
result.contents.asSequence().take(5).forEach {
2024-09-23 20:24:15 +08:00
try {
2024-09-27 23:31:01 +08:00
val msg = MessageChain.Builder()
2024-09-23 20:24:15 +08:00
val imageBase64 = URI(it.getOriginUrl()).toURL().readBytes().encodeToBase64()
2024-09-27 23:31:01 +08:00
msg.addImage(imageBase64, true)
.addText("标题: ${it.title}")
.addNewLine()
.addText("用户: ${it.userName}(${it.userId}) | 作品ID: ${it.illustId}")
2024-09-23 20:24:15 +08:00
.addNewLine()
2024-09-27 23:31:01 +08:00
.addText("日期: ${it.date}")
2024-09-28 16:31:55 +08:00
node.addMessageChain(msg.build(), configManager.selfId)
2024-09-23 20:24:15 +08:00
} catch (_: Exception) {
}
2024-09-05 18:29:54 +08:00
}
2024-09-28 16:31:55 +08:00
node.addMessageChain(MessageChain.Builder()
.addText("图片来源: Pixiv")
.build(), configManager.selfId)
2024-09-27 23:31:01 +08:00
message.reply(node.build())
2024-09-05 18:29:54 +08:00
return
}
else -> {
try {
val id = keyword.toLong()
2024-09-23 20:24:15 +08:00
val imageBase64 = URI("$imageProxyURL/$id.png").toURL().readBytes().encodeToBase64()
2024-09-05 18:29:54 +08:00
val msg = MessageChain.Builder()
2024-09-23 20:24:15 +08:00
.addImage(imageBase64, true)
2024-09-27 23:31:01 +08:00
.addText("图片来源: Pixiv")
2024-09-05 18:29:54 +08:00
.build()
2024-09-27 23:31:01 +08:00
message.reply(msg)
2024-09-05 18:29:54 +08:00
} catch (_: Exception) {
2024-09-27 23:31:01 +08:00
val msg = MessageChain.Builder().addText("输入错误~检查一下有没有输错吧~").build()
message.reply(msg)
2024-09-05 18:29:54 +08:00
}
}
}
}
}