Fix reconnect. Fix #894

This commit is contained in:
Him188 2021-01-23 23:29:09 +08:00
parent 2e5b223b6a
commit 65eff19f32

View File

@ -96,16 +96,14 @@ internal abstract class AbstractBot<N : BotNetworkHandler> constructor(
priority = MONITOR, priority = MONITOR,
concurrency = ConcurrencyKind.LOCKED concurrency = ConcurrencyKind.LOCKED
) { event -> ) { event ->
if (!event.bot.isActive) { if (
// bot closed !event.bot.isActive // bot closed
return@subscribeAlways || !::_network.isInitialized // bot 还未登录就被 close
} || _isConnecting // bot 还在登入
if (!::_network.isInitialized) { ) {
// bot 还未登录就被 close // Close network to avoid endless reconnection while network is ok
return@subscribeAlways // https://github.com/mamoe/mirai/issues/894
} kotlin.runCatching { network.close(event.castOrNull<BotOfflineEvent.CauseAware>()?.cause) }
if (_isConnecting) {
// bot 还在登入
return@subscribeAlways return@subscribeAlways
} }
/* /*
@ -167,13 +165,20 @@ internal abstract class AbstractBot<N : BotNetworkHandler> constructor(
if (tryCount != 0) { if (tryCount != 0) {
delay(configuration.reconnectPeriodMillis) delay(configuration.reconnectPeriodMillis)
} }
network.withConnectionLock {
/**
* [AbstractBot.relogin] only, no [BotNetworkHandler.init] // Close network to avoid endless reconnection while network is ok
*/ // https://github.com/mamoe/mirai/issues/894
@OptIn(ThisApiMustBeUsedInWithConnectionLockBlock::class) kotlin.runCatching { network.close(event.castOrNull<BotOfflineEvent.CauseAware>()?.cause) }
relogin((event as? BotOfflineEvent.Dropped)?.cause)
} login()
// network.withConnectionLock {
// /**
// * [AbstractBot.relogin] only, no [BotNetworkHandler.init]
// */
// @OptIn(ThisApiMustBeUsedInWithConnectionLockBlock::class)
// relogin((event as? BotOfflineEvent.Dropped)?.cause)
// }
launch { launch {
BotReloginEvent(bot, (event as? BotOfflineEvent.CauseAware)?.cause).broadcast() BotReloginEvent(bot, (event as? BotOfflineEvent.CauseAware)?.cause).broadcast()
} }