From 51d036c4c63088f922116da3aca392446332420d Mon Sep 17 00:00:00 2001 From: LamGC Date: Mon, 4 Jul 2022 16:40:17 +0800 Subject: [PATCH] =?UTF-8?q?feat(launch):=20=E5=BB=B6=E5=90=8E=20BotConfig?= =?UTF-8?q?=20=E7=9A=84=E5=8F=8D=E5=BA=8F=E5=88=97=E5=8C=96=E6=97=B6?= =?UTF-8?q?=E6=9C=BA.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 通过将 BotConfig 的反序列化时机延后到启动机器人的时候, 可以避免因某个机器人配置错误导致所有机器人都无法启动的问题. 注意, 语法错误还是会在启动时报错, 只是说部分序列化器会检查字段值是否有误, 通过延后反序列化来防止全反序列化的时候一个配置炸了影响全部而已. --- scalabot-app/src/main/kotlin/AppConfigs.kt | 10 +++---- scalabot-app/src/main/kotlin/AppMain.kt | 32 +++++++++++++++++----- 2 files changed, 30 insertions(+), 12 deletions(-) diff --git a/scalabot-app/src/main/kotlin/AppConfigs.kt b/scalabot-app/src/main/kotlin/AppConfigs.kt index b692e64..2626a54 100644 --- a/scalabot-app/src/main/kotlin/AppConfigs.kt +++ b/scalabot-app/src/main/kotlin/AppConfigs.kt @@ -3,7 +3,7 @@ package net.lamgc.scalabot import ch.qos.logback.core.PropertyDefinerBase import com.google.gson.Gson import com.google.gson.GsonBuilder -import com.google.gson.reflect.TypeToken +import com.google.gson.JsonArray import mu.KotlinLogging import net.lamgc.scalabot.config.* import net.lamgc.scalabot.config.serializer.* @@ -221,8 +221,8 @@ internal fun initialFiles() { } } -private object GsonConst { - val baseGson: Gson = GsonBuilder() +internal object GsonConst { + private val baseGson: Gson = GsonBuilder() .setPrettyPrinting() .serializeNulls() .create() @@ -253,10 +253,10 @@ internal fun loadAppConfig(configFile: File = AppPaths.CONFIG_APPLICATION.file): } } -internal fun loadBotConfig(botConfigFile: File = AppPaths.CONFIG_BOT.file): Set? { +internal fun loadBotConfigJson(botConfigFile: File = AppPaths.CONFIG_BOT.file): JsonArray? { try { botConfigFile.bufferedReader(StandardCharsets.UTF_8).use { - return GsonConst.botConfigGson.fromJson(it, object : TypeToken>() {}.type)!! + return GsonConst.botConfigGson.fromJson(it, JsonArray::class.java)!! } } catch (e: Exception) { log.error(e) { "读取 Bot 配置文件 (bot.json) 时发生错误, 请检查配置格式是否正确." } diff --git a/scalabot-app/src/main/kotlin/AppMain.kt b/scalabot-app/src/main/kotlin/AppMain.kt index 41db020..d96fbd4 100644 --- a/scalabot-app/src/main/kotlin/AppMain.kt +++ b/scalabot-app/src/main/kotlin/AppMain.kt @@ -1,5 +1,6 @@ package net.lamgc.scalabot +import com.google.gson.JsonParseException import io.prometheus.client.exporter.HTTPServer import kotlinx.coroutines.runBlocking import mu.KotlinLogging @@ -110,22 +111,39 @@ internal class Launcher(private val config: AppConfig = Const.config) : AutoClos @Synchronized fun launch(): Boolean { - val botConfigs = loadBotConfig() ?: return false - if (botConfigs.isEmpty()) { + val botConfigs = loadBotConfigJson() ?: return false + if (botConfigs.isEmpty) { log.warn { "尚未配置任何机器人, 请先配置机器人后再启动本程序." } return false - } else if (botConfigs.none { it.enabled }) { - log.warn { "配置文件中没有已启用的机器人, 请至少启用一个机器人." } - return false } - for (botConfig in botConfigs) { + var launchedCounts = 0 + for (botConfigJson in botConfigs) { + val botConfig = try { + GsonConst.botConfigGson.fromJson(botConfigJson, BotConfig::class.java) + } catch (e: JsonParseException) { + val botName = try { + botConfigJson.asJsonObject.get("account")?.asJsonObject?.get("name")?.asString ?: "Unknown" + } catch (e: Exception) { + "Unknown" + } + log.error(e) { "机器人 `$botName` 配置有误, 跳过该机器人的启动." } + continue + } + try { launchBot(botConfig) + launchedCounts++ } catch (e: Exception) { log.error(e) { "机器人 `${botConfig.account.name}` 启动时发生错误." } } } - return true + return if (launchedCounts != 0) { + log.info { "已启动 $launchedCounts 个机器人." } + true + } else { + log.warn { "未启动任何机器人, 请检查配置并至少启用一个机器人." } + false + } } private fun launchBot(botConfig: BotConfig) {