From 8a1b8d5a487f969fe6a3fd46ce8d654c74fef33d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=AD=B1=E5=82=91?= <840465812@qq.com> Date: Wed, 22 Dec 2021 21:26:56 +0800 Subject: [PATCH] =?UTF-8?q?V0.1.1=20=E5=AE=9E=E7=8E=B0=20#1=20=E6=94=AF?= =?UTF-8?q?=E6=8C=81=E4=BF=9D=E5=AD=98=E8=BD=AC=E5=8F=91=E6=B6=88=E6=81=AF?= =?UTF-8?q?=E4=B8=AD=E7=9A=84=E5=9B=BE=E7=89=87?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- build.gradle.kts | 2 +- src/main/kotlin/JPictureArchiving.kt | 49 ++++++++++++++++++---------- 2 files changed, 33 insertions(+), 18 deletions(-) diff --git a/build.gradle.kts b/build.gradle.kts index ddae64a..31186ce 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -7,7 +7,7 @@ plugins { } group = "top.jie65535" -version = "0.1.0" +version = "0.1.1" repositories { mavenLocal() diff --git a/src/main/kotlin/JPictureArchiving.kt b/src/main/kotlin/JPictureArchiving.kt index 4d91e5f..7641085 100644 --- a/src/main/kotlin/JPictureArchiving.kt +++ b/src/main/kotlin/JPictureArchiving.kt @@ -5,8 +5,10 @@ import net.mamoe.mirai.console.plugin.jvm.JvmPluginDescription import net.mamoe.mirai.console.plugin.jvm.KotlinPlugin import net.mamoe.mirai.event.events.MessageEvent 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.Key.queryUrl +import net.mamoe.mirai.message.data.MessageChain import net.mamoe.mirai.utils.info import okhttp3.OkHttpClient import okhttp3.Request @@ -18,7 +20,7 @@ object JPictureArchiving : KotlinPlugin( JvmPluginDescription( id = "top.jie65535.mirai-console-jpa-plugin", name = "J Picture Archiving", - version = "0.1.0" + version = "0.1.1" ) { author("jie65535") info("这个插件只做一件事,将机器人收到的所有图片存档") @@ -35,23 +37,36 @@ object JPictureArchiving : KotlinPlugin( JPACommand.register() globalEventChannel().subscribeAlways { - message.filterIsInstance().forEach { - val url = it.queryUrl() - val filePath = "${this.subject.id}/${it.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}.") - } + saveImages(this.subject.id.toString(), message) + } + } + + private suspend fun saveImages(from: String, message: MessageChain) { + val fm = message[ForwardMessage] + if (fm != null) { + fm.nodeList.forEach { saveImages(from, it.messageChain) } + } else { + message.filterIsInstance().forEach { img -> + saveImage(from, img) } } } + + 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}.") + } + } }