Skip to content

Commit 9fdff0f

Browse files
committed
fix
1 parent 4dccdeb commit 9fdff0f

File tree

2 files changed

+8
-2
lines changed

2 files changed

+8
-2
lines changed

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

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -112,8 +112,6 @@ class WebsocketBotClient(override var botId: Long, override var password: String
112112
}
113113

114114
override suspend fun initBot() {
115-
wsClient = httpClient.newWebSocket(wsRequest, wsListener)
116-
117115
val myDeviceInfo = File("device/bot-${botId}.json").loadAsMyDeviceInfo(json)
118116
bot = BotFactory.newBot(botId, password) {
119117
protocol = myDeviceInfo.protocol
@@ -139,6 +137,11 @@ class WebsocketBotClient(override var botId: Long, override var password: String
139137
bot.eventChannel.subscribeAlways<NewFriendRequestEvent> {
140138
bot.friendRequestLru.put(it.eventId, it)
141139
}
140+
bot.eventChannel.subscribeAlways<BotOnlineEvent> {
141+
if (wsClient == null) {
142+
wsClient = httpClient.newWebSocket(wsRequest, wsListener)
143+
}
144+
}
142145
GlobalScope.launch {
143146
bot.login()
144147
}

src/main/kotlin/net/lz1998/mirai/ext/MyDeviceInfo.kt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@ import kotlin.random.nextInt
1717
*/
1818
public fun File.loadAsMyDeviceInfo(json: Json): MyDeviceInfo {
1919
if (!this.exists() || this.length() == 0L) {
20+
if (!this.parentFile.exists()) {
21+
this.parentFile.mkdirs()
22+
}
2023
return MyDeviceInfo.random().also {
2124
this.writeText(json.encodeToString(MyDeviceInfo.serializer(), it))
2225
}

0 commit comments

Comments
 (0)