Commit 4f827037 authored by jiahua.liu's avatar jiahua.liu

plugin supporting

parent c0a9db0e
import kotlinx.coroutines.GlobalScope import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.launch import kotlinx.coroutines.launch
import net.mamoe.mirai.Bot import net.mamoe.mirai.Bot
import net.mamoe.mirai.alsoLogin
import net.mamoe.mirai.plugin.PluginManager import net.mamoe.mirai.plugin.PluginManager
import kotlin.concurrent.thread import kotlin.concurrent.thread
...@@ -22,7 +23,7 @@ fun main() { ...@@ -22,7 +23,7 @@ fun main() {
PluginManager.loadPlugins() PluginManager.loadPlugins()
Runtime.getRuntime().addShutdownHook(thread { Runtime.getRuntime().addShutdownHook(Thread {
PluginManager.disableAllPlugins() PluginManager.disableAllPlugins()
}) })
} }
...@@ -40,7 +41,7 @@ tailrec fun processNextCommandLine() { ...@@ -40,7 +41,7 @@ tailrec fun processNextCommandLine() {
val qqPassword = commandArgs[2] val qqPassword = commandArgs[2]
println("login...") println("login...")
GlobalScope.launch { GlobalScope.launch {
Bot(qqNumber, qqPassword) Bot(qqNumber, qqPassword).alsoLogin()
} }
} }
} }
......
...@@ -223,9 +223,14 @@ object PluginManager { ...@@ -223,9 +223,14 @@ object PluginManager {
logger.info("loading plugin " + description.name) logger.info("loading plugin " + description.name)
try { try {
val pluginClass = val pluginClass = try {
PluginClassLoader((pluginsLocation[description.name]!!), this.javaClass.classLoader) PluginClassLoader((pluginsLocation[description.name]!!), this.javaClass.classLoader)
.loadClass(description.basePath) .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 { return try {
val subClass = pluginClass.asSubclass(PluginBase::class.java) val subClass = pluginClass.asSubclass(PluginBase::class.java)
val plugin: PluginBase = subClass.getDeclaredConstructor().newInstance() val plugin: PluginBase = subClass.getDeclaredConstructor().newInstance()
......
package net.mamoe.mirai.imageplugin
import com.alibaba.fastjson.JSON import com.alibaba.fastjson.JSON
import kotlinx.coroutines.* import kotlinx.coroutines.*
import net.mamoe.mirai.contact.Contact import net.mamoe.mirai.contact.Contact
......
package net.mamoe.mirai.imageplugin
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.GlobalScope import kotlinx.coroutines.GlobalScope
import net.mamoe.mirai.event.events.BotLoginSucceedEvent import net.mamoe.mirai.event.events.BotLoginSucceedEvent
import net.mamoe.mirai.event.subscribeAlways import net.mamoe.mirai.event.subscribeAlways
...@@ -7,7 +10,7 @@ import net.mamoe.mirai.plugin.PluginBase ...@@ -7,7 +10,7 @@ import net.mamoe.mirai.plugin.PluginBase
import net.mamoe.mirai.utils.MiraiExperimentalAPI import net.mamoe.mirai.utils.MiraiExperimentalAPI
class ImageSenderMain : PluginBase() { class ImageSenderMain : PluginBase() {
@ExperimentalCoroutinesApi
@MiraiExperimentalAPI @MiraiExperimentalAPI
override fun onEnable() { override fun onEnable() {
logger.info("Image Sender plugin enabled") logger.info("Image Sender plugin enabled")
...@@ -19,9 +22,13 @@ class ImageSenderMain : PluginBase() { ...@@ -19,9 +22,13 @@ class ImageSenderMain : PluginBase() {
(At(sender) + " ? ").reply() (At(sender) + " ? ").reply()
} }
(contains("image") || contains("图")) { (contains("image") or contains("图")) {
(At(sender) + " ? ").reply() "图片发送中".reply()
ImageProvider().apply {
this.contact = sender
}.image.await().reply()
} }
} }
} }
} }
......
name: ImageSender name: ImageSender
main: net.mamoe.mirai.imageplugin.ImagePluginMain main: net.mamoe.mirai.imageplugin.ImageSenderMain
version: 1.0.0 version: 1.0.0
author: mamoe author: mamoe
info: a demo plugin of mirai info: a demo plugin of mirai
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment