Commit 34f10fe2 authored by Him188's avatar Him188

adjust login result

parent e2171fd1
package net.mamoe.mirai.network.protocol.tim.packet.login
import net.mamoe.mirai.network.protocol.tim.packet.login.LoginResult.SUCCESS
/**
* 登录结果. 除 [SUCCESS] 外均为失败.
* @see LoginResult.requireSuccess 要求成功
......@@ -60,8 +62,29 @@ fun LoginResult.requireSuccess(lazyMessage: (LoginResult) -> String) {
/**
* 如果 [this] 不为 [LoginResult.SUCCESS] 就抛出消息为 "Login failed $this" 的 [IllegalStateException]
* 检查 [this] 为 [LoginResult.SUCCESS].
* 失败则 [error]
*/
fun LoginResult.requireSuccess() {
if (this != LoginResult.SUCCESS) error("Login failed: $this")
}
\ No newline at end of file
if (requireSuccessOrNull() === null)
error("Login failed: $this")
}
/**
* 检查 [this] 为 [LoginResult.SUCCESS].
* 失败则返回 `null`
*
* @return 成功时 [Unit], 失败时 `null`
*/
fun LoginResult.requireSuccessOrNull(): Unit? =
if (this != LoginResult.SUCCESS) Unit else null
/**
* 检查 [this] 为 [LoginResult.SUCCESS].
* 失败则返回 `null`
*
* @return 成功时 [Unit], 失败时 `null`
*/
inline fun <R> LoginResult.ifFail(block: (LoginResult) -> R): R? =
if (this != LoginResult.SUCCESS) block(this) else null
package net.mamoe.mirai.utils
import io.ktor.util.cio.writeChannel
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.sync.Mutex
import kotlinx.coroutines.sync.withLock
import kotlinx.coroutines.withContext
import kotlinx.io.core.IoBuffer
import kotlinx.io.core.readBytes
import java.awt.Image
import java.awt.image.BufferedImage
import java.io.File
......@@ -20,19 +20,23 @@ import kotlin.math.min
* @return 用户输入得到的验证码
*/
internal actual suspend fun solveCaptcha(captchaBuffer: IoBuffer): String? = captchaLock.withLock {
val captcha = captchaBuffer.readBytes()
val tempFile = File(System.getProperty("user.dir") + "/temp/Captcha.png").also {
withContext(Dispatchers.IO) {
it.createNewFile(); @Suppress("EXPERIMENTAL_API_USAGE")
it.writeChannel().writeFully(captchaBuffer)
}
}
withContext(Dispatchers.IO) {
MiraiLogger.verbose(ImageIO.read(captcha.inputStream()).createCharImg())
MiraiLogger.info(ImageIO.read(tempFile.inputStream()).createCharImg())
}
MiraiLogger.verbose("需要验证码登录, 验证码为 4 字母")
MiraiLogger.info("需要验证码登录, 验证码为 4 字母")
try {
File(System.getProperty("user.dir") + "/temp/Captcha.png")
.let { withContext(Dispatchers.IO) { it.createNewFile(); it.writeBytes(captcha) } }
MiraiLogger.verbose("若看不清字符图片, 请查看 Mirai 目录下 /temp/Captcha.png")
MiraiLogger.info("若看不清字符图片, 请查看 Mirai 目录下 /temp/Captcha.png")
} catch (e: Exception) {
MiraiLogger.verbose("无法写出验证码文件(${e.message}), 请尝试查看以上字符图片")
MiraiLogger.info("无法写出验证码文件(${e.message}), 请尝试查看以上字符图片")
}
MiraiLogger.verbose("若要更换验证码, 请直接回车")
MiraiLogger.info("若要更换验证码, 请直接回车")
readLine()?.takeUnless { it.isEmpty() || it.length != 4 }
}
......
......@@ -13,7 +13,7 @@ import net.mamoe.mirai.login
import net.mamoe.mirai.message.*
import net.mamoe.mirai.network.protocol.tim.packet.OutgoingRawPacket
import net.mamoe.mirai.network.protocol.tim.packet.action.uploadImage
import net.mamoe.mirai.network.protocol.tim.packet.login.requireSuccess
import net.mamoe.mirai.network.protocol.tim.packet.login.ifFail
import net.mamoe.mirai.network.session
import net.mamoe.mirai.qqAccount
import net.mamoe.mirai.utils.io.hexToBytes
......@@ -21,6 +21,7 @@ import net.mamoe.mirai.utils.io.toByteArray
import net.mamoe.mirai.utils.io.toUHexString
import net.mamoe.mirai.utils.toExternalImage
import java.io.File
import kotlin.system.exitProcess
private fun readTestAccount(): BotAccount? {
val file = File("testAccount.txt")
......@@ -48,7 +49,10 @@ suspend fun main() {
// 覆盖默认的配置
bot.login {
randomDeviceName = false
}.requireSuccess()
}.ifFail {
bot.logger.error("Login failed: $it")
exitProcess(1)
}
bot.messageDSL()
directlySubscribe(bot)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment