Skip to content

Commit

Permalink
feat: device info reset
Browse files Browse the repository at this point in the history
  • Loading branch information
cssxsh committed Apr 23, 2023
1 parent 995791b commit a59be93
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/main/kotlin/xyz/cssxsh/mirai/device/MiraiDevicePlugin.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -39,5 +40,6 @@ public object MiraiDevicePlugin : KotlinPlugin(
generator.addr = Json.decodeFromString(readText())
}
}
MiraiDeviceReset.registerTo(globalEventChannel())
}
}
33 changes: 33 additions & 0 deletions src/main/kotlin/xyz/cssxsh/mirai/device/MiraiDeviceReset.kt
Original file line number Diff line number Diff line change
@@ -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) {
// ...
}
}
}

0 comments on commit a59be93

Please sign in to comment.