feat: add gif speed up and speed down command
This commit is contained in:
3 files changed
+56
-13
No files matched your search
@@ -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.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.commands.reply.ImageBedCommand
|
||||||
|
import cn.rtast.fancybot.commands.reply.SpeedUpGIFCommand
|
||||||
import cn.rtast.fancybot.entity.nailong.NaiLongDetectPayload
|
import cn.rtast.fancybot.entity.nailong.NaiLongDetectPayload
|
||||||
import cn.rtast.fancybot.entity.nailong.NaiLongDetectResponse
|
import cn.rtast.fancybot.entity.nailong.NaiLongDetectResponse
|
||||||
import cn.rtast.fancybot.enums.WSType
|
import cn.rtast.fancybot.enums.WSType
|
||||||
@@ -61,17 +62,6 @@ class FancyBot : OneBotListener {
|
|||||||
logger.info("$sender($senderId: $groupId >>> $messageId): $msg")
|
logger.info("$sender($senderId: $groupId >>> $messageId): $msg")
|
||||||
logger.trace("$sender($senderId: $groupId >>> $messageId: $json")
|
logger.trace("$sender($senderId: $groupId >>> $messageId: $json")
|
||||||
|
|
||||||
if (message.images.isNotEmpty()) {
|
|
||||||
message.images.forEach {
|
|
||||||
val payload = NaiLongDetectPayload(it.file).toJson()
|
|
||||||
val result = Http.post<NaiLongDetectResponse>(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" }) {
|
if (message.message.any { it.type == ArrayMessageType.face && it.data.id.toString() == "419" }) {
|
||||||
message.reply("你发牛魔的火车呢, 我直接就是打断")
|
message.reply("你发牛魔的火车呢, 我直接就是打断")
|
||||||
}
|
}
|
||||||
@@ -104,6 +94,10 @@ class FancyBot : OneBotListener {
|
|||||||
val plainTextContent = getMsg.message
|
val plainTextContent = getMsg.message
|
||||||
.filter { it.type == ArrayMessageType.text }
|
.filter { it.type == ArrayMessageType.text }
|
||||||
.joinToString { it.data.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")) {
|
if (command.contains("倒放") || command.contains("/df")) {
|
||||||
// 使用回复消息的方式对一个gif倒放
|
// 使用回复消息的方式对一个gif倒放
|
||||||
ReverseGIFCommand.reverse(message, getMsg)
|
ReverseGIFCommand.reverse(message, getMsg)
|
||||||
@@ -138,6 +132,16 @@ class FancyBot : OneBotListener {
|
|||||||
}
|
}
|
||||||
|
|
||||||
coroutineScope.launch {
|
coroutineScope.launch {
|
||||||
|
if (message.images.isNotEmpty()) {
|
||||||
|
message.images.forEach {
|
||||||
|
val payload = NaiLongDetectPayload(it.file).toJson()
|
||||||
|
val result = Http.post<NaiLongDetectResponse>(configManager.naiLongApiUrl, payload)
|
||||||
|
logger.info("奶龙识别结果: ${result.result}")
|
||||||
|
if (result.result) {
|
||||||
|
message.reply("本群禁止发奶龙!")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
message.message.forEach {
|
message.message.forEach {
|
||||||
if (it.type == ArrayMessageType.image) {
|
if (it.type == ArrayMessageType.image) {
|
||||||
val filename = it.data.file!!.split("=").last() + ".png"
|
val filename = it.data.file!!.split("=").last() + ".png"
|
||||||
|
|||||||
@@ -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)
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -12,7 +12,7 @@ import com.madgag.gif.fmsware.GifDecoder
|
|||||||
import java.awt.image.BufferedImage
|
import java.awt.image.BufferedImage
|
||||||
import java.io.ByteArrayOutputStream
|
import java.io.ByteArrayOutputStream
|
||||||
|
|
||||||
fun GifDecoder.makeGif(frames: List<BufferedImage>): ByteArray {
|
fun GifDecoder.makeGif(frames: List<BufferedImage>, speed: Float? = null): ByteArray {
|
||||||
val estimatedSize = frames.size * 50 * 1024
|
val estimatedSize = frames.size * 50 * 1024
|
||||||
val byteArrayOutputStream = ByteArrayOutputStream(estimatedSize)
|
val byteArrayOutputStream = ByteArrayOutputStream(estimatedSize)
|
||||||
val encoder = AnimatedGifEncoder()
|
val encoder = AnimatedGifEncoder()
|
||||||
@@ -20,7 +20,13 @@ fun GifDecoder.makeGif(frames: List<BufferedImage>): ByteArray {
|
|||||||
encoder.setRepeat(0)
|
encoder.setRepeat(0)
|
||||||
val delays = frames.indices.map { this.getDelay(this.frameCount - it - 1) }
|
val delays = frames.indices.map { this.getDelay(this.frameCount - it - 1) }
|
||||||
frames.indices.forEach { i ->
|
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.addFrame(frames[i])
|
||||||
}
|
}
|
||||||
encoder.finish()
|
encoder.finish()
|
||||||
|
|||||||
Reference in New Issue
Block a user