mirror of
https://github.com/InsanusMokrassar/PlaguBot.git
synced 2024-11-22 07:33:46 +00:00
PlaguBot class
This commit is contained in:
parent
15109391b1
commit
8444172e44
@ -2,6 +2,12 @@
|
|||||||
|
|
||||||
## 0.1.7
|
## 0.1.7
|
||||||
|
|
||||||
|
* `PlaguBot`
|
||||||
|
* New class `PlaguBot` (😊)
|
||||||
|
* `initPlaguBot` is deprecated
|
||||||
|
* New shortcut for params - `plagubot`. `PlaguBot` class can be put inside other plagubot
|
||||||
|
for additional opportunities
|
||||||
|
|
||||||
## 0.1.6
|
## 0.1.6
|
||||||
|
|
||||||
* `Versions`
|
* `Versions`
|
||||||
|
@ -1,38 +1,22 @@
|
|||||||
package dev.inmo.plagubot
|
package dev.inmo.plagubot
|
||||||
|
|
||||||
import dev.inmo.micro_utils.coroutines.safelyWithoutExceptions
|
|
||||||
import dev.inmo.plagubot.config.*
|
import dev.inmo.plagubot.config.*
|
||||||
import dev.inmo.plagubot.config.configJsonFormat
|
import dev.inmo.plagubot.config.configJsonFormat
|
||||||
import dev.inmo.tgbotapi.bot.Ktor.telegramBot
|
|
||||||
import dev.inmo.tgbotapi.extensions.api.bot.setMyCommands
|
|
||||||
import dev.inmo.tgbotapi.extensions.behaviour_builder.buildBehaviour
|
|
||||||
import dev.inmo.tgbotapi.types.botCommandsLimit
|
|
||||||
import kotlinx.coroutines.*
|
import kotlinx.coroutines.*
|
||||||
import kotlinx.serialization.InternalSerializationApi
|
import kotlinx.serialization.InternalSerializationApi
|
||||||
import java.io.File
|
import java.io.File
|
||||||
|
|
||||||
|
@Deprecated(
|
||||||
|
"This method is redundant due to new class PlaguBot",
|
||||||
|
ReplaceWith(
|
||||||
|
"PlaguBot(config).start(scope)",
|
||||||
|
"dev.inmo.plagubot.PlaguBot"
|
||||||
|
)
|
||||||
|
)
|
||||||
suspend inline fun initPlaguBot(
|
suspend inline fun initPlaguBot(
|
||||||
config: Config,
|
config: Config,
|
||||||
scope: CoroutineScope = CoroutineScope(Dispatchers.Default)
|
scope: CoroutineScope = CoroutineScope(Dispatchers.Default)
|
||||||
): Job {
|
): Job = PlaguBot(config).start(scope)
|
||||||
val bot = telegramBot(config.botToken)
|
|
||||||
|
|
||||||
val paramsMap = config.params ?.toMap() ?: emptyMap()
|
|
||||||
val database = config.params ?.database ?: config.database.database
|
|
||||||
return bot.buildBehaviour(scope) {
|
|
||||||
val commands = config.plugins.flatMap {
|
|
||||||
it.apply { invoke(database, paramsMap) }
|
|
||||||
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)
|
|
||||||
}
|
|
||||||
safelyWithoutExceptions { setMyCommands(commands) }
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* This method by default expects one argument in [args] field: path to config
|
* This method by default expects one argument in [args] field: path to config
|
||||||
@ -43,6 +27,5 @@ suspend fun main(args: Array<String>) {
|
|||||||
val file = File(configPath)
|
val file = File(configPath)
|
||||||
val config = configJsonFormat.decodeFromString(ConfigSerializer, file.readText())
|
val config = configJsonFormat.decodeFromString(ConfigSerializer, file.readText())
|
||||||
|
|
||||||
val scope = CoroutineScope(Dispatchers.Default)
|
PlaguBot(config).start().join()
|
||||||
initPlaguBot(config, scope).join()
|
|
||||||
}
|
}
|
||||||
|
56
bot/src/main/kotlin/dev/inmo/plagubot/PlaguBot.kt
Normal file
56
bot/src/main/kotlin/dev/inmo/plagubot/PlaguBot.kt
Normal file
@ -0,0 +1,56 @@
|
|||||||
|
package dev.inmo.plagubot
|
||||||
|
|
||||||
|
import dev.inmo.micro_utils.coroutines.safelyWithoutExceptions
|
||||||
|
import dev.inmo.plagubot.config.Config
|
||||||
|
import dev.inmo.plagubot.config.database
|
||||||
|
import dev.inmo.tgbotapi.bot.Ktor.telegramBot
|
||||||
|
import dev.inmo.tgbotapi.extensions.api.bot.setMyCommands
|
||||||
|
import dev.inmo.tgbotapi.extensions.behaviour_builder.BehaviourContext
|
||||||
|
import dev.inmo.tgbotapi.extensions.behaviour_builder.buildBehaviour
|
||||||
|
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(
|
||||||
|
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)
|
||||||
|
): Job = bot.buildBehaviour(scope) {
|
||||||
|
invoke(database, paramsMap)
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user