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

47 lines
1.6 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
2020-11-12 05:52:09 +00:00
import dev.inmo.plagubot.config.Config
import dev.inmo.plagubot.config.configSerialFormat
2020-11-11 17:05:21 +00:00
import dev.inmo.tgbotapi.extensions.api.bot.setMyCommands
2020-11-08 13:04:44 +00:00
import dev.inmo.tgbotapi.extensions.api.telegramBot
2020-11-11 17:05:21 +00:00
import dev.inmo.tgbotapi.extensions.utils.updates.retrieving.startGettingFlowsUpdatesByLongPolling
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-11 17:05:21 +00:00
* This method by default expects one argument in [args] field: path to config
2020-11-08 13:04:44 +00:00
*/
2020-11-12 05:52:09 +00:00
@InternalSerializationApi
2020-11-08 13:04:44 +00:00
suspend fun main(args: Array<String>) {
2020-11-11 17:05:21 +00:00
val (configPath) = args
val file = File(configPath)
val config = configSerialFormat.decodeFromString(Config.serializer(), file.readText())
val scope = CoroutineScope(Dispatchers.Default)
val bot = telegramBot(config.botToken)
bot.startGettingFlowsUpdatesByLongPolling(scope = scope) {
val commands = config.plugins.flatMap {
it.invoke(bot, config.database.database, this, scope)
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)
}
scope.launch {
safelyWithoutExceptions {
bot.setMyCommands(commands)
}
}
}
2020-11-12 06:20:18 +00:00
scope.coroutineContext.job.join()
2020-11-08 13:04:44 +00:00
}