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,10 +1,10 @@
/* /*
* Copyright 2019-2020 Mamoe Technologies and contributors. * Copyright 2019-2021 Mamoe Technologies and contributors.
* *
* 此源代码的使用受 GNU AFFERO GENERAL PUBLIC LICENSE version 3 许可证的约束, 可以在以下链接找到该许可证. * 此源代码的使用受 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 * https://github.com/mamoe/mirai/blob/master/LICENSE
*/ */
package net.mamoe.mirai.console.command package net.mamoe.mirai.console.command
@ -12,7 +12,7 @@ package net.mamoe.mirai.console.command
import kotlinx.coroutines.* import kotlinx.coroutines.*
import kotlinx.coroutines.sync.Mutex import kotlinx.coroutines.sync.Mutex
import kotlinx.coroutines.sync.withLock 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.MiraiConsole
import net.mamoe.mirai.console.command.CommandManager.INSTANCE.register import net.mamoe.mirai.console.command.CommandManager.INSTANCE.register
import net.mamoe.mirai.console.command.descriptor.CommandArgumentParserException 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.console.util.sendAnsiMessage
import net.mamoe.mirai.event.events.EventCancelledException import net.mamoe.mirai.event.events.EventCancelledException
import net.mamoe.mirai.message.nextMessageOrNull import net.mamoe.mirai.message.nextMessageOrNull
import net.mamoe.mirai.utils.BotConfiguration
import net.mamoe.mirai.utils.secondsToMillis import net.mamoe.mirai.utils.secondsToMillis
import java.lang.management.ManagementFactory import java.lang.management.ManagementFactory
import java.lang.management.MemoryUsage import java.lang.management.MemoryUsage
@ -154,10 +155,23 @@ public object BuiltInCommands {
ConsoleCommandOwner, "login", "登录", ConsoleCommandOwner, "login", "登录",
description = "登录一个账号", description = "登录一个账号",
), BuiltInCommandInternal { ), BuiltInCommandInternal {
private suspend fun Bot.doLogin() = kotlin.runCatching {
login(); this
}.onFailure { close() }.getOrThrow()
@Handler @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 { kotlin.runCatching {
MiraiConsole.addBot(id, password).alsoLogin() MiraiConsole.addBot(id, password) {
if (protocol != null) {
this.protocol = protocol
}
}.doLogin()
}.fold( }.fold(
onSuccess = { sendMessage("${it.nick} ($id) Login successful") }, onSuccess = { sendMessage("${it.nick} ($id) Login successful") },
onFailure = { throwable -> onFailure = { throwable ->
@ -361,8 +375,9 @@ public object BuiltInCommands {
internal interface MemoryUsageGet { internal interface MemoryUsageGet {
val heapMemoryUsage: MUsage val heapMemoryUsage: MUsage
val nonHeapMemoryUsage: MUsage val nonHeapMemoryUsage: MUsage
val objectPendingFinalizationCount:Int val objectPendingFinalizationCount: Int
} }
internal val memoryUsageGet: MemoryUsageGet = kotlin.runCatching { internal val memoryUsageGet: MemoryUsageGet = kotlin.runCatching {
ByMemoryMXBean ByMemoryMXBean
}.getOrElse { ByRuntime } }.getOrElse { ByRuntime }

View File

@ -1,10 +1,10 @@
/* /*
* Copyright 2019-2020 Mamoe Technologies and contributors. * Copyright 2019-2021 Mamoe Technologies and contributors.
* *
* 此源代码的使用受 GNU AFFERO GENERAL PUBLIC LICENSE version 3 许可证的约束, 可以在以下链接找到该许可证. * 此源代码的使用受 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 * https://github.com/mamoe/mirai/blob/master/LICENSE
*/ */
@file:OptIn(ConsoleExperimentalApi::class) @file:OptIn(ConsoleExperimentalApi::class)
@ -267,6 +267,7 @@ internal object MiraiConsoleImplementationBridge : CoroutineScope, MiraiConsoleI
runCatching { bot.login() }.getOrElse { runCatching { bot.login() }.getOrElse {
mainLogger.error(it) mainLogger.error(it)
bot.close()
} }
} }