add template module

This commit is contained in:
InsanusMokrassar 2022-07-09 13:41:59 +06:00
parent 5c1c4d0221
commit 3de960b26a
3 changed files with 86 additions and 4 deletions

View File

@ -24,7 +24,3 @@ dependencies {
api libs.plagubot.plugin api libs.plagubot.plugin
api libs.kslog api libs.kslog
} }
application {
mainClassName = 'dev.inmo.plagubot.AppKt'
}

26
template/build.gradle Normal file
View File

@ -0,0 +1,26 @@
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath libs.kotlin.gradle.plugin
classpath libs.kotlin.serialization.plugin
}
}
plugins {
alias libs.plugins.kotlin.jvm
alias libs.plugins.kotlin.serialization
}
repositories {
mavenCentral()
mavenLocal()
}
dependencies {
implementation libs.kotlin
api libs.plagubot.plugin
api libs.kslog
}

View File

@ -0,0 +1,60 @@
import dev.inmo.kslog.common.logger
import dev.inmo.kslog.common.w
import dev.inmo.plagubot.Plugin
import dev.inmo.tgbotapi.extensions.api.send.reply
import dev.inmo.tgbotapi.extensions.api.send.sendMessage
import dev.inmo.tgbotapi.extensions.behaviour_builder.BehaviourContext
import dev.inmo.tgbotapi.extensions.behaviour_builder.triggers_handling.onCommand
import dev.inmo.tgbotapi.extensions.behaviour_builder.triggers_handling.onMyChatMemberUpdated
import dev.inmo.tgbotapi.types.chat.PrivateChat
import kotlinx.serialization.Serializable
import kotlinx.serialization.json.*
import org.jetbrains.exposed.sql.Database
import org.koin.core.Koin
import org.koin.core.module.Module
/**
* This is template of plugin with preset [log]ger, [Config] and template configurations of [setupDI] and [setupBotPlugin].
* Replace [pluginConfigSectionName] value with your one to customize configuration section name
*/
@Serializable
class TemplatePlugin : Plugin {
/**
* Default logger of [TemplatePlugin] got with [logger]
*/
private val log = logger
/**
* Configuration class for current plugin
*
* See realization of [setupDI] to get know how this class will be deserialized from global config
*
* See realization of [setupBotPlugin] to get know how to get access to this class
*/
@Serializable
private class Config(
)
/**
* DI configuration of current plugin. Here we are decoding [Config] and put it into [Module] receiver
*/
override fun Module.setupDI(database: Database, params: JsonObject) {
single { get<Json>().decodeFromJsonElement(Config.serializer(), params[pluginConfigSectionName] ?: return@single null) }
}
/**
* Final configuration of bot. Here we are getting [Config] from [koin] and configure reaction on `/start` command
*/
override suspend fun BehaviourContext.setupBotPlugin(koin: Koin) {
val config = koin.getOrNull<Config>()
if (config == null) {
log.w("Plugin has been disabled due to absence of \"$pluginConfigSectionName\" field in config or some error during configuration loading")
return
}
}
companion object {
private const val pluginConfigSectionName = "new"
}
}