From c0fa5c3a49498b3b999f9eb35fa18b11c8aa8e8f Mon Sep 17 00:00:00 2001 From: RTAkland Date: Sun, 27 Oct 2024 17:43:06 +0800 Subject: [PATCH] feat: add gif speed up and speed down command --- src/main/kotlin/cn/rtast/fancybot/FancyBot.kt | 26 ++++++++------- .../commands/reply/SpeedUpGIFCommand.kt | 33 +++++++++++++++++++ .../kotlin/cn/rtast/fancybot/util/misc/GIF.kt | 10 ++++-- 3 files changed, 56 insertions(+), 13 deletions(-) create mode 100644 src/main/kotlin/cn/rtast/fancybot/commands/reply/SpeedUpGIFCommand.kt diff --git a/src/main/kotlin/cn/rtast/fancybot/FancyBot.kt b/src/main/kotlin/cn/rtast/fancybot/FancyBot.kt index 1660c4c..cbc77af 100644 --- a/src/main/kotlin/cn/rtast/fancybot/FancyBot.kt +++ b/src/main/kotlin/cn/rtast/fancybot/FancyBot.kt @@ -13,6 +13,7 @@ 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.commands.reply.SpeedUpGIFCommand import cn.rtast.fancybot.entity.nailong.NaiLongDetectPayload import cn.rtast.fancybot.entity.nailong.NaiLongDetectResponse import cn.rtast.fancybot.enums.WSType @@ -61,17 +62,6 @@ class FancyBot : OneBotListener { logger.info("$sender($senderId: $groupId >>> $messageId): $msg") logger.trace("$sender($senderId: $groupId >>> $messageId: $json") - if (message.images.isNotEmpty()) { - message.images.forEach { - val payload = NaiLongDetectPayload(it.file).toJson() - val result = Http.post(configManager.naiLongApiUrl, payload) - logger.info("奶龙识别结果: ${result.result}") - if (result.result) { - message.reply("本群禁止发奶龙!") - } - } - } - if (message.message.any { it.type == ArrayMessageType.face && it.data.id.toString() == "419" }) { message.reply("你发牛魔的火车呢, 我直接就是打断") } @@ -104,6 +94,10 @@ class FancyBot : OneBotListener { val plainTextContent = getMsg.message .filter { it.type == ArrayMessageType.text } .joinToString { it.data.text!! } + if (command.contains("加速")) { + val multiply = command.split("加速").last().toFloat() + SpeedUpGIFCommand.speedUp(message, getMsg, multiply) + } if (command.contains("倒放") || command.contains("/df")) { // 使用回复消息的方式对一个gif倒放 ReverseGIFCommand.reverse(message, getMsg) @@ -138,6 +132,16 @@ class FancyBot : OneBotListener { } coroutineScope.launch { + if (message.images.isNotEmpty()) { + message.images.forEach { + val payload = NaiLongDetectPayload(it.file).toJson() + val result = Http.post(configManager.naiLongApiUrl, payload) + logger.info("奶龙识别结果: ${result.result}") + if (result.result) { + message.reply("本群禁止发奶龙!") + } + } + } message.message.forEach { if (it.type == ArrayMessageType.image) { val filename = it.data.file!!.split("=").last() + ".png" diff --git a/src/main/kotlin/cn/rtast/fancybot/commands/reply/SpeedUpGIFCommand.kt b/src/main/kotlin/cn/rtast/fancybot/commands/reply/SpeedUpGIFCommand.kt new file mode 100644 index 0000000..5ec3b99 --- /dev/null +++ b/src/main/kotlin/cn/rtast/fancybot/commands/reply/SpeedUpGIFCommand.kt @@ -0,0 +1,33 @@ +/* + * Copyright © 2024 RTAkland + * Author: RTAkland + * Date: 2024/10/27 + */ + + +package cn.rtast.fancybot.commands.reply + +import cn.rtast.fancybot.commands.misc.ShortLinkCommand.Companion.makeShortLink +import cn.rtast.fancybot.commands.parse.AsciiArtCommand +import cn.rtast.fancybot.util.misc.ImageBed +import cn.rtast.fancybot.util.misc.makeGif +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 com.madgag.gif.fmsware.GifDecoder + +object SpeedUpGIFCommand { + suspend fun speedUp(message: GroupMessage, getMsg: GetMessage.Message, multiply: Float) { + val gifUrl = AsciiArtCommand.getImageUrl(getMsg) + val gifStream = gifUrl.toURL().openStream() + val decoder = GifDecoder() + decoder.read(gifStream) + val frames = (0 until decoder.frameCount).map { decoder.getFrame(it) }.reversed() + val gifBase64 = decoder.makeGif(frames, multiply) + val imgBedUrl = ImageBed.upload(gifBase64) + val shortLink = imgBedUrl.makeShortLink() + val msg = MessageChain.Builder().addImage(imgBedUrl).addText(shortLink).build() + message.reply(msg) + } +} \ No newline at end of file diff --git a/src/main/kotlin/cn/rtast/fancybot/util/misc/GIF.kt b/src/main/kotlin/cn/rtast/fancybot/util/misc/GIF.kt index 528c6d5..1df2a88 100644 --- a/src/main/kotlin/cn/rtast/fancybot/util/misc/GIF.kt +++ b/src/main/kotlin/cn/rtast/fancybot/util/misc/GIF.kt @@ -12,7 +12,7 @@ import com.madgag.gif.fmsware.GifDecoder import java.awt.image.BufferedImage import java.io.ByteArrayOutputStream -fun GifDecoder.makeGif(frames: List): ByteArray { +fun GifDecoder.makeGif(frames: List, speed: Float? = null): ByteArray { val estimatedSize = frames.size * 50 * 1024 val byteArrayOutputStream = ByteArrayOutputStream(estimatedSize) val encoder = AnimatedGifEncoder() @@ -20,7 +20,13 @@ fun GifDecoder.makeGif(frames: List): ByteArray { encoder.setRepeat(0) val delays = frames.indices.map { this.getDelay(this.frameCount - it - 1) } frames.indices.forEach { i -> - encoder.setDelay(delays[i]) + if (speed == null) { + encoder.setDelay(delays[i]) + } else { + var delay = this.getDelay(i) + delay = (delay / speed).toInt() + encoder.setDelay(delay.coerceAtLeast(1)) + } encoder.addFrame(frames[i]) } encoder.finish()