PlaguBot/bot/src/main/kotlin/dev/inmo/plagubot/App.kt

49 lines
1.7 KiB
Kotlin
Raw Normal View History

2020-11-08 13:23:01 +00:00
package dev.inmo.plagubot
2020-11-08 13:04:44 +00:00
2020-11-11 17:05:21 +00:00
import dev.inmo.micro_utils.coroutines.safelyWithoutExceptions
2021-02-16 19:21:41 +00:00
import dev.inmo.plagubot.config.*
2021-02-22 13:08:49 +00:00
import dev.inmo.plagubot.config.configJsonFormat
2021-01-10 09:04:45 +00:00
import dev.inmo.tgbotapi.bot.Ktor.telegramBot
2020-11-11 17:05:21 +00:00
import dev.inmo.tgbotapi.extensions.api.bot.setMyCommands
2021-01-10 09:21:31 +00:00
import dev.inmo.tgbotapi.extensions.behaviour_builder.buildBehaviour
2020-11-11 17:05:21 +00:00
import dev.inmo.tgbotapi.types.botCommandsLimit
import kotlinx.coroutines.*
2020-11-12 05:52:09 +00:00
import kotlinx.serialization.InternalSerializationApi
2020-11-11 17:05:21 +00:00
import java.io.File
2020-11-08 13:04:44 +00:00
2020-11-13 05:25:04 +00:00
suspend inline fun initPlaguBot(
config: Config,
scope: CoroutineScope = CoroutineScope(Dispatchers.Default)
2021-02-17 15:11:24 +00:00
): Job {
2020-11-11 17:05:21 +00:00
val bot = telegramBot(config.botToken)
2021-02-16 19:21:41 +00:00
val paramsMap = config.params ?.toMap() ?: emptyMap()
val database = config.params ?.database ?: config.database.database
2021-02-17 15:11:24 +00:00
return bot.buildBehaviour(scope) {
2020-11-11 17:05:21 +00:00
val commands = config.plugins.flatMap {
2021-02-16 19:21:41 +00:00
it.apply { invoke(database, paramsMap) }
2020-11-11 17:05:21 +00:00
it.getCommands()
}.let {
val futureUnavailable = it.drop(botCommandsLimit.last)
if (futureUnavailable.isNotEmpty()) {
println("Next commands are out of range in setting command request and will be unavailable from autocompleting: ${futureUnavailable}")
}
it.take(botCommandsLimit.last)
}
2021-01-10 09:21:31 +00:00
safelyWithoutExceptions { setMyCommands(commands) }
2020-11-11 17:05:21 +00:00
}
2020-11-13 05:25:04 +00:00
}
2020-11-12 06:20:18 +00:00
2020-11-13 05:25:04 +00:00
/**
* This method by default expects one argument in [args] field: path to config
*/
@InternalSerializationApi
suspend fun main(args: Array<String>) {
val (configPath) = args
val file = File(configPath)
2021-02-22 13:08:49 +00:00
val config = configJsonFormat.decodeFromString(ConfigSerializer, file.readText())
2020-11-13 05:25:04 +00:00
val scope = CoroutineScope(Dispatchers.Default)
2021-02-17 15:11:24 +00:00
initPlaguBot(config, scope).join()
2020-11-08 13:04:44 +00:00
}