Support select protocol in /login; Close bot when bot login failed

This commit is contained in:
Karlatemp 2021-02-01 10:09:58 +08:00
parent 3f98d8ec2a
commit 8683e88892
No known key found for this signature in database
GPG Key ID: 21FBDDF664FF06F8
2 changed files with 28 additions and 12 deletions

View File

@ -1,8 +1,8 @@
/*
* Copyright 2019-2020 Mamoe Technologies and contributors.
* Copyright 2019-2021 Mamoe Technologies and contributors.
*
* 此源代码的使用受 GNU AFFERO GENERAL PUBLIC LICENSE version 3 许可证的约束, 可以在以下链接找到该许可证.
* Use of this source code is governed by the GNU AFFERO GENERAL PUBLIC LICENSE version 3 license that can be found through the following link.
* Use of this source code is governed by the GNU AGPLv3 license that can be found through the following link.
*
* https://github.com/mamoe/mirai/blob/master/LICENSE
*/
@ -12,7 +12,7 @@ package net.mamoe.mirai.console.command
import kotlinx.coroutines.*
import kotlinx.coroutines.sync.Mutex
import kotlinx.coroutines.sync.withLock
import net.mamoe.mirai.alsoLogin
import net.mamoe.mirai.Bot
import net.mamoe.mirai.console.MiraiConsole
import net.mamoe.mirai.console.command.CommandManager.INSTANCE.register
import net.mamoe.mirai.console.command.descriptor.CommandArgumentParserException
@ -47,6 +47,7 @@ import net.mamoe.mirai.console.util.ConsoleInternalApi
import net.mamoe.mirai.console.util.sendAnsiMessage
import net.mamoe.mirai.event.events.EventCancelledException
import net.mamoe.mirai.message.nextMessageOrNull
import net.mamoe.mirai.utils.BotConfiguration
import net.mamoe.mirai.utils.secondsToMillis
import java.lang.management.ManagementFactory
import java.lang.management.MemoryUsage
@ -154,10 +155,23 @@ public object BuiltInCommands {
ConsoleCommandOwner, "login", "登录",
description = "登录一个账号",
), BuiltInCommandInternal {
private suspend fun Bot.doLogin() = kotlin.runCatching {
login(); this
}.onFailure { close() }.getOrThrow()
@Handler
public suspend fun CommandSender.handle(@Name("qq") id: Long, password: String) {
@JvmOverloads
public suspend fun CommandSender.handle(
@Name("qq") id: Long,
password: String,
protocol: BotConfiguration.MiraiProtocol? = null,
) {
kotlin.runCatching {
MiraiConsole.addBot(id, password).alsoLogin()
MiraiConsole.addBot(id, password) {
if (protocol != null) {
this.protocol = protocol
}
}.doLogin()
}.fold(
onSuccess = { sendMessage("${it.nick} ($id) Login successful") },
onFailure = { throwable ->
@ -363,6 +377,7 @@ public object BuiltInCommands {
val nonHeapMemoryUsage: MUsage
val objectPendingFinalizationCount: Int
}
internal val memoryUsageGet: MemoryUsageGet = kotlin.runCatching {
ByMemoryMXBean
}.getOrElse { ByRuntime }

View File

@ -1,8 +1,8 @@
/*
* Copyright 2019-2020 Mamoe Technologies and contributors.
* Copyright 2019-2021 Mamoe Technologies and contributors.
*
* 此源代码的使用受 GNU AFFERO GENERAL PUBLIC LICENSE version 3 许可证的约束, 可以在以下链接找到该许可证.
* Use of this source code is governed by the GNU AFFERO GENERAL PUBLIC LICENSE version 3 license that can be found through the following link.
* Use of this source code is governed by the GNU AGPLv3 license that can be found through the following link.
*
* https://github.com/mamoe/mirai/blob/master/LICENSE
*/
@ -267,6 +267,7 @@ internal object MiraiConsoleImplementationBridge : CoroutineScope, MiraiConsoleI
runCatching { bot.login() }.getOrElse {
mainLogger.error(it)
bot.close()
}
}