2024-10-13 10:52:37 +08:00
|
|
|
/*
|
|
|
|
|
* 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
|
2024-11-08 20:59:51 +08:00
|
|
|
import cn.rtast.rob.onebot.MessageChain
|
|
|
|
|
import cn.rtast.rob.onebot.toNode
|
2024-10-13 10:55:08 +08:00
|
|
|
import okio.IOException
|
2024-10-13 10:52:37 +08:00
|
|
|
|
|
|
|
|
object ImageBedCommand {
|
2024-10-25 14:25:43 +08:00
|
|
|
suspend fun execute(getMsg: GetMessage.Message, message: GroupMessage) {
|
2024-10-13 10:52:37 +08:00
|
|
|
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)
|
|
|
|
|
}
|
2024-11-07 19:09:08 +08:00
|
|
|
message.reply(messages.toNode(configManager.selfId))
|
2024-10-13 10:52:37 +08:00
|
|
|
}
|
2024-10-13 10:55:08 +08:00
|
|
|
} catch (_: IOException) {
|
|
|
|
|
message.reply("上传失败: 图片已过期")
|
2024-10-13 10:52:37 +08:00
|
|
|
} catch (e: Exception) {
|
2024-10-13 10:55:08 +08:00
|
|
|
e.printStackTrace()
|
2024-10-13 10:52:37 +08:00
|
|
|
message.reply("上传失败: ${e.message}")
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|