Configure workingDir for bots, support custom file name for deviceInfo, close #249

This commit is contained in:
Him188 2021-02-01 08:53:26 +08:00
parent 68860f7e12
commit 0970343bdb
3 changed files with 49 additions and 20 deletions

View File

@ -16,10 +16,7 @@ import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Job
import kotlinx.coroutines.runBlocking
import net.mamoe.mirai.Bot
import net.mamoe.mirai.console.MalformedMiraiConsoleImplementationError
import net.mamoe.mirai.console.MiraiConsole
import net.mamoe.mirai.console.MiraiConsoleFrontEndDescription
import net.mamoe.mirai.console.MiraiConsoleImplementation
import net.mamoe.mirai.console.*
import net.mamoe.mirai.console.command.BuiltInCommands
import net.mamoe.mirai.console.command.CommandManager
import net.mamoe.mirai.console.command.ConsoleCommandSender
@ -29,6 +26,7 @@ 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
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
@ -223,17 +221,34 @@ internal object MiraiConsoleImplementationBridge : CoroutineScope, MiraiConsoleI
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
)
}
account.configuration[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
)
}
}
workingDir = MiraiConsole.rootDir
.resolve("bots")
.resolve(id.toString())
if (!workingDir.exists()
&& workingDir.mkdirs()
&& account.configuration[ConfigurationKey.device] == null // no custom device
) {
// copy root/deviceInfo.json to bots/id/deviceInfo.json
val deviceInfoInRoot = MiraiConsole.rootDir.resolve("deviceInfo.json")
deviceInfoInRoot.copyTo(workingDir.resolve("deviceInfo.json"))
fileBasedDeviceInfo("deviceInfo.json")
}
account.configuration[ConfigurationKey.device]?.let { device ->
fileBasedDeviceInfo(device.toString())
}
}
val bot = when (account.password.kind) {

View File

@ -31,10 +31,13 @@ public object AutoLoginConfig : AutoSavePluginConfig("AutoLogin") {
@Comment("账号, 现只支持 QQ 数字账号")
val account: String,
val password: Password,
@Comment("""
@Comment(
"""
账号配置. 可用配置列表 (注意大小写):
"protocol": "ANDROID_PHONE" / "ANDROID_PAD" / "ANDROID_WATCH"
""")
"device": "deviceInfo.json"
"""
)
val configuration: Map<ConfigurationKey, @Serializable(with = YamlDynamicSerializer::class) Any> = mapOf(),
) {
@Serializable
@ -49,6 +52,7 @@ public object AutoLoginConfig : AutoSavePluginConfig("AutoLogin") {
@Serializable
public enum class ConfigurationKey {
protocol,
device,
;
@ -76,7 +80,16 @@ public object AutoLoginConfig : AutoSavePluginConfig("AutoLogin") {
}
}
public val accounts: MutableList<Account> by value(mutableListOf(
Account("123456", Account.Password(Account.PasswordKind.PLAIN, "pwd"), mapOf(Account.ConfigurationKey.protocol to "ANDROID_PHONE"))
))
public val accounts: MutableList<Account> by value(
mutableListOf(
Account(
account = "123456",
password = Account.Password(Account.PasswordKind.PLAIN, "pwd"),
configuration = mapOf(
Account.ConfigurationKey.protocol to "ANDROID_PHONE",
Account.ConfigurationKey.device to "deviceInfo.json"
)
)
)
)
}

View File

@ -22,7 +22,8 @@ fun main() {
internal fun configureUserDir() {
val projectDir = runCatching {
File(".").resolve("frontend").resolve("mirai-console-terminal")
File(".").resolve("frontend").resolve("mirai-console-terminal").takeIf { it.isDirectory }
?: File(".").resolve("mirai-console/frontend").resolve("mirai-console-terminal")
}.getOrElse { return }
if (projectDir.isDirectory) {
val run = projectDir.resolve("run")