initial: 基本完成的首个版本, 还需要调整一下.

暂时按照当初的计划实现了一个可用版本出来, 发布与否晚些再确定.
This commit is contained in:
2022-01-16 20:21:18 +08:00
parent cbaeb2ce00
commit 37f0d4e6b8
28 changed files with 1513 additions and 0 deletions

View File

@ -0,0 +1,88 @@
package net.lamgc.scalabot
import kotlinx.coroutines.runBlocking
import mu.KotlinLogging
import org.telegram.telegrambots.bots.DefaultBotOptions
import org.telegram.telegrambots.meta.TelegramBotsApi
import org.telegram.telegrambots.meta.generics.BotSession
import org.telegram.telegrambots.updatesreceivers.DefaultBotSession
import kotlin.system.exitProcess
private val log = KotlinLogging.logger { }
private val launcher = Launcher()
fun main(args: Array<String>): Unit = runBlocking {
log.info { "ScalaBot 正在启动中..." }
log.debug { "启动参数: ${args.joinToString(prefix = "[", postfix = "]")}" }
initialFiles()
if (!launcher.launch()) {
exitProcess(1)
}
}
internal class Launcher {
companion object {
@JvmStatic
private val log = KotlinLogging.logger { }
}
private val botApi = TelegramBotsApi(DefaultBotSession::class.java)
private val botSessionMap = mutableMapOf<ScalaBot, BotSession>()
fun launch(): Boolean {
val botConfigs = loadBotConfig()
if (botConfigs.isEmpty()) {
log.warn { "尚未配置任何机器人, 请先配置机器人后再启动本程序." }
return false
}
for (botConfig in botConfigs) {
launchBot(botConfig)
}
return true
}
private fun launchBot(botConfig: BotConfig) {
if (!botConfig.enabled) {
log.debug { "机器人 `${botConfig.account.name}` 已禁用, 跳过启动." }
return
}
log.info { "正在启动机器人 `${botConfig.account.name}`..." }
val botOption = DefaultBotOptions().apply {
val proxyConfig =
if (botConfig.proxy != null && botConfig.proxy.type != DefaultBotOptions.ProxyType.NO_PROXY) {
botConfig.proxy
} else if (Const.config.proxy.type != DefaultBotOptions.ProxyType.NO_PROXY) {
Const.config.proxy
} else {
null
}
if (proxyConfig != null) {
proxyType = proxyConfig.type
proxyHost = Const.config.proxy.host
proxyPort = Const.config.proxy.port
log.debug { "机器人 `${botConfig.account.name}` 已启用代理配置: $proxyConfig" }
}
if (botConfig.baseApiUrl != null) {
baseUrl = botConfig.baseApiUrl
}
}
val account = botConfig.account
val bot = ScalaBot(
account.name,
account.token,
account.creatorId,
BotDBMaker.getBotMaker(account),
botOption,
botConfig.extensions,
botConfig.disableBuiltInAbility
)
botSessionMap[bot] = botApi.registerBot(bot)
log.info { "机器人 `${bot.botUsername}` 已启动." }
}
}