Kill bot on returnCode <=-10000, fix #691

This commit is contained in:
Him188 2020-12-20 08:21:50 +08:00
parent 79242ed483
commit d51b268c15
4 changed files with 11 additions and 8 deletions

View File

@ -73,7 +73,8 @@ public sealed class BotOfflineEvent : BotEvent, AbstractEvent() {
* returnCode = -10008 等原因掉线
*/
@MiraiInternalApi("This is very experimental and might be changed")
public data class PacketFactory10008 internal constructor(
public data class PacketFactoryErrorCode internal constructor(
val returnCode: Int,
public override val bot: Bot,
public override val cause: Throwable
) : BotOfflineEvent(), Packet, BotPassiveEvent, CauseAware

View File

@ -103,7 +103,7 @@ internal abstract class AbstractBot<N : BotNetworkHandler> constructor(
is BotOfflineEvent.MsfOffline,
is BotOfflineEvent.Dropped,
is BotOfflineEvent.RequireReconnect,
is BotOfflineEvent.PacketFactory10008
is BotOfflineEvent.PacketFactoryErrorCode
-> {
if (!_network.isActive) {
// normally closed

View File

@ -30,7 +30,7 @@ import net.mamoe.mirai.internal.contact.*
import net.mamoe.mirai.internal.network.protocol.data.jce.StTroopNum
import net.mamoe.mirai.internal.network.protocol.data.proto.MsgSvc
import net.mamoe.mirai.internal.network.protocol.packet.*
import net.mamoe.mirai.internal.network.protocol.packet.KnownPacketFactories.PacketFactoryIllegalState10008Exception
import net.mamoe.mirai.internal.network.protocol.packet.KnownPacketFactories.PacketFactoryIllegalStateException
import net.mamoe.mirai.internal.network.protocol.packet.chat.GroupInfoImpl
import net.mamoe.mirai.internal.network.protocol.packet.chat.receive.MessageSvcPbGetMsg
import net.mamoe.mirai.internal.network.protocol.packet.list.FriendList
@ -443,9 +443,9 @@ internal class QQAndroidBotNetworkHandler(coroutineContext: CoroutineContext, bo
input.use {
try {
parsePacket(it)
} catch (e: PacketFactoryIllegalState10008Exception) {
} catch (e: PacketFactoryIllegalStateException) {
logger.warning { "Network force offline: ${e.message}" }
bot.launch { BotOfflineEvent.PacketFactory10008(bot, e).broadcast() }
bot.launch { BotOfflineEvent.PacketFactoryErrorCode(e.code, bot, e).broadcast() }
}
}
}

View File

@ -173,7 +173,8 @@ internal object KnownPacketFactories {
?: IncomingFactories.firstOrNull { it.receivingCommandName == commandName }
}
class PacketFactoryIllegalState10008Exception @JvmOverloads constructor(
class PacketFactoryIllegalStateException @JvmOverloads constructor(
val code: Int,
override val message: String? = null,
override val cause: Throwable? = null
) : RuntimeException()
@ -310,8 +311,9 @@ internal object KnownPacketFactories {
val returnCode = readInt()
check(returnCode == 0) {
if (returnCode == -10008) { // https://github.com/mamoe/mirai/issues/470
throw PacketFactoryIllegalState10008Exception("returnCode = $returnCode")
if (returnCode <= -10000) {
// https://github.com/mamoe/mirai/issues/470
throw PacketFactoryIllegalStateException(returnCode, "returnCode = $returnCode")
} else "returnCode = $returnCode"
}