plugin supporting

This commit is contained in:
jiahua.liu 2020-01-19 20:14:22 +08:00
parent c0a9db0e07
commit 4f82703703
6 changed files with 22 additions and 7 deletions

View File

@ -1,6 +1,7 @@
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.launch
import net.mamoe.mirai.Bot
import net.mamoe.mirai.alsoLogin
import net.mamoe.mirai.plugin.PluginManager
import kotlin.concurrent.thread
@ -22,7 +23,7 @@ fun main() {
PluginManager.loadPlugins()
Runtime.getRuntime().addShutdownHook(thread {
Runtime.getRuntime().addShutdownHook(Thread {
PluginManager.disableAllPlugins()
})
}
@ -40,7 +41,7 @@ tailrec fun processNextCommandLine() {
val qqPassword = commandArgs[2]
println("login...")
GlobalScope.launch {
Bot(qqNumber, qqPassword)
Bot(qqNumber, qqPassword).alsoLogin()
}
}
}

View File

@ -223,9 +223,14 @@ object PluginManager {
logger.info("loading plugin " + description.name)
try {
val pluginClass =
val pluginClass = try {
PluginClassLoader((pluginsLocation[description.name]!!), this.javaClass.classLoader)
.loadClass(description.basePath)
} catch (e: ClassNotFoundException) {
logger.info("failed to find Main: " + description.basePath + " checking if it's kotlin's path")
PluginClassLoader((pluginsLocation[description.name]!!), this.javaClass.classLoader)
.loadClass("${description.basePath}Kt")
}
return try {
val subClass = pluginClass.asSubclass(PluginBase::class.java)
val plugin: PluginBase = subClass.getDeclaredConstructor().newInstance()

View File

@ -1,3 +1,5 @@
package net.mamoe.mirai.imageplugin
import com.alibaba.fastjson.JSON
import kotlinx.coroutines.*
import net.mamoe.mirai.contact.Contact

View File

@ -1,3 +1,6 @@
package net.mamoe.mirai.imageplugin
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.GlobalScope
import net.mamoe.mirai.event.events.BotLoginSucceedEvent
import net.mamoe.mirai.event.subscribeAlways
@ -7,7 +10,7 @@ import net.mamoe.mirai.plugin.PluginBase
import net.mamoe.mirai.utils.MiraiExperimentalAPI
class ImageSenderMain : PluginBase() {
@ExperimentalCoroutinesApi
@MiraiExperimentalAPI
override fun onEnable() {
logger.info("Image Sender plugin enabled")
@ -19,9 +22,13 @@ class ImageSenderMain : PluginBase() {
(At(sender) + " ? ").reply()
}
(contains("image") || contains("")) {
(At(sender) + " ? ").reply()
(contains("image") or contains("")) {
"图片发送中".reply()
ImageProvider().apply {
this.contact = sender
}.image.await().reply()
}
}
}
}

View File

@ -1,5 +1,5 @@
name: ImageSender
main: net.mamoe.mirai.imageplugin.ImagePluginMain
main: net.mamoe.mirai.imageplugin.ImageSenderMain
version: 1.0.0
author: mamoe
info: a demo plugin of mirai