mirai/mirai-console/src/main/kotlin/MiraiConsole.kt

73 lines
2.3 KiB
Kotlin
Raw Normal View History

2020-01-19 20:41:51 +08:00
import kotlinx.coroutines.runBlocking
2020-01-18 18:44:03 +08:00
import net.mamoe.mirai.Bot
2020-01-25 17:23:48 +08:00
import net.mamoe.mirai.plugin.Command
import net.mamoe.mirai.plugin.CommandManager
2020-01-18 21:01:14 +08:00
import net.mamoe.mirai.plugin.PluginManager
2020-01-18 18:44:03 +08:00
import kotlin.concurrent.thread
2020-01-25 17:23:48 +08:00
val bots = mutableMapOf<Long, Bot>()
2020-01-18 18:44:03 +08:00
2020-01-25 17:23:48 +08:00
fun main() {
2020-01-18 18:44:03 +08:00
println("loading Mirai in console environments")
println("正在控制台环境中启动Mirai ")
println()
println("Mirai-console is still in testing stage, some feature is not available")
println("Mirai-console 还处于测试阶段, 部分功能不可用")
println()
println("Mirai-console now running on " + System.getProperty("user.dir"))
println("Mirai-console 正在 " + System.getProperty("user.dir") + " 运行")
println()
2020-01-25 17:23:48 +08:00
println("\"/login qqnumber qqpassword \" to login a bot")
println("\"/login qq号 qq密码 \" 来登陆一个BOT")
2020-01-18 18:44:03 +08:00
2020-01-18 21:17:47 +08:00
thread { processNextCommandLine() }
2020-01-18 20:06:33 +08:00
2020-01-18 21:17:47 +08:00
PluginManager.loadPlugins()
2020-01-25 17:23:48 +08:00
defaultCommands()
2020-01-19 00:02:47 +08:00
2020-01-19 21:15:25 +08:00
Runtime.getRuntime().addShutdownHook(thread(start = false) {
2020-01-19 00:02:47 +08:00
PluginManager.disableAllPlugins()
})
2020-01-18 21:17:47 +08:00
}
2020-01-25 17:23:48 +08:00
fun defaultCommands() {
class LoginCommand : Command(
"login"
) {
override fun onCommand(args: List<String>): Boolean {
if (args.size < 2) {
println("\"/login qqnumber qqpassword \" to login a bot")
println("\"/login qq号 qq密码 \" 来登录一个BOT")
return false
2020-01-18 21:17:47 +08:00
}
2020-01-25 17:23:48 +08:00
val qqNumber = args[0].toLong()
val qqPassword = args[1]
2020-01-18 21:17:47 +08:00
println("login...")
2020-01-19 20:41:51 +08:00
runBlocking {
try {
2020-01-25 17:23:48 +08:00
Bot(qqNumber, qqPassword).also {
it.login()
bots[qqNumber] = it
}
2020-01-19 20:41:51 +08:00
} catch (e: Exception) {
2020-01-25 17:23:48 +08:00
println("$qqNumber login failed")
2020-01-19 20:41:51 +08:00
}
2020-01-18 18:44:03 +08:00
}
2020-01-25 17:23:48 +08:00
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")
2020-01-18 18:44:03 +08:00
}
}
2020-01-25 17:23:48 +08:00
processNextCommandLine();
2020-01-18 18:44:03 +08:00
}