mirror of
https://github.com/mamoe/mirai.git
synced 2025-01-22 22:01:00 +08:00
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:
parent
fbc7f69bb5
commit
309a712102
@ -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 ->
|
||||||
|
@ -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 {
|
||||||
|
@ -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
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
|
Loading…
Reference in New Issue
Block a user