Commit f4b3dbfa authored by jiahua.liu's avatar jiahua.liu

Console Update

parent a9f9618f
import kotlinx.coroutines.runBlocking import kotlinx.coroutines.runBlocking
import net.mamoe.mirai.Bot import net.mamoe.mirai.Bot
import net.mamoe.mirai.alsoLogin import net.mamoe.mirai.plugin.Command
import net.mamoe.mirai.plugin.CommandManager
import net.mamoe.mirai.plugin.PluginManager import net.mamoe.mirai.plugin.PluginManager
import kotlin.concurrent.thread import kotlin.concurrent.thread
fun main() { val bots = mutableMapOf<Long, Bot>()
fun main() {
println("loading Mirai in console environments") println("loading Mirai in console environments")
println("正在控制台环境中启动Mirai ") println("正在控制台环境中启动Mirai ")
println() println()
...@@ -15,39 +17,56 @@ fun main() { ...@@ -15,39 +17,56 @@ fun main() {
println("Mirai-console now running on " + System.getProperty("user.dir")) println("Mirai-console now running on " + System.getProperty("user.dir"))
println("Mirai-console 正在 " + System.getProperty("user.dir") + " 运行") println("Mirai-console 正在 " + System.getProperty("user.dir") + " 运行")
println() println()
println("\"login qqnumber qqpassword \" to login a bot") println("\"/login qqnumber qqpassword \" to login a bot")
println("\"login qq号 qq密码 \" 来登陆一个BOT") println("\"/login qq号 qq密码 \" 来登陆一个BOT")
thread { processNextCommandLine() } thread { processNextCommandLine() }
PluginManager.loadPlugins() PluginManager.loadPlugins()
defaultCommands()
Runtime.getRuntime().addShutdownHook(thread(start = false) { Runtime.getRuntime().addShutdownHook(thread(start = false) {
PluginManager.disableAllPlugins() PluginManager.disableAllPlugins()
}) })
} }
tailrec fun processNextCommandLine() {
val commandArgs = readLine()?.split(" ") ?: return fun defaultCommands() {
when (commandArgs[0]) { class LoginCommand : Command(
"login" -> { "login"
if (commandArgs.size < 3) { ) {
println("\"login qqnumber qqpassword \" to login a bot") override fun onCommand(args: List<String>): Boolean {
println("\"login qq号 qq密码 \" 来登录一个BOT") if (args.size < 2) {
return processNextCommandLine() println("\"/login qqnumber qqpassword \" to login a bot")
println("\"/login qq号 qq密码 \" 来登录一个BOT")
return false
} }
val qqNumber = commandArgs[1].toLong() val qqNumber = args[0].toLong()
val qqPassword = commandArgs[2] val qqPassword = args[1]
println("login...") println("login...")
runBlocking { runBlocking {
try { try {
Bot(qqNumber, qqPassword).alsoLogin() Bot(qqNumber, qqPassword).also {
it.login()
bots[qqNumber] = it
}
} catch (e: Exception) { } catch (e: Exception) {
println("login failed") println("$qqNumber login failed")
} }
} }
return true
}
}
CommandManager.register(LoginCommand())
}
tailrec fun processNextCommandLine() {
val fullCommand = readLine()
if (fullCommand != null && fullCommand.startsWith("/")) {
if (!CommandManager.runCommand(fullCommand)) {
println("unknown command $fullCommand")
println("未知指令 $fullCommand")
} }
} }
return processNextCommandLine() processNextCommandLine();
} }
...@@ -16,6 +16,18 @@ object CommandManager { ...@@ -16,6 +16,18 @@ object CommandManager {
} }
} }
fun runCommand(fullCommand: String): Boolean {
val blocks = fullCommand.split(" ")
val commandHead = blocks[0].replace("/", "")
if (!registeredCommand.containsKey(commandHead)) {
return false
}
registeredCommand[commandHead]?.onCommand(
blocks.subList(1, blocks.size)
)
return true
}
} }
...@@ -27,7 +39,7 @@ abstract class Command( ...@@ -27,7 +39,7 @@ abstract class Command(
* 最高优先级监听器 * 最高优先级监听器
* 如果return [false] 这次指令不会被[PluginBase]的全局onCommand监听器监听 * 如果return [false] 这次指令不会被[PluginBase]的全局onCommand监听器监听
* */ * */
fun onCommand(args: List<String>): Boolean { open fun onCommand(args: List<String>): Boolean {
return true return true
} }
} }
...@@ -196,7 +196,6 @@ object PluginManager { ...@@ -196,7 +196,6 @@ object PluginManager {
PluginDescription.readFromContent(URL("jar:file:" + file.absoluteFile + "!/" + pluginYml.name).openConnection().inputStream.use { PluginDescription.readFromContent(URL("jar:file:" + file.absoluteFile + "!/" + pluginYml.name).openConnection().inputStream.use {
it.readBytes().encodeToString() it.readBytes().encodeToString()
}) })
println(description)
pluginsFound[description.name] = description pluginsFound[description.name] = description
pluginsLocation[description.name] = file pluginsLocation[description.name] = file
} }
......
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