merge
This commit is contained in:
jiahua.liu 2020-02-25 17:32:31 +08:00
parent 014439e731
commit 5402505a8f
4 changed files with 98 additions and 36 deletions

View File

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

View File

@ -90,8 +90,15 @@ abstract class PluginBase(coroutineContext: CoroutineContext) : CoroutineScope {
fun getPluginManager() = PluginManager fun getPluginManager() = PluginManager
val logger: MiraiLogger by lazy { 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? { fun getResources(fileName: String): InputStream? {
return PluginManager.getFileInJarByName( return PluginManager.getFileInJarByName(
@ -99,6 +106,8 @@ abstract class PluginBase(coroutineContext: CoroutineContext) : CoroutineScope {
fileName fileName
) )
} }
//fun getResourcesConfig()
} }
class PluginDescription( class PluginDescription(

View File

@ -10,19 +10,29 @@
package net.mamoe.mirai.imageplugin package net.mamoe.mirai.imageplugin
import kotlinx.coroutines.* 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.Config
import net.mamoe.mirai.console.plugins.ConfigSection import net.mamoe.mirai.console.plugins.ConfigSection
import net.mamoe.mirai.console.plugins.PluginBase 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.Contact
import net.mamoe.mirai.contact.sendMessage
import net.mamoe.mirai.event.events.BotOnlineEvent 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.subscribeAlways
import net.mamoe.mirai.event.subscribeGroupMessages
import net.mamoe.mirai.event.subscribeMessages import net.mamoe.mirai.event.subscribeMessages
import net.mamoe.mirai.message.data.Image 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.message.uploadAsImage
import net.mamoe.mirai.utils.MiraiExperimentalAPI import net.mamoe.mirai.utils.MiraiExperimentalAPI
import org.jsoup.Jsoup import org.jsoup.Jsoup
import java.io.File import java.awt.RenderingHints
import java.net.URL import java.awt.image.BufferedImage
import javax.imageio.ImageIO
class ImageSenderMain : PluginBase() { class ImageSenderMain : PluginBase() {
@ -30,42 +40,60 @@ class ImageSenderMain : PluginBase() {
lateinit var normal: List<ConfigSection> lateinit var normal: List<ConfigSection>
lateinit var r18: 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() { override fun onEnable() {
logger.info("Image Sender plugin enabled") 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") logger.info("${this.bot.uin} login succeed, it will be controlled by Image Sender Plugin")
this.bot.subscribeMessages { this.bot.subscribeGroupMessages {
(contains("色图")) { (contains(Normal_Image_Trigger)) {
try { sendImage(subject, normal.random())
with(normal.random()) {
getImage(
subject, this.getString("url"), this.getString("pid")
).plus(this.getString("tags")).send()
}
} catch (e: Exception) {
reply(e.message ?: "unknown error")
}
}
(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 { private fun sendImage(contact: Contact, configSection: ConfigSection) {
return withTimeoutOrNull(20 * 1000) { 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) { withContext(Dispatchers.IO) {
Jsoup Jsoup
.connect(url) .connect(url)
@ -78,8 +106,29 @@ class ImageSenderMain : PluginBase() {
.maxBodySize(100000000) .maxBodySize(100000000)
.execute().also { check(it.statusCode() == 200) { "Failed to download image" } } .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() { override fun onLoad() {
logger.info("loading local image data") logger.info("loading local image data")
@ -97,8 +146,10 @@ class ImageSenderMain : PluginBase() {
logger.info("R18 * " + r18.size) logger.info("R18 * " + r18.size)
} }
fun registerCommands() {
override fun onDisable() { registerCommand {
} }
}
} }