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

56 lines
2.0 KiB
Kotlin
Raw Normal View History

2021-04-03 16:04:18 +00:00
package dev.inmo.plagubot
import dev.inmo.micro_utils.coroutines.safelyWithoutExceptions
import dev.inmo.plagubot.config.*
2021-04-03 16:04:18 +00:00
import dev.inmo.tgbotapi.bot.Ktor.telegramBot
import dev.inmo.tgbotapi.extensions.api.bot.setMyCommands
2021-11-12 08:05:42 +00:00
import dev.inmo.tgbotapi.extensions.behaviour_builder.*
2021-04-03 16:04:18 +00:00
import dev.inmo.tgbotapi.types.BotCommand
import dev.inmo.tgbotapi.types.botCommandsLimit
import kotlinx.coroutines.*
import kotlinx.serialization.Serializable
import kotlinx.serialization.Transient
import org.jetbrains.exposed.sql.Database
const val DefaultPlaguBotParamsKey = "plagubot"
val Map<String, Any>.plagubot
get() = get(DefaultPlaguBotParamsKey) as? PlaguBot
@Serializable
data class PlaguBot(
@Serializable(PluginsConfigurationSerializer::class)
2021-04-03 16:04:18 +00:00
private val config: Config
) : Plugin {
@Transient
private val bot = telegramBot(config.botToken)
@Transient
private val paramsMap = config.params ?.toMap() ?: emptyMap()
@Transient
private val database = config.params ?.database ?: config.database.database
override suspend fun getCommands(): List<BotCommand> = config.plugins.flatMap {
it.getCommands()
}
override suspend fun BehaviourContext.invoke(database: Database, params: Map<String, Any>) {
config.plugins.forEach {
it.apply { invoke(database, params) }
}
val commands = getCommands()
val futureUnavailable = commands.drop(botCommandsLimit.last)
if (futureUnavailable.isNotEmpty()) {
println("Next commands are out of range in setting command request and will be unavailable from autocompleting: $futureUnavailable")
}
safelyWithoutExceptions { setMyCommands(commands.take(botCommandsLimit.last)) }
}
/**
* This method will create an [Job] which will be the main [Job] of ran instance
*/
suspend fun start(
scope: CoroutineScope = CoroutineScope(Dispatchers.Default)
2021-11-12 08:05:42 +00:00
): Job = bot.buildBehaviourWithLongPolling(scope) {
2021-04-03 16:04:18 +00:00
invoke(database, paramsMap)
}
}