1
1
package net.lz1998.mirai.controller
2
2
3
+ import kotlinx.coroutines.GlobalScope
4
+ import kotlinx.coroutines.launch
3
5
import kotlinx.coroutines.runBlocking
4
6
import net.lz1998.mirai.service.BotService
5
7
import net.lz1998.mirai.service.LoginDataType
@@ -16,16 +18,42 @@ class BotController {
16
18
lateinit var botService: BotService
17
19
18
20
21
+ // 创建一个机器人并登陆
19
22
@RequestMapping(" /createBot" )
20
23
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
+ }
25
32
}
26
33
return " ok"
27
34
}
28
35
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
+
29
57
// 通过轮询获取登陆验证url
30
58
@RequestMapping(" /getLoginUrl" )
31
59
fun getLoginUrl (botId : Long ): String? {
0 commit comments