chore: split image bed command to single file

This commit is contained in:
2024-10-13 10:52:37 +08:00
parent ec6e186284
commit c831a76850
3 files changed
+71 -51

No files matched your search

+3 -46
View File
@@ -9,17 +9,14 @@ package cn.rtast.fancybot
import cn.rtast.fancybot.commands.misc.ReactionCommand import cn.rtast.fancybot.commands.misc.ReactionCommand
import cn.rtast.fancybot.commands.misc.ScanQRCodeCommand import cn.rtast.fancybot.commands.misc.ScanQRCodeCommand
import cn.rtast.fancybot.commands.misc.ShortLinkCommand.Companion.makeShortLink
import cn.rtast.fancybot.commands.parse.* import cn.rtast.fancybot.commands.parse.*
import cn.rtast.fancybot.commands.reply.ImageBedCommand
import cn.rtast.fancybot.enums.WSType import cn.rtast.fancybot.enums.WSType
import cn.rtast.fancybot.util.* import cn.rtast.fancybot.util.*
import cn.rtast.fancybot.util.file.getFileType
import cn.rtast.fancybot.util.misc.ImageBed
import cn.rtast.fancybot.util.misc.convertToDate import cn.rtast.fancybot.util.misc.convertToDate
import cn.rtast.fancybot.util.misc.initCommandAndItem import cn.rtast.fancybot.util.misc.initCommandAndItem
import cn.rtast.fancybot.util.misc.initFilesDir import cn.rtast.fancybot.util.misc.initFilesDir
import cn.rtast.fancybot.util.misc.initSetuIndex import cn.rtast.fancybot.util.misc.initSetuIndex
import cn.rtast.fancybot.util.misc.toURL
import cn.rtast.rob.ROneBotFactory import cn.rtast.rob.ROneBotFactory
import cn.rtast.rob.entity.* import cn.rtast.rob.entity.*
import cn.rtast.rob.entity.lagrange.FileEvent import cn.rtast.rob.entity.lagrange.FileEvent
@@ -28,7 +25,6 @@ import cn.rtast.rob.enums.ArrayMessageType
import cn.rtast.rob.enums.QQFace import cn.rtast.rob.enums.QQFace
import cn.rtast.rob.util.ob.MessageChain import cn.rtast.rob.util.ob.MessageChain
import cn.rtast.rob.util.ob.OneBotListener import cn.rtast.rob.util.ob.OneBotListener
import cn.rtast.rob.util.ob.asNode
import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch import kotlinx.coroutines.launch
@@ -39,7 +35,6 @@ class FancyBot : OneBotListener {
private val logger = Logger.getLogger<FancyBot>() private val logger = Logger.getLogger<FancyBot>()
private val coroutineScope = CoroutineScope(Dispatchers.IO) private val coroutineScope = CoroutineScope(Dispatchers.IO)
private val githubRegex = Regex("""github\.com/([^/]+)/([^/]+)""")
override suspend fun onWebsocketOpenEvent() { override suspend fun onWebsocketOpenEvent() {
this.sendPrivateMessage(configManager.noticeUser, "FancyBot启动完成~") this.sendPrivateMessage(configManager.noticeUser, "FancyBot启动完成~")
@@ -65,6 +60,7 @@ class FancyBot : OneBotListener {
message.reply("你原神牛魔呢") message.reply("你原神牛魔呢")
} }
GitHubParseCommand.parse(message)
ReverseGIFCommand.callback(message) ReverseGIFCommand.callback(message)
AsciiArtCommand.callback(message) AsciiArtCommand.callback(message)
ScanQRCodeCommand.callback(message) ScanQRCodeCommand.callback(message)
@@ -96,49 +92,10 @@ class FancyBot : OneBotListener {
} }
if (command.contains("图床")) { if (command.contains("图床")) {
// 将一个图片上传到图床 // 将一个图片上传到图床
val imagesUrl = ImageURLCommand.getImageUrl(getMsg) ImageBedCommand.execute(getMsg, message)
if (imagesUrl.isEmpty()) {
message.reply("这个消息里没有图片呢!")
} else {
try {
if (imagesUrl.size == 1) {
val imageByteArray = imagesUrl.first().toURL().readBytes()
val imageFileType = imageByteArray.getFileType()
val imageBedUrl = ImageBed.upload(imageByteArray, imageFileType)
val msg = MessageChain.Builder()
.addText(imageBedUrl.makeShortLink())
.addNewLine(2)
.addText(imageBedUrl)
.build()
message.reply(msg)
} else {
val messages = mutableListOf<MessageChain>()
imagesUrl.forEach {
val imageByteArray = it.toURL().readBytes()
val imageFileType = imageByteArray.getFileType()
val imageBedUrl = ImageBed.upload(imageByteArray, imageFileType)
val msg = MessageChain.Builder()
.addText(imageBedUrl.makeShortLink())
.addNewLine(2)
.addText(imageBedUrl)
.build()
messages.add(msg)
}
message.reply(messages.asNode(configManager.selfId))
}
} catch (e: Exception) {
message.reply("上传失败: ${e.message}")
}
}
} }
} }
val matchedResult = githubRegex.find(message.rawMessage)
if (message.rawMessage.contains("github.com") && matchedResult != null) {
val (user, repo) = matchedResult.destructured
GitHubParseCommand.parse(message, user, repo)
}
coroutineScope.launch { coroutineScope.launch {
message.message.forEach { message.message.forEach {
if (it.type == ArrayMessageType.image) { if (it.type == ArrayMessageType.image) {
@@ -41,6 +41,7 @@ object GitHubParseCommand {
private val descriptionCustomFont = Font("Serif", Font.PLAIN, 40).deriveFont(Font.BOLD or Font.ITALIC) private val descriptionCustomFont = Font("Serif", Font.PLAIN, 40).deriveFont(Font.BOLD or Font.ITALIC)
private val backgroundColor = Color.WHITE private val backgroundColor = Color.WHITE
private val textColor = Color(60, 60, 60) private val textColor = Color(60, 60, 60)
private val githubRegex = Regex("""github\.com/([^/]+)/([^/]+)""")
private val languageColors = mapOf( private val languageColors = mapOf(
"Kotlin" to Color(169, 123, 255), "Kotlin" to Color(169, 123, 255),
@@ -170,11 +171,15 @@ object GitHubParseCommand {
return canvas.toByteArray().encodeToBase64() return canvas.toByteArray().encodeToBase64()
} }
suspend fun parse(message: GroupMessage, user: String, repo: String) { suspend fun parse(message: GroupMessage) {
val repoStat = this.getRepoStat(user, repo) val matchedResult = githubRegex.find(message.rawMessage)
val image = this.createImage(repoStat) if (message.rawMessage.contains("github.com") && matchedResult != null) {
val msg = MessageChain.Builder().addImage(image, true).build() val (user, repo) = matchedResult.destructured
message.reply(msg) val repoStat = this.getRepoStat(user, repo)
val image = this.createImage(repoStat)
val msg = MessageChain.Builder().addImage(image, true).build()
message.reply(msg)
}
} }
fun creatRepoImage(user: String, repo: String): String { fun creatRepoImage(user: String, repo: String): String {
@@ -0,0 +1,58 @@
/*
* Copyright © 2024 RTAkland
* Author: RTAkland
* Date: 2024/10/13
*/
package cn.rtast.fancybot.commands.reply
import cn.rtast.fancybot.commands.misc.ShortLinkCommand.Companion.makeShortLink
import cn.rtast.fancybot.commands.parse.ImageURLCommand
import cn.rtast.fancybot.configManager
import cn.rtast.fancybot.util.file.getFileType
import cn.rtast.fancybot.util.misc.ImageBed
import cn.rtast.fancybot.util.misc.toURL
import cn.rtast.rob.entity.GetMessage
import cn.rtast.rob.entity.GroupMessage
import cn.rtast.rob.util.ob.MessageChain
import cn.rtast.rob.util.ob.asNode
object ImageBedCommand {
suspend fun execute(getMsg: GetMessage.Data, message: GroupMessage) {
val imagesUrl = ImageURLCommand.getImageUrl(getMsg)
if (imagesUrl.isEmpty()) {
message.reply("这个消息里没有图片呢!")
} else {
try {
if (imagesUrl.size == 1) {
val imageByteArray = imagesUrl.first().toURL().readBytes()
val imageFileType = imageByteArray.getFileType()
val imageBedUrl = ImageBed.upload(imageByteArray, imageFileType)
val msg = MessageChain.Builder()
.addText(imageBedUrl.makeShortLink())
.addNewLine(2)
.addText(imageBedUrl)
.build()
message.reply(msg)
} else {
val messages = mutableListOf<MessageChain>()
imagesUrl.forEach {
val imageByteArray = it.toURL().readBytes()
val imageFileType = imageByteArray.getFileType()
val imageBedUrl = ImageBed.upload(imageByteArray, imageFileType)
val msg = MessageChain.Builder()
.addText(imageBedUrl.makeShortLink())
.addNewLine(2)
.addText(imageBedUrl)
.build()
messages.add(msg)
}
message.reply(messages.asNode(configManager.selfId))
}
} catch (e: Exception) {
message.reply("上传失败: ${e.message}")
}
}
}
}