jiahua.liu 6 lat temu
rodzic
commit
5402505a8f

+ 5 - 3
mirai-console/src/main/kotlin/net/mamoe/mirai/console/plugins/ConfigSection.kt

@@ -184,9 +184,11 @@ class WithDefaultWriteLoader<T : Any>(
         prop: KProperty<*>
     ): ReadWriteProperty<Any, T> {
         val defaultValue by lazy { defaultValue.invoke() }
-        config.setIfAbsent(prop.name, defaultValue)
-        if (save) {
-            config.save()
+        if (!config.contains(prop.name)) {
+            config[prop.name] = defaultValue
+            if (save) {
+                config.save()
+            }
         }
         return object : ReadWriteProperty<Any, T> {
             override fun getValue(thisRef: Any, property: KProperty<*>): T {

+ 10 - 1
mirai-console/src/main/kotlin/net/mamoe/mirai/console/plugins/PluginBase.kt

@@ -90,15 +90,24 @@ abstract class PluginBase(coroutineContext: CoroutineContext) : CoroutineScope {
     fun getPluginManager() = PluginManager
 
     val logger: MiraiLogger by lazy {
-        DefaultLogger(pluginDescription.name)
+        SimpleLogger("Plugin ${pluginDescription.name}") { _, message, e ->
+            MiraiConsole.logger("[${pluginDescription.name}]", 0, message)
+            if (e != null) {
+                MiraiConsole.logger("[${pluginDescription.name}]", 0, e.toString())
+                e.printStackTrace()
+            }
+        }
     }
 
+
     fun getResources(fileName: String): InputStream? {
         return PluginManager.getFileInJarByName(
             this.pluginDescription.name,
             fileName
         )
     }
+
+    //fun getResourcesConfig()
 }
 
 class PluginDescription(

+ 83 - 32
mirai-plugins/image-sender/src/main/java/net/mamoe/mirai/imageplugin/ImageSenderMain.kt

@@ -10,19 +10,29 @@
 package net.mamoe.mirai.imageplugin
 
 import kotlinx.coroutines.*
+import net.mamoe.mirai.console.MiraiConsole
+import net.mamoe.mirai.console.command.registerCommand
 import net.mamoe.mirai.console.plugins.Config
 import net.mamoe.mirai.console.plugins.ConfigSection
 import net.mamoe.mirai.console.plugins.PluginBase
+import net.mamoe.mirai.console.plugins.withDefaultWriteSave
 import net.mamoe.mirai.contact.Contact
+import net.mamoe.mirai.contact.sendMessage
 import net.mamoe.mirai.event.events.BotOnlineEvent
+import net.mamoe.mirai.event.events.MemberPermissionChangeEvent
 import net.mamoe.mirai.event.subscribeAlways
+import net.mamoe.mirai.event.subscribeGroupMessages
 import net.mamoe.mirai.event.subscribeMessages
 import net.mamoe.mirai.message.data.Image
+import net.mamoe.mirai.message.data.sendTo
+import net.mamoe.mirai.message.upload
 import net.mamoe.mirai.message.uploadAsImage
 import net.mamoe.mirai.utils.MiraiExperimentalAPI
 import org.jsoup.Jsoup
-import java.io.File
-import java.net.URL
+import java.awt.RenderingHints
+import java.awt.image.BufferedImage
+import javax.imageio.ImageIO
+
 
 class ImageSenderMain : PluginBase() {
 
@@ -30,42 +40,60 @@ class ImageSenderMain : PluginBase() {
     lateinit var normal: List<ConfigSection>
     lateinit var r18: List<ConfigSection>
 
-    @ExperimentalCoroutinesApi
-    @MiraiExperimentalAPI
+
+    val config by lazy {
+        loadConfig("setting.yml")
+    }
+
+    val Normal_Image_Trigger by config.withDefaultWriteSave { "色图" }
+    val R18_Image_Trigger by config.withDefaultWriteSave { "不够色" }
+    val Image_Resize_Max_Width_Height by config.withDefaultWriteSave { 800 }
+
+    val groupsAllowNormal by lazy {
+        config.getLongList("Allow_Normal_Image_Groups").toMutableList()
+    }
+
+    val groupsAllowR18 by lazy {
+        config.getLongList("Allow_R18_Image_Groups").toMutableList()
+    }
+
+    override fun onDisable() {
+        config["Allow_R18_Image_Groups"] = groupsAllowR18
+        config["Allow_Normal_Image_Groups"] = groupsAllowNormal
+        config.save()
+    }
+
     override fun onEnable() {
         logger.info("Image Sender plugin enabled")
-        GlobalScope.subscribeAlways<BotOnlineEvent> {
+        registerCommands()
+        subscribeAlways<MemberPermissionChangeEvent> {
             logger.info("${this.bot.uin} login succeed, it will be controlled by Image Sender Plugin")
-            this.bot.subscribeMessages {
-                (contains("色图")) {
-                    try {
-                        with(normal.random()) {
-                            getImage(
-                                subject, this.getString("url"), this.getString("pid")
-                            ).plus(this.getString("tags")).send()
-                        }
-                    } catch (e: Exception) {
-                        reply(e.message ?: "unknown error")
-                    }
+            this.bot.subscribeGroupMessages {
+                (contains(Normal_Image_Trigger)) {
+                    sendImage(subject, normal.random())
                 }
-
-                (contains("不够色")) {
-                    try {
-                        with(r18.random()) {
-                            getImage(
-                                subject, this.getString("url"), this.getString("pid")
-                            ).plus(this.getString("tags")).send()
-                        }
-                    } catch (e: Exception) {
-                        reply(e.message ?: "unknown error")
-                    }
+                (contains(R18_Image_Trigger)) {
+                    sendImage(subject, r18.random())
                 }
             }
         }
     }
 
-    suspend fun getImage(contact: Contact, url: String, pid: String): Image {
-        return withTimeoutOrNull(20 * 1000) {
+    private fun sendImage(contact: Contact, configSection: ConfigSection) {
+        launch {
+            try {
+                logger.info("正在推送图片")
+                getImage(
+                    contact, configSection.getString("url"), configSection.getString("pid"), 800
+                ).plus(configSection.getString("tags")).sendTo(contact)
+            } catch (e: Exception) {
+                contact.sendMessage(e.message ?: "unknown error")
+            }
+        }
+    }
+
+    private suspend fun getImage(contact: Contact, url: String, pid: String, maxWidthOrHeight: Int): Image {
+        val bodyStream = withTimeoutOrNull(20 * 1000) {
             withContext(Dispatchers.IO) {
                 Jsoup
                     .connect(url)
@@ -78,9 +106,30 @@ class ImageSenderMain : PluginBase() {
                     .maxBodySize(100000000)
                     .execute().also { check(it.statusCode() == 200) { "Failed to download image" } }
             }
-        }?.bodyStream()?.uploadAsImage(contact) ?: error("Unable to download image")
+        }?.bodyStream() ?: error("Failed to download image")
+        if (maxWidthOrHeight < 1) {
+            return bodyStream.uploadAsImage(contact)
+        }
+        val image = withContext(Dispatchers.IO) {
+            ImageIO.read(bodyStream)
+        }
+        if (image.width.coerceAtLeast(image.height) <= maxWidthOrHeight) {
+            return image.upload(contact)
+        }
+        val rate = (maxWidthOrHeight.toFloat() / image.width.coerceAtLeast(image.height))
+        val newWidth = (image.width * rate).toInt()
+        val newHeight = (image.height * rate).toInt()
+        return withContext(Dispatchers.IO) {
+            val dimg = BufferedImage(newWidth, newHeight, image.type)
+            val g = dimg.createGraphics()
+            g.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR)
+            g.drawImage(image, 0, 0, newWidth, newHeight, 0, 0, image.width, image.height, null)
+            g.dispose()
+            dimg
+        }.upload(contact)
     }
 
+
     override fun onLoad() {
         logger.info("loading local image data")
 
@@ -97,8 +146,10 @@ class ImageSenderMain : PluginBase() {
         logger.info("R18    * " + r18.size)
     }
 
+    fun registerCommands() {
+        registerCommand {
 
-    override fun onDisable() {
-
+        }
     }
+
 }

+ 0 - 0
mirai-plugins/image-sender/src/main/java/net/mamoe/mirai/imageplugin/Test.kt → mirai-plugins/image-sender/src/main/resources/Test.kt