diff --git a/mirai-console/src/main/kotlin/net/mamoe/mirai/console/plugins/ConfigSection.kt b/mirai-console/src/main/kotlin/net/mamoe/mirai/console/plugins/ConfigSection.kt index e80676438..ed52dae92 100644 --- a/mirai-console/src/main/kotlin/net/mamoe/mirai/console/plugins/ConfigSection.kt +++ b/mirai-console/src/main/kotlin/net/mamoe/mirai/console/plugins/ConfigSection.kt @@ -184,9 +184,11 @@ class WithDefaultWriteLoader( prop: KProperty<*> ): ReadWriteProperty { 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 { override fun getValue(thisRef: Any, property: KProperty<*>): T { diff --git a/mirai-console/src/main/kotlin/net/mamoe/mirai/console/plugins/PluginBase.kt b/mirai-console/src/main/kotlin/net/mamoe/mirai/console/plugins/PluginBase.kt index 4d1bd6f07..0e3770dfc 100644 --- a/mirai-console/src/main/kotlin/net/mamoe/mirai/console/plugins/PluginBase.kt +++ b/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( diff --git a/mirai-plugins/image-sender/src/main/java/net/mamoe/mirai/imageplugin/ImageSenderMain.kt b/mirai-plugins/image-sender/src/main/java/net/mamoe/mirai/imageplugin/ImageSenderMain.kt index 2e575f86d..0bbb64fca 100644 --- a/mirai-plugins/image-sender/src/main/java/net/mamoe/mirai/imageplugin/ImageSenderMain.kt +++ b/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 lateinit var r18: List - @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 { + registerCommands() + subscribeAlways { 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() { - + } } + } \ No newline at end of file diff --git a/mirai-plugins/image-sender/src/main/java/net/mamoe/mirai/imageplugin/Test.kt b/mirai-plugins/image-sender/src/main/resources/Test.kt similarity index 100% rename from mirai-plugins/image-sender/src/main/java/net/mamoe/mirai/imageplugin/Test.kt rename to mirai-plugins/image-sender/src/main/resources/Test.kt