No broadcast for events from friend message syncing

This commit is contained in:
Him188 2020-02-19 18:28:58 +08:00
parent 32abb01861
commit 2deb1601a8
2 changed files with 22 additions and 8 deletions

View File

@ -20,10 +20,7 @@ import kotlinx.io.core.buildPacket
import kotlinx.io.core.use
import net.mamoe.mirai.data.MultiPacket
import net.mamoe.mirai.data.Packet
import net.mamoe.mirai.event.BroadcastControllable
import net.mamoe.mirai.event.CancellableEvent
import net.mamoe.mirai.event.Event
import net.mamoe.mirai.event.broadcast
import net.mamoe.mirai.event.*
import net.mamoe.mirai.event.events.BotOfflineEvent
import net.mamoe.mirai.event.events.BotOnlineEvent
import net.mamoe.mirai.network.BotNetworkHandler
@ -296,7 +293,17 @@ internal class QQAndroidBotNetworkHandler(bot: QQAndroidBot) : BotNetworkHandler
heartbeatJob = startHeartbeatJobOrKill()
joinAll(friendListJob, groupJob)
MessageSvc.PbGetMsg(bot.client, MsgSvc.SyncFlag.START, currentTimeSeconds).sendAndExpect<MessageSvc.PbGetMsg.GetMsgSuccess>()
withTimeoutOrNull(5000) {
lateinit var listener: Listener<PacketReceivedEvent>
listener = this.subscribeAlways {
if (it.packet is MessageSvc.PbGetMsg.GetMsgSuccess) {
listener.complete()
}
}
MessageSvc.PbGetMsg(bot.client, MsgSvc.SyncFlag.START, currentTimeSeconds).sendWithoutExpect()
} ?: error("timeout syncing friend message history")
bot.firstLoginSucceed = true

View File

@ -17,6 +17,9 @@ import net.mamoe.mirai.Bot
import net.mamoe.mirai.event.internal.Handler
import net.mamoe.mirai.event.internal.subscribeInternal
import net.mamoe.mirai.utils.MiraiInternalAPI
import kotlin.contracts.ExperimentalContracts
import kotlin.contracts.InvocationKind
import kotlin.contracts.contract
import kotlin.coroutines.CoroutineContext
/*
@ -109,9 +112,13 @@ inline fun <reified E : Event> CoroutineScope.subscribe(crossinline handler: sus
*
* @see subscribe 获取更多说明
*/
@UseExperimental(MiraiInternalAPI::class)
inline fun <reified E : Event> CoroutineScope.subscribeAlways(crossinline listener: suspend E.(E) -> Unit): Listener<E> =
E::class.subscribeInternal(Handler { it.listener(it); ListeningStatus.LISTENING })
@UseExperimental(MiraiInternalAPI::class, ExperimentalContracts::class)
inline fun <reified E : Event> CoroutineScope.subscribeAlways(crossinline listener: suspend E.(E) -> Unit): Listener<E> {
contract {
callsInPlace(listener, InvocationKind.UNKNOWN)
}
return E::class.subscribeInternal(Handler { it.listener(it); ListeningStatus.LISTENING })
}
/**
* 在指定的 [CoroutineScope] 下订阅所有 [E] 及其子类事件.