mirror of
https://github.com/InsanusMokrassar/PlaguBot.git
synced 2025-09-16 05:52:59 +00:00
first version
This commit is contained in:
44
bot/src/main/kotlin/dev/inmo/plagubot/App.kt
Normal file
44
bot/src/main/kotlin/dev/inmo/plagubot/App.kt
Normal file
@@ -0,0 +1,44 @@
|
||||
package dev.inmo.plagubot
|
||||
|
||||
import dev.inmo.micro_utils.coroutines.safelyWithoutExceptions
|
||||
import dev.inmo.plagubot.config.Config
|
||||
import dev.inmo.plagubot.config.configSerialFormat
|
||||
import dev.inmo.tgbotapi.extensions.api.bot.setMyCommands
|
||||
import dev.inmo.tgbotapi.extensions.api.telegramBot
|
||||
import dev.inmo.tgbotapi.extensions.utils.updates.retrieving.startGettingFlowsUpdatesByLongPolling
|
||||
import dev.inmo.tgbotapi.types.botCommandsLimit
|
||||
import kotlinx.coroutines.*
|
||||
import kotlinx.serialization.InternalSerializationApi
|
||||
import java.io.File
|
||||
|
||||
/**
|
||||
* 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)
|
||||
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)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
23
bot/src/main/kotlin/dev/inmo/plagubot/HelloPlugin.kt
Normal file
23
bot/src/main/kotlin/dev/inmo/plagubot/HelloPlugin.kt
Normal file
@@ -0,0 +1,23 @@
|
||||
package dev.inmo.plagubot
|
||||
|
||||
import dev.inmo.tgbotapi.bot.TelegramBot
|
||||
import dev.inmo.tgbotapi.updateshandlers.FlowsUpdatesFilter
|
||||
import kotlinx.coroutines.CoroutineScope
|
||||
import kotlinx.serialization.SerialName
|
||||
import kotlinx.serialization.Serializable
|
||||
import org.jetbrains.exposed.sql.Database
|
||||
|
||||
@Serializable
|
||||
@SerialName("Hello")
|
||||
data class HelloPlugin(
|
||||
val parameter: String
|
||||
) : Plugin {
|
||||
override suspend fun invoke(
|
||||
bot: TelegramBot,
|
||||
database: Database,
|
||||
updatesFilter: FlowsUpdatesFilter,
|
||||
scope: CoroutineScope
|
||||
) {
|
||||
println(parameter)
|
||||
}
|
||||
}
|
42
bot/src/main/kotlin/dev/inmo/plagubot/config/Config.kt
Normal file
42
bot/src/main/kotlin/dev/inmo/plagubot/config/Config.kt
Normal file
@@ -0,0 +1,42 @@
|
||||
package dev.inmo.plagubot.config
|
||||
|
||||
import com.github.matfax.klassindex.KlassIndex
|
||||
import dev.inmo.plagubot.Plugin
|
||||
import kotlinx.serialization.*
|
||||
import kotlinx.serialization.json.Json
|
||||
import kotlinx.serialization.modules.*
|
||||
import kotlin.reflect.KClass
|
||||
|
||||
@InternalSerializationApi
|
||||
internal inline fun <T : Plugin> KClass<T>.includeIn(builder: PolymorphicModuleBuilder<Plugin>) = builder.subclass(this, serializer())
|
||||
|
||||
@InternalSerializationApi
|
||||
internal val configSerialFormat: StringFormat
|
||||
get() = Json {
|
||||
ignoreUnknownKeys = true
|
||||
serializersModule = SerializersModule {
|
||||
polymorphic(Plugin::class) {
|
||||
KlassIndex.getSubclasses(Plugin::class).flatMap { kclass ->
|
||||
kclass.includeIn(this)
|
||||
kclass.annotations.mapNotNull { it as? SerialName }.map {
|
||||
it.value to kclass.serializer()
|
||||
} + listOfNotNull(
|
||||
kclass.simpleName ?.let {
|
||||
it to kclass.serializer()
|
||||
}
|
||||
)
|
||||
}.toMap().let {
|
||||
default { requiredType ->
|
||||
it[requiredType]
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Serializable
|
||||
data class Config(
|
||||
val plugins: List<@Contextual Plugin>,
|
||||
val database: DatabaseConfig,
|
||||
val botToken: String
|
||||
)
|
@@ -0,0 +1,40 @@
|
||||
package dev.inmo.plagubot.config
|
||||
|
||||
import kotlinx.serialization.Serializable
|
||||
import kotlinx.serialization.Transient
|
||||
import org.jetbrains.exposed.sql.Database
|
||||
import org.jetbrains.exposed.sql.transactions.transactionManager
|
||||
import org.sqlite.JDBC
|
||||
import java.sql.Connection
|
||||
|
||||
@Serializable
|
||||
data class DatabaseConfig(
|
||||
val url: String = "jdbc:sqlite:file:test?mode=memory&cache=shared",
|
||||
val driver: String = JDBC::class.qualifiedName!!,
|
||||
val username: String = "",
|
||||
val password: String = "",
|
||||
val initAutomatically: Boolean = true
|
||||
) {
|
||||
@Transient
|
||||
private lateinit var _database: Database
|
||||
val database: Database
|
||||
get() = try {
|
||||
_database
|
||||
} catch (e: UninitializedPropertyAccessException) {
|
||||
Database.connect(
|
||||
url,
|
||||
driver,
|
||||
username,
|
||||
password
|
||||
).also {
|
||||
_database = it
|
||||
it.transactionManager.defaultIsolationLevel = Connection.TRANSACTION_SERIALIZABLE // Or Connection.TRANSACTION_READ_UNCOMMITTED
|
||||
}
|
||||
}
|
||||
|
||||
init {
|
||||
if (initAutomatically) {
|
||||
database // init database
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user