mirror of
https://github.com/mamoe/mirai.git
synced 2025-02-13 20:02:57 +08:00
AutoLoginCommand
This commit is contained in:
parent
bf83b206e2
commit
4489da91e0
@ -22,6 +22,9 @@ import net.mamoe.mirai.console.command.descriptor.PermitteeIdValueArgumentParser
|
||||
import net.mamoe.mirai.console.command.descriptor.buildCommandArgumentContext
|
||||
import net.mamoe.mirai.console.internal.command.CommandManagerImpl
|
||||
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.Account.*
|
||||
import net.mamoe.mirai.console.internal.data.builtins.AutoLoginConfig.Account.PasswordKind.PLAIN
|
||||
import net.mamoe.mirai.console.internal.util.runIgnoreException
|
||||
import net.mamoe.mirai.console.permission.Permission
|
||||
import net.mamoe.mirai.console.permission.PermissionService
|
||||
@ -216,4 +219,83 @@ public object BuiltInCommands {
|
||||
sendMessage(PermissionService.INSTANCE.getRegisteredPermissions().joinToString("\n") { it.id.toString() })
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public object AutoLoginCommand : CompositeCommand(
|
||||
ConsoleCommandOwner, "autoLogin", "自动登录",
|
||||
description = "自动登录设置",
|
||||
), BuiltInCommandInternal {
|
||||
|
||||
@Description("添加自动登录")
|
||||
@SubCommand
|
||||
public suspend fun CommandSender.add(account: Long, password: String, passwordKind: PasswordKind = PLAIN) {
|
||||
val accountStr = account.toString()
|
||||
if (AutoLoginConfig.accounts.any { it.account == accountStr }) {
|
||||
sendMessage("已有相同账号在自动登录配置中. 请先删除该账号.")
|
||||
return
|
||||
}
|
||||
AutoLoginConfig.accounts.add(AutoLoginConfig.Account(accountStr, Password(passwordKind, password)))
|
||||
sendMessage("已成功添加 '$account'.")
|
||||
}
|
||||
|
||||
@Description("清除所有配置")
|
||||
@SubCommand
|
||||
public suspend fun CommandSender.clear() {
|
||||
AutoLoginConfig.accounts.clear()
|
||||
sendMessage("已清除所有自动登录配置.")
|
||||
}
|
||||
|
||||
@Description("删除一个账号")
|
||||
@SubCommand
|
||||
public suspend fun CommandSender.remove(account: Long) {
|
||||
val accountStr = account.toString()
|
||||
if (AutoLoginConfig.accounts.removeIf { it.account == accountStr }) {
|
||||
sendMessage("已成功删除 '$account'.")
|
||||
return
|
||||
}
|
||||
sendMessage("账号 '$account' 未配置自动登录.")
|
||||
}
|
||||
|
||||
@Description("设置一个账号的一个配置项")
|
||||
@SubCommand
|
||||
public suspend fun CommandSender.setConfig(account: Long, configKey: ConfigurationKey, value: String) {
|
||||
val accountStr = account.toString()
|
||||
|
||||
val oldAccount = AutoLoginConfig.accounts.find { it.account == accountStr } ?: kotlin.run {
|
||||
sendMessage("未找到账号 $account.")
|
||||
return
|
||||
}
|
||||
|
||||
if (value.isEmpty()) return removeConfig(account, configKey)
|
||||
|
||||
val newAccount = oldAccount.copy(configuration = oldAccount.configuration.toMutableMap().apply {
|
||||
put(configKey, value)
|
||||
})
|
||||
|
||||
AutoLoginConfig.accounts.remove(oldAccount)
|
||||
AutoLoginConfig.accounts.add(newAccount)
|
||||
|
||||
sendMessage("成功修改 '$account' 的配置 '$configKey' 为 '$value'")
|
||||
}
|
||||
|
||||
@Description("删除一个账号的一个配置项")
|
||||
@SubCommand
|
||||
public suspend fun CommandSender.removeConfig(account: Long, configKey: ConfigurationKey) {
|
||||
val accountStr = account.toString()
|
||||
|
||||
val oldAccount = AutoLoginConfig.accounts.find { it.account == accountStr } ?: kotlin.run {
|
||||
sendMessage("未找到账号 $account.")
|
||||
return
|
||||
}
|
||||
|
||||
val newAccount = oldAccount.copy(configuration = oldAccount.configuration.toMutableMap().apply {
|
||||
remove(configKey)
|
||||
})
|
||||
|
||||
AutoLoginConfig.accounts.remove(oldAccount)
|
||||
AutoLoginConfig.accounts.add(newAccount)
|
||||
|
||||
sendMessage("成功删除 '$account' 的配置 '$configKey'.")
|
||||
}
|
||||
}
|
||||
}
|
@ -7,20 +7,27 @@
|
||||
* https://github.com/mamoe/mirai/blob/master/LICENSE
|
||||
*/
|
||||
|
||||
@file:Suppress("EXPOSED_SUPER_CLASS")
|
||||
|
||||
package net.mamoe.mirai.console.internal.data.builtins
|
||||
|
||||
import kotlinx.serialization.Serializable
|
||||
import net.mamoe.mirai.console.command.CommandSender
|
||||
import net.mamoe.mirai.console.command.descriptor.CommandValueArgumentParser
|
||||
import net.mamoe.mirai.console.command.descriptor.InternalCommandValueArgumentParserExtensions
|
||||
import net.mamoe.mirai.console.data.AutoSavePluginConfig
|
||||
import net.mamoe.mirai.console.data.ValueDescription
|
||||
import net.mamoe.mirai.console.data.value
|
||||
import net.mamoe.mirai.console.util.ConsoleExperimentalApi
|
||||
import net.mamoe.yamlkt.Comment
|
||||
import net.mamoe.yamlkt.YamlDynamicSerializer
|
||||
|
||||
@ConsoleExperimentalApi
|
||||
@ValueDescription("自动登录配置")
|
||||
internal object AutoLoginConfig : AutoSavePluginConfig("AutoLogin") {
|
||||
public object AutoLoginConfig : AutoSavePluginConfig("AutoLogin") {
|
||||
|
||||
@Serializable
|
||||
data class Account(
|
||||
public data class Account(
|
||||
@Comment("账号, 现只支持 QQ 数字账号")
|
||||
val account: String,
|
||||
val password: Password,
|
||||
@ -31,7 +38,7 @@ internal object AutoLoginConfig : AutoSavePluginConfig("AutoLogin") {
|
||||
val configuration: Map<ConfigurationKey, @Serializable(with = YamlDynamicSerializer::class) Any> = mapOf(),
|
||||
) {
|
||||
@Serializable
|
||||
data class Password(
|
||||
public data class Password(
|
||||
@Comment("密码种类, 可选 PLAIN 或 MD5")
|
||||
val kind: PasswordKind,
|
||||
@Comment("密码内容, PLAIN 时为密码文本, MD5 时为 16 进制")
|
||||
@ -40,19 +47,36 @@ internal object AutoLoginConfig : AutoSavePluginConfig("AutoLogin") {
|
||||
|
||||
@Suppress("EnumEntryName")
|
||||
@Serializable
|
||||
enum class ConfigurationKey {
|
||||
public enum class ConfigurationKey {
|
||||
protocol,
|
||||
|
||||
;
|
||||
|
||||
public object Parser : CommandValueArgumentParser<ConfigurationKey>, InternalCommandValueArgumentParserExtensions<ConfigurationKey>() {
|
||||
override fun parse(raw: String, sender: CommandSender): ConfigurationKey {
|
||||
val key = values().find { it.name.equals(raw, ignoreCase = true) }
|
||||
if (key != null) return key
|
||||
illegalArgument("未知配置项, 可选值: ${values().joinToString()}")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Serializable
|
||||
enum class PasswordKind {
|
||||
public enum class PasswordKind {
|
||||
PLAIN,
|
||||
MD5
|
||||
MD5;
|
||||
|
||||
public object Parser : CommandValueArgumentParser<ConfigurationKey>, InternalCommandValueArgumentParserExtensions<ConfigurationKey>() {
|
||||
override fun parse(raw: String, sender: CommandSender): ConfigurationKey {
|
||||
val key = ConfigurationKey.values().find { it.name.equals(raw, ignoreCase = true) }
|
||||
if (key != null) return key
|
||||
illegalArgument("未知配置项, 可选值: ${ConfigurationKey.values().joinToString()}")
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
val accounts: MutableList<Account> by value(mutableListOf(
|
||||
public val accounts: MutableList<Account> by value(mutableListOf(
|
||||
Account("123456", Account.Password(Account.PasswordKind.PLAIN, "pwd"), mapOf(Account.ConfigurationKey.protocol to "ANDROID_PHONE"))
|
||||
))
|
||||
}
|
Loading…
Reference in New Issue
Block a user