Skip to content

Commit d4e3fb1

Browse files
committed
update
1 parent 69cfdfc commit d4e3fb1

File tree

5 files changed

+13
-18
lines changed

5 files changed

+13
-18
lines changed

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

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,11 @@ class BotController {
1515
@Autowired
1616
lateinit var botService: BotService
1717

18-
@Autowired
19-
lateinit var myLoginSolver: MyLoginSolver
20-
2118

2219
// 创建一个机器人并登陆
2320
@RequestMapping("/create/v1", produces = ["application/x-protobuf"], consumes = ["application/x-protobuf"])
2421
fun createBot(@RequestBody param: HttpDto.CreateBotReq): HttpDto.CreateBotResp {
25-
botService.createBot(param.botId, param.password)
22+
GlobalScope.launch { botService.createBot(param.botId, param.password) }
2623
return HttpDto.CreateBotResp.newBuilder().build()
2724
}
2825

src/main/kotlin/net/lz1998/mirai/entity/RemoteBot.kt

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,15 @@
11
package net.lz1998.mirai.entity
22

3-
import net.lz1998.mirai.ext.messageSourceLru
4-
import net.lz1998.mirai.service.myLoginSolver
53
import net.mamoe.mirai.Bot
6-
import net.mamoe.mirai.alsoLogin
74
import net.mamoe.mirai.event.events.BotEvent
8-
import net.mamoe.mirai.event.subscribeAlways
9-
import net.mamoe.mirai.message.MessageEvent
105
import onebot.OnebotFrame
116

127
interface RemoteBot {
138
var bot: Bot
149
var botId: Long
1510
var password: String
1611

17-
fun initBot()
12+
suspend fun initBot()
1813

1914
suspend fun login()
2015

src/main/kotlin/net/lz1998/mirai/entity/WebSocketBotClient.kt

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,11 @@ import kotlinx.coroutines.GlobalScope
55
import kotlinx.coroutines.launch
66
import kotlinx.coroutines.withContext
77
import net.lz1998.mirai.ext.messageSourceLru
8-
import net.lz1998.mirai.service.myLoginSolver
8+
import net.lz1998.mirai.service.MyLoginSolver
99
import net.lz1998.mirai.utils.*
1010
import net.lz1998.mirai.utils.toFrame
1111
import net.mamoe.mirai.Bot
12+
import net.mamoe.mirai.alsoLogin
1213
import net.mamoe.mirai.event.events.BotEvent
1314
import net.mamoe.mirai.event.subscribeAlways
1415
import net.mamoe.mirai.message.MessageEvent
@@ -88,13 +89,13 @@ class WebsocketBotClient(override var botId: Long, override var password: String
8889
}
8990

9091

91-
override fun initBot() {
92+
override suspend fun initBot() {
9293
wsClient = httpClient.newWebSocket(wsRequest, wsListener)
9394
bot = Bot(botId, password) {
9495
fileBasedDeviceInfo("device.json")
95-
loginSolver = myLoginSolver
96+
loginSolver = MyLoginSolver
9697
noNetworkLog()
97-
}
98+
}.alsoLogin()
9899
bot.subscribeAlways<BotEvent> {
99100
onBotEvent(this)
100101
}

src/main/kotlin/net/lz1998/mirai/service/BotService.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,12 @@ class BotService {
1010
val botMap = mutableMapOf<Long, RemoteBot>()
1111

1212
@Synchronized
13-
fun createBot(botId: Long, password: String) {
13+
suspend fun createBot(botId: Long, password: String) {
1414
var bot = botMap[botId]
1515
if (bot == null) {
1616
bot = WebsocketBotClient(botId, password, "ws://127.0.0.1:8081/ws/cq/")
17-
bot.initBot()
1817
botMap[botId] = bot
18+
bot.initBot()
1919
}
2020
}
2121

src/main/kotlin/net/lz1998/mirai/service/MyLoginSolver.kt

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import okio.ByteString.Companion.toByteString
99
import org.springframework.stereotype.Service
1010

1111
@Service
12-
class MyLoginSolver : LoginSolver() {
12+
object MyLoginSolver : LoginSolver() {
1313
// TODO 通过轮询查询 loginMap
1414
val loginMap = mutableMapOf<Long, LoginData>()
1515

@@ -34,12 +34,14 @@ class MyLoginSolver : LoginSolver() {
3434
val def = CompletableDeferred<String>()
3535
val loginData = LoginData(bot.id, LoginDataType.UNSAFE_DEVICE_LOGIN_VERIFY, def, null, url)
3636
loginMap[bot.id] = loginData
37+
println(loginMap.toString())
3738
return def.await().trim()
3839
}
3940

4041
fun solveLogin(botId: Long, result: String) {
4142
val loginData = loginMap[botId] ?: return
4243
loginData.def.complete(result)
44+
loginMap.remove(botId)
4345
}
4446

4547
fun getCaptchaList(): Collection<HttpDto.Captcha> {
@@ -79,4 +81,4 @@ enum class LoginDataType(val type: String) {
7981
UNSAFE_DEVICE_LOGIN_VERIFY("unsafe_device_login_verify"),
8082
}
8183

82-
val myLoginSolver = MyLoginSolver()
84+
//val myLoginSolver = MyLoginSolver()

0 commit comments

Comments
 (0)