|
1 | 1 | package net.lz1998.mirai.entity
|
2 | 2 |
|
3 | 3 | import kotlinx.coroutines.*
|
| 4 | +import kotlinx.serialization.json.Json |
4 | 5 | import net.lz1998.mirai.alias.BFrame
|
5 | 6 | import net.lz1998.mirai.alias.BFrameType
|
6 | 7 | import net.lz1998.mirai.ext.*
|
7 | 8 | import net.lz1998.mirai.service.MyLoginSolver
|
8 | 9 | import net.lz1998.mirai.utils.*
|
9 | 10 | import net.mamoe.mirai.Bot
|
10 |
| -import net.mamoe.mirai.alsoLogin |
11 |
| -import net.mamoe.mirai.event.events.BotEvent |
12 |
| -import net.mamoe.mirai.event.events.BotInvitedJoinGroupRequestEvent |
13 |
| -import net.mamoe.mirai.event.events.MemberJoinRequestEvent |
14 |
| -import net.mamoe.mirai.event.events.NewFriendRequestEvent |
15 |
| -import net.mamoe.mirai.event.subscribeAlways |
16 |
| -import net.mamoe.mirai.message.MessageEvent |
| 11 | +import net.mamoe.mirai.BotFactory |
| 12 | +import net.mamoe.mirai.event.events.* |
17 | 13 | import okhttp3.*
|
18 |
| -import okhttp3.internal.ws.WebSocketProtocol |
19 | 14 | import okio.ByteString
|
20 | 15 | import okio.ByteString.Companion.toByteString
|
21 |
| -import java.lang.Thread.sleep |
| 16 | +import java.io.File |
22 | 17 | import java.util.concurrent.TimeUnit
|
23 | 18 |
|
| 19 | +var json: Json = runCatching { |
| 20 | + Json { |
| 21 | + isLenient = true |
| 22 | + ignoreUnknownKeys = true |
| 23 | + prettyPrint = true |
| 24 | + } |
| 25 | +}.getOrElse { Json {} } |
| 26 | + |
24 | 27 | class WebsocketBotClient(override var botId: Long, override var password: String, wsUrl: String) : RemoteBot {
|
25 | 28 | override lateinit var bot: Bot
|
26 | 29 |
|
27 | 30 |
|
28 | 31 | private var lastWsConnectTime: Long = 0
|
29 | 32 | var connecting: Boolean = false
|
30 | 33 |
|
31 |
| - private var wsClient: WebSocket? = null |
| 34 | + var wsClient: WebSocket? = null |
32 | 35 | private var httpClient: OkHttpClient = OkHttpClient.Builder()
|
33 | 36 | .callTimeout(20, TimeUnit.SECONDS)
|
34 | 37 | .connectTimeout(20, TimeUnit.SECONDS)
|
@@ -109,28 +112,39 @@ class WebsocketBotClient(override var botId: Long, override var password: String
|
109 | 112 | }
|
110 | 113 |
|
111 | 114 | override suspend fun initBot() {
|
112 |
| - wsClient = httpClient.newWebSocket(wsRequest, wsListener) |
113 |
| - bot = Bot(botId, password) { |
114 |
| - fileStrBasedDeviceInfo("device/${botId}.json") |
| 115 | + val myDeviceInfo = File("device/bot-${botId}.json").loadAsMyDeviceInfo(json) |
| 116 | + bot = BotFactory.newBot(botId, password) { |
| 117 | + protocol = myDeviceInfo.protocol |
| 118 | + deviceInfo = { myDeviceInfo.generateDeviceInfoData() } |
115 | 119 | loginSolver = MyLoginSolver
|
116 |
| -// noNetworkLog() |
117 |
| - }.alsoLogin() |
118 |
| - bot.subscribeAlways<BotEvent> { |
| 120 | + } |
| 121 | + bot.logger.info("DeviceInfo: ${json.encodeToString(MyDeviceInfo.serializer(), myDeviceInfo)}") |
| 122 | + |
| 123 | + bot.eventChannel.subscribeAlways<BotEvent> { |
119 | 124 | onBotEvent(this)
|
120 | 125 | }
|
121 |
| - bot.subscribeAlways<MessageEvent> { |
| 126 | + bot.eventChannel.subscribeAlways<net.mamoe.mirai.event.events.MessageEvent> { |
122 | 127 | val messageSource = this.source // 撤回消息用
|
123 |
| - bot.messageSourceLru.put(messageSource.id, messageSource) |
| 128 | + val messageId = if (messageSource.ids.isNotEmpty()) messageSource.ids[0] else 0 |
| 129 | + bot.messageSourceLru.put(messageId, messageSource) |
124 | 130 | }
|
125 |
| - bot.subscribeAlways<MemberJoinRequestEvent> { |
| 131 | + bot.eventChannel.subscribeAlways<MemberJoinRequestEvent> { |
126 | 132 | bot.groupRequestLru.put(it.eventId, it)
|
127 | 133 | }
|
128 |
| - bot.subscribeAlways<BotInvitedJoinGroupRequestEvent> { |
| 134 | + bot.eventChannel.subscribeAlways<BotInvitedJoinGroupRequestEvent> { |
129 | 135 | bot.botInvitedGroupRequestLru.put(it.eventId, it)
|
130 | 136 | }
|
131 |
| - bot.subscribeAlways<NewFriendRequestEvent> { |
| 137 | + bot.eventChannel.subscribeAlways<NewFriendRequestEvent> { |
132 | 138 | bot.friendRequestLru.put(it.eventId, it)
|
133 | 139 | }
|
| 140 | + bot.eventChannel.subscribeAlways<BotOnlineEvent> { |
| 141 | + if (wsClient == null) { |
| 142 | + wsClient = httpClient.newWebSocket(wsRequest, wsListener) |
| 143 | + } |
| 144 | + } |
| 145 | + GlobalScope.launch { |
| 146 | + bot.login() |
| 147 | + } |
134 | 148 | }
|
135 | 149 |
|
136 | 150 | override suspend fun login() {
|
|
0 commit comments