diff --git a/src/main/kotlin/cn/rtast/fancybot/FancyBot.kt b/src/main/kotlin/cn/rtast/fancybot/FancyBot.kt index c479168..13b99ba 100644 --- a/src/main/kotlin/cn/rtast/fancybot/FancyBot.kt +++ b/src/main/kotlin/cn/rtast/fancybot/FancyBot.kt @@ -9,17 +9,14 @@ package cn.rtast.fancybot import cn.rtast.fancybot.commands.misc.ReactionCommand 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.reply.ImageBedCommand import cn.rtast.fancybot.enums.WSType 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.initCommandAndItem import cn.rtast.fancybot.util.misc.initFilesDir import cn.rtast.fancybot.util.misc.initSetuIndex -import cn.rtast.fancybot.util.misc.toURL import cn.rtast.rob.ROneBotFactory import cn.rtast.rob.entity.* 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.util.ob.MessageChain import cn.rtast.rob.util.ob.OneBotListener -import cn.rtast.rob.util.ob.asNode import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.launch @@ -39,7 +35,6 @@ class FancyBot : OneBotListener { private val logger = Logger.getLogger() private val coroutineScope = CoroutineScope(Dispatchers.IO) - private val githubRegex = Regex("""github\.com/([^/]+)/([^/]+)""") override suspend fun onWebsocketOpenEvent() { this.sendPrivateMessage(configManager.noticeUser, "FancyBot启动完成~") @@ -65,6 +60,7 @@ class FancyBot : OneBotListener { message.reply("你原神牛魔呢") } + GitHubParseCommand.parse(message) ReverseGIFCommand.callback(message) AsciiArtCommand.callback(message) ScanQRCodeCommand.callback(message) @@ -96,49 +92,10 @@ class FancyBot : OneBotListener { } if (command.contains("图床")) { // 将一个图片上传到图床 - 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() - 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}") - } - } + ImageBedCommand.execute(getMsg, 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 { message.message.forEach { if (it.type == ArrayMessageType.image) { diff --git a/src/main/kotlin/cn/rtast/fancybot/commands/parse/GitHubParseCommand.kt b/src/main/kotlin/cn/rtast/fancybot/commands/parse/GitHubParseCommand.kt index 8bbf436..2ef7af3 100644 --- a/src/main/kotlin/cn/rtast/fancybot/commands/parse/GitHubParseCommand.kt +++ b/src/main/kotlin/cn/rtast/fancybot/commands/parse/GitHubParseCommand.kt @@ -41,6 +41,7 @@ object GitHubParseCommand { private val descriptionCustomFont = Font("Serif", Font.PLAIN, 40).deriveFont(Font.BOLD or Font.ITALIC) private val backgroundColor = Color.WHITE private val textColor = Color(60, 60, 60) + private val githubRegex = Regex("""github\.com/([^/]+)/([^/]+)""") private val languageColors = mapOf( "Kotlin" to Color(169, 123, 255), @@ -170,11 +171,15 @@ object GitHubParseCommand { return canvas.toByteArray().encodeToBase64() } - suspend fun parse(message: GroupMessage, user: String, repo: String) { - val repoStat = this.getRepoStat(user, repo) - val image = this.createImage(repoStat) - val msg = MessageChain.Builder().addImage(image, true).build() - message.reply(msg) + suspend fun parse(message: GroupMessage) { + val matchedResult = githubRegex.find(message.rawMessage) + if (message.rawMessage.contains("github.com") && matchedResult != null) { + val (user, repo) = matchedResult.destructured + 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 { diff --git a/src/main/kotlin/cn/rtast/fancybot/commands/reply/ImageBedCommand.kt b/src/main/kotlin/cn/rtast/fancybot/commands/reply/ImageBedCommand.kt new file mode 100644 index 0000000..58b14b3 --- /dev/null +++ b/src/main/kotlin/cn/rtast/fancybot/commands/reply/ImageBedCommand.kt @@ -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() + 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}") + } + } + } +} \ No newline at end of file