add welcome plugin (currently uncompilable) and small fix in kdoc of template

This commit is contained in:
InsanusMokrassar 2022-07-09 13:44:12 +06:00
parent 3de960b26a
commit 5861b909cd
3 changed files with 88 additions and 2 deletions

View File

@ -20,7 +20,7 @@ import org.koin.core.module.Module
@Serializable
class TemplatePlugin : Plugin {
/**
* Default logger of [TemplatePlugin] got with [logger]
* Default logger of [WelcomePlugin] got with [logger]
*/
private val log = logger
@ -43,7 +43,7 @@ class TemplatePlugin : Plugin {
}
/**
* Final configuration of bot. Here we are getting [Config] from [koin] and configure reaction on `/start` command
* Final configuration of bot. Here we are getting [Config] from [koin]
*/
override suspend fun BehaviourContext.setupBotPlugin(koin: Koin) {
val config = koin.getOrNull<Config>()

26
welcome/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 WelcomePlugin : Plugin {
/**
* Default logger of [WelcomePlugin] 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]
*/
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 = "welcome"
}
}