Support login account record (#414)

* AutologinConfig add `autoLogin` field to control autoLogin
Login account exist in accounts

* some tips

* solve known problems

* solve known problems

* modify as require

* AutologinConfig add `autoLogin` field to control autoLogin
Login account exist in accounts

* some tips

* solve known problems

* solve known problems

* modify as require

* revert change

* revert change

* revert change

* modify as require

* check if
This commit is contained in:
Eritque arcus 2021-11-10 06:03:54 +08:00 committed by GitHub
parent fbc7f69bb5
commit 309a712102
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 29 additions and 8 deletions

View File

@ -27,6 +27,7 @@ import net.mamoe.mirai.console.internal.command.CommandManagerImpl
import net.mamoe.mirai.console.internal.command.CommandManagerImpl.allRegisteredCommands import net.mamoe.mirai.console.internal.command.CommandManagerImpl.allRegisteredCommands
import net.mamoe.mirai.console.internal.data.builtins.AutoLoginConfig import net.mamoe.mirai.console.internal.data.builtins.AutoLoginConfig
import net.mamoe.mirai.console.internal.data.builtins.AutoLoginConfig.Account.* import net.mamoe.mirai.console.internal.data.builtins.AutoLoginConfig.Account.*
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.AutoLoginConfig.Account.PasswordKind.PLAIN
import net.mamoe.mirai.console.internal.permission.BuiltInPermissionService import net.mamoe.mirai.console.internal.permission.BuiltInPermissionService
import net.mamoe.mirai.console.internal.plugin.PluginManagerImpl import net.mamoe.mirai.console.internal.plugin.PluginManagerImpl
@ -182,15 +183,30 @@ public object BuiltInCommands {
@JvmOverloads @JvmOverloads
public suspend fun CommandSender.handle( public suspend fun CommandSender.handle(
@Name("qq") id: Long, @Name("qq") id: Long,
password: String, password: String? = null,
protocol: BotConfiguration.MiraiProtocol? = null, protocol: BotConfiguration.MiraiProtocol? = null,
) { ) {
fun BotConfiguration.setup(protocol: BotConfiguration.MiraiProtocol?): BotConfiguration {
if (protocol != null) this.protocol = protocol
return this
}
suspend fun getPassword(id: Long): Any? {
val acc = AutoLoginConfig.accounts.firstOrNull { it.account == id.toString() }
if (acc == null) {
sendMessage("Could not find '$id' in AutoLogin config. Please specify password.")
return null
}
return if (acc.password.kind == MD5) acc.password.value.toByteArray() else acc.password.value
}
val pwd: Any = password ?: getPassword(id) ?: return
kotlin.runCatching { kotlin.runCatching {
MiraiConsole.addBot(id, password) { when (pwd) {
if (protocol != null) { is String -> MiraiConsole.addBot(id, pwd) { setup(protocol) }.doLogin()
this.protocol = protocol is ByteArray -> MiraiConsole.addBot(id, pwd) { setup(protocol) }.doLogin()
} else -> throw AssertionError("Assertion failed, please report to https://github.com/mamoe/mirai-console/issues/new/choose, debug=${pwd.javaClass}")// Unreachable
}.doLogin() }
}.fold( }.fold(
onSuccess = { scopeWith(ConsoleCommandSender).sendMessage("${it.nick} ($id) Login successful") }, onSuccess = { scopeWith(ConsoleCommandSender).sendMessage("${it.nick} ($id) Login successful") },
onFailure = { throwable -> onFailure = { throwable ->

View File

@ -217,7 +217,9 @@ internal object MiraiConsoleImplementationBridge : CoroutineScope, MiraiConsoleI
phase("auto-login bots") { phase("auto-login bots") {
runBlocking { runBlocking {
val accounts = AutoLoginConfig.accounts.toList() val accounts = AutoLoginConfig.accounts.toList()
for (account in accounts) { for (account in accounts.filter {
it.configuration[ConfigurationKey.enable]?.toString()?.equals("true", true) ?: true
}) {
val id = kotlin.runCatching { val id = kotlin.runCatching {
account.account.toLong() account.account.toLong()
}.getOrElse { }.getOrElse {

View File

@ -36,6 +36,7 @@ public object AutoLoginConfig : AutoSavePluginConfig("AutoLogin") {
账号配置. 可用配置列表 (注意大小写): 账号配置. 可用配置列表 (注意大小写):
"protocol": "ANDROID_PHONE" / "ANDROID_PAD" / "ANDROID_WATCH" "protocol": "ANDROID_PHONE" / "ANDROID_PAD" / "ANDROID_WATCH"
"device": "device.json" "device": "device.json"
"enable": true
""" """
) )
val configuration: Map<ConfigurationKey, @Serializable(with = YamlDynamicSerializer::class) Any> = mapOf(), val configuration: Map<ConfigurationKey, @Serializable(with = YamlDynamicSerializer::class) Any> = mapOf(),
@ -53,6 +54,7 @@ public object AutoLoginConfig : AutoSavePluginConfig("AutoLogin") {
public enum class ConfigurationKey { public enum class ConfigurationKey {
protocol, protocol,
device, device,
enable,
; ;
@ -89,7 +91,8 @@ public object AutoLoginConfig : AutoSavePluginConfig("AutoLogin") {
password = Account.Password(Account.PasswordKind.PLAIN, "pwd"), password = Account.Password(Account.PasswordKind.PLAIN, "pwd"),
configuration = mapOf( configuration = mapOf(
Account.ConfigurationKey.protocol to "ANDROID_PHONE", Account.ConfigurationKey.protocol to "ANDROID_PHONE",
Account.ConfigurationKey.device to "device.json" Account.ConfigurationKey.device to "device.json",
Account.ConfigurationKey.enable to true
) )
) )
) )