diff --git a/src/main/kotlin/xyz/cssxsh/mirai/device/MiraiDevicePlugin.kt b/src/main/kotlin/xyz/cssxsh/mirai/device/MiraiDevicePlugin.kt index 7df769a..4ed4ba5 100644 --- a/src/main/kotlin/xyz/cssxsh/mirai/device/MiraiDevicePlugin.kt +++ b/src/main/kotlin/xyz/cssxsh/mirai/device/MiraiDevicePlugin.kt @@ -4,6 +4,7 @@ import kotlinx.serialization.* import kotlinx.serialization.json.* import net.mamoe.mirai.console.extension.* import net.mamoe.mirai.console.plugin.jvm.* +import net.mamoe.mirai.event.* public object MiraiDevicePlugin : KotlinPlugin( JvmPluginDescription( @@ -39,5 +40,6 @@ public object MiraiDevicePlugin : KotlinPlugin( generator.addr = Json.decodeFromString(readText()) } } + MiraiDeviceReset.registerTo(globalEventChannel()) } } \ No newline at end of file diff --git a/src/main/kotlin/xyz/cssxsh/mirai/device/MiraiDeviceReset.kt b/src/main/kotlin/xyz/cssxsh/mirai/device/MiraiDeviceReset.kt new file mode 100644 index 0000000..d4fd5b0 --- /dev/null +++ b/src/main/kotlin/xyz/cssxsh/mirai/device/MiraiDeviceReset.kt @@ -0,0 +1,33 @@ +package xyz.cssxsh.mirai.device + +import net.mamoe.mirai.console.events.* +import net.mamoe.mirai.event.* +import net.mamoe.mirai.network.* +import net.mamoe.mirai.utils.* + +/** + * 自动重载 Device Info + */ +public object MiraiDeviceReset : SimpleListenerHost() { + + @EventHandler + public fun ConsoleEvent.handle() { + try { + val failure = this as? AutoLoginEvent.Failure ?: return + val exception = failure.cause.suppressedExceptions + .firstNotNullOfOrNull { it as? WrongPasswordException } ?: return + val message = exception.message ?: return + if ("""code=(45|235)""".toRegex() !in message) return + val success = with(failure.bot.configuration.workingDir) { + val file = resolve("device.json") + val backup = resolve("device.${System.currentTimeMillis()}.json") + file.renameTo(backup) + } + if (success) { + bot.logger.info { "device.json 可能已经被拉黑, 将重新生成,请稍后重新尝试手动重新登陆" } + } + } catch (_: ClassNotFoundException) { + // ... + } + } +} \ No newline at end of file