From ee1561f97d077530ad5fb2acf6de02cb0e32bbd3 Mon Sep 17 00:00:00 2001 From: Karlatemp Date: Thu, 11 Mar 2021 22:43:15 +0800 Subject: [PATCH] Close bot with `runCatching` --- backend/mirai-console/src/command/BuiltInCommands.kt | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/backend/mirai-console/src/command/BuiltInCommands.kt b/backend/mirai-console/src/command/BuiltInCommands.kt index 97cc717ba..bd7c69eaf 100644 --- a/backend/mirai-console/src/command/BuiltInCommands.kt +++ b/backend/mirai-console/src/command/BuiltInCommands.kt @@ -49,6 +49,7 @@ 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.MiraiLogger import net.mamoe.mirai.utils.secondsToMillis import java.lang.management.ManagementFactory import java.lang.management.MemoryUsage @@ -131,7 +132,15 @@ public object BuiltInCommands { if (!MiraiConsole.isActive) return@withLock sendMessage("Stopping mirai-console") kotlin.runCatching { - Bot.instances.forEach { it.closeAndJoin() } + Bot.instances.forEach { bot -> + lateinit var logger: MiraiLogger + kotlin.runCatching { + logger = bot.logger + bot.closeAndJoin() + }.onFailure { t -> + kotlin.runCatching { logger.error("Error in closing bot", t) } + } + } MiraiConsole.job.cancelAndJoin() }.fold( onSuccess = {