Skip to content

Commit 67423ba

Browse files
committed
update
1 parent a372954 commit 67423ba

File tree

1 file changed

+32
-4
lines changed

1 file changed

+32
-4
lines changed

src/main/kotlin/net/lz1998/mirai/controller/BotController.kt

Lines changed: 32 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package net.lz1998.mirai.controller
22

3+
import kotlinx.coroutines.GlobalScope
4+
import kotlinx.coroutines.launch
35
import kotlinx.coroutines.runBlocking
46
import net.lz1998.mirai.service.BotService
57
import net.lz1998.mirai.service.LoginDataType
@@ -16,16 +18,42 @@ class BotController {
1618
lateinit var botService: BotService
1719

1820

21+
// 创建一个机器人并登陆
1922
@RequestMapping("/createBot")
2023
fun createBot(botId: Long, password: String): String {
21-
println(botId)
22-
println(password)
23-
runBlocking { // TODO 是否可以优化? suspend报错怎么解决?
24-
botService.createBot(botId, password)
24+
GlobalScope.launch { // TODO 是否可以优化? suspend报错怎么解决?
25+
val bot = botService.botMap[botId]
26+
if (bot != null) { // 机器人已存在,直接登陆
27+
bot.bot.login()
28+
return@launch
29+
} else { // 机器人不存在,创建
30+
botService.createBot(botId, password)
31+
}
2532
}
2633
return "ok"
2734
}
2835

36+
// 获取机器人状态
37+
@RequestMapping("/getStatus")
38+
fun getStatus(botId: Long): String {
39+
// 机器人不存在
40+
val bot = botService.botMap[botId] ?: return "NOT_CREATED"
41+
42+
// 机器人在线
43+
if (bot.bot.isOnline) {
44+
return "ONLINE"
45+
}
46+
47+
// 机器人需要登陆
48+
val loginData = myLoginSolver.getLoginData(botId)
49+
if (loginData != null) {
50+
return loginData.type.name // 登陆类型
51+
}
52+
53+
// 其他状态
54+
return "UNKNOWN"
55+
}
56+
2957
// 通过轮询获取登陆验证url
3058
@RequestMapping("/getLoginUrl")
3159
fun getLoginUrl(botId: Long): String? {

0 commit comments

Comments
 (0)