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) {