mirror of
https://github.com/jie65535/mirai-console-jpa-plugin.git
synced 2024-07-27 19:15:06 +08:00
V0.1.1 实现 #1 支持保存转发消息中的图片
This commit is contained in:
parent
bcfc156bd0
commit
8a1b8d5a48
@ -7,7 +7,7 @@ plugins {
|
|||||||
}
|
}
|
||||||
|
|
||||||
group = "top.jie65535"
|
group = "top.jie65535"
|
||||||
version = "0.1.0"
|
version = "0.1.1"
|
||||||
|
|
||||||
repositories {
|
repositories {
|
||||||
mavenLocal()
|
mavenLocal()
|
||||||
|
@ -5,8 +5,10 @@ import net.mamoe.mirai.console.plugin.jvm.JvmPluginDescription
|
|||||||
import net.mamoe.mirai.console.plugin.jvm.KotlinPlugin
|
import net.mamoe.mirai.console.plugin.jvm.KotlinPlugin
|
||||||
import net.mamoe.mirai.event.events.MessageEvent
|
import net.mamoe.mirai.event.events.MessageEvent
|
||||||
import net.mamoe.mirai.event.globalEventChannel
|
import net.mamoe.mirai.event.globalEventChannel
|
||||||
|
import net.mamoe.mirai.message.data.ForwardMessage
|
||||||
import net.mamoe.mirai.message.data.Image
|
import net.mamoe.mirai.message.data.Image
|
||||||
import net.mamoe.mirai.message.data.Image.Key.queryUrl
|
import net.mamoe.mirai.message.data.Image.Key.queryUrl
|
||||||
|
import net.mamoe.mirai.message.data.MessageChain
|
||||||
import net.mamoe.mirai.utils.info
|
import net.mamoe.mirai.utils.info
|
||||||
import okhttp3.OkHttpClient
|
import okhttp3.OkHttpClient
|
||||||
import okhttp3.Request
|
import okhttp3.Request
|
||||||
@ -18,7 +20,7 @@ object JPictureArchiving : KotlinPlugin(
|
|||||||
JvmPluginDescription(
|
JvmPluginDescription(
|
||||||
id = "top.jie65535.mirai-console-jpa-plugin",
|
id = "top.jie65535.mirai-console-jpa-plugin",
|
||||||
name = "J Picture Archiving",
|
name = "J Picture Archiving",
|
||||||
version = "0.1.0"
|
version = "0.1.1"
|
||||||
) {
|
) {
|
||||||
author("jie65535")
|
author("jie65535")
|
||||||
info("这个插件只做一件事,将机器人收到的所有图片存档")
|
info("这个插件只做一件事,将机器人收到的所有图片存档")
|
||||||
@ -35,23 +37,36 @@ object JPictureArchiving : KotlinPlugin(
|
|||||||
JPACommand.register()
|
JPACommand.register()
|
||||||
|
|
||||||
globalEventChannel().subscribeAlways<MessageEvent> {
|
globalEventChannel().subscribeAlways<MessageEvent> {
|
||||||
message.filterIsInstance<Image>().forEach {
|
saveImages(this.subject.id.toString(), message)
|
||||||
val url = it.queryUrl()
|
}
|
||||||
val filePath = "${this.subject.id}/${it.imageId}"
|
}
|
||||||
val file = if (JPAPluginConfig.archiveDirectory.isBlank()) {
|
|
||||||
resolveDataFile(filePath)
|
private suspend fun saveImages(from: String, message: MessageChain) {
|
||||||
} else {
|
val fm = message[ForwardMessage]
|
||||||
File(JPAPluginConfig.archiveDirectory, filePath)
|
if (fm != null) {
|
||||||
}
|
fm.nodeList.forEach { saveImages(from, it.messageChain) }
|
||||||
if (!file.exists()) {
|
} else {
|
||||||
val request = Request.Builder().url(url).build()
|
message.filterIsInstance<Image>().forEach { img ->
|
||||||
val imageByte = okHttpClient.newCall(request).execute().body!!.bytes()
|
saveImage(from, img)
|
||||||
val fileParent = file.parentFile
|
|
||||||
if (!fileParent.exists()) fileParent.mkdirs()
|
|
||||||
file.writeBytes(imageByte)
|
|
||||||
logger.info("Saved ${file.path}.")
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private suspend fun saveImage(from: String, image: Image) {
|
||||||
|
val url = image.queryUrl()
|
||||||
|
val filePath = "${from}/${image.imageId}"
|
||||||
|
val file = if (JPAPluginConfig.archiveDirectory.isBlank()) {
|
||||||
|
resolveDataFile(filePath)
|
||||||
|
} else {
|
||||||
|
File(JPAPluginConfig.archiveDirectory, filePath)
|
||||||
|
}
|
||||||
|
if (!file.exists()) {
|
||||||
|
val request = Request.Builder().url(url).build()
|
||||||
|
val imageByte = okHttpClient.newCall(request).execute().body!!.bytes()
|
||||||
|
val fileParent = file.parentFile
|
||||||
|
if (!fileParent.exists()) fileParent.mkdirs()
|
||||||
|
file.writeBytes(imageByte)
|
||||||
|
logger.info("Saved ${file.path}.")
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user