From 471a919248b78969c757a3558917443a76bb0d0c Mon Sep 17 00:00:00 2001 From: Karlatemp Date: Wed, 18 Nov 2020 18:25:12 +0800 Subject: [PATCH] Fix auto login configuration --- backend/mirai-console/src/MiraiConsole.kt | 2 +- .../MiraiConsoleImplementationBridge.kt | 27 ++++++++++--------- 2 files changed, 16 insertions(+), 13 deletions(-) diff --git a/backend/mirai-console/src/MiraiConsole.kt b/backend/mirai-console/src/MiraiConsole.kt index 2211eff30..9f26892c2 100644 --- a/backend/mirai-console/src/MiraiConsole.kt +++ b/backend/mirai-console/src/MiraiConsole.kt @@ -143,7 +143,7 @@ public interface MiraiConsole : CoroutineScope { return when (password) { is ByteArray -> Bot(id, password, config) is String -> Bot(id, password, config) - else -> null!! + else -> throw IllegalArgumentException("Bad password type: `${password.javaClass.name}`. Require ByteArray or String") } } diff --git a/backend/mirai-console/src/internal/MiraiConsoleImplementationBridge.kt b/backend/mirai-console/src/internal/MiraiConsoleImplementationBridge.kt index 1e6e4c0d7..67b45b9b7 100644 --- a/backend/mirai-console/src/internal/MiraiConsoleImplementationBridge.kt +++ b/backend/mirai-console/src/internal/MiraiConsoleImplementationBridge.kt @@ -30,7 +30,6 @@ import net.mamoe.mirai.console.extensions.PostStartupExtension import net.mamoe.mirai.console.extensions.SingletonExtensionSelector import net.mamoe.mirai.console.internal.command.CommandConfig import net.mamoe.mirai.console.internal.data.builtins.AutoLoginConfig -import net.mamoe.mirai.console.internal.data.builtins.AutoLoginConfig.Account.ConfigurationKey.protocol import net.mamoe.mirai.console.internal.data.builtins.AutoLoginConfig.Account.PasswordKind.MD5 import net.mamoe.mirai.console.internal.data.builtins.AutoLoginConfig.Account.PasswordKind.PLAIN import net.mamoe.mirai.console.internal.data.builtins.ConsoleDataScope @@ -230,20 +229,24 @@ internal object MiraiConsoleImplementationBridge : CoroutineScope, MiraiConsoleI error("Bad auto-login account: '${account.account}'") } if (id == 123456L) continue - val config = BotConfiguration() - for ((key, value) in account.configuration) { - runCatching { - when (key) { - protocol -> config.protocol = BotConfiguration.MiraiProtocol.valueOf(value.toString()) + fun BotConfiguration.configBot() { + mainLogger.info { "Auto-login ${account.account}" } + + account.configuration[AutoLoginConfig.Account.ConfigurationKey.protocol] + ?.let { protocol -> + this.protocol = runCatching { + BotConfiguration.MiraiProtocol.valueOf(protocol.toString()) + }.getOrElse { + throw IllegalArgumentException( + "Bad auto-login config value for `protocol` for account $id", + it + ) + } } - }.onFailure { - error("Bad auto-login config value for $key for account $id") - } } when (account.password.kind) { PLAIN -> { - mainLogger.info { "Auto-login ${account.account}" } - MiraiConsole.addBot(id, account.password.value).alsoLogin() + MiraiConsole.addBot(id, account.password.value, BotConfiguration::configBot).alsoLogin() } MD5 -> { val md5 = kotlin.runCatching { @@ -251,7 +254,7 @@ internal object MiraiConsoleImplementationBridge : CoroutineScope, MiraiConsoleI }.getOrElse { error("Bad auto-login md5: '${account.password.value}' for account $id") } - MiraiConsole.addBot(id, md5).alsoLogin() + MiraiConsole.addBot(id, md5, BotConfiguration::configBot).alsoLogin() } } }