introduction plugin

This commit is contained in:
InsanusMokrassar 2022-06-17 22:24:44 +06:00
parent 85f59d8745
commit 0ecf6cbf59
6 changed files with 84 additions and 3 deletions

View File

@ -23,6 +23,8 @@ repositories {
dependencies {
implementation libs.kotlin
implementation libs.plagubot
implementation project(":introduction")
}
application {

View File

@ -1,10 +1,10 @@
{
"botToken": "1234567890:ABCDEFGHIJKLMNOP_qrstuvwxyz12345678",
"plugins": [
"dev.inmo.plagubot.HelloPlugin"
"IntroductionPlugin"
],
"helloPlugin": {
"print": "Hello World"
"introduction": {
"onStartCommandMessage": "Hello World"
},
"database": {
"url": "jdbc:sqlite:file:local.db?mode=memory&cache=shared"

View File

@ -2,11 +2,14 @@
kotlin = "1.6.21"
plagubot = "1.1.1"
kslog = "0.3.1"
[libraries]
kotlin = { module = "org.jetbrains.kotlin:kotlin-stdlib", version.ref = "kotlin" }
plagubot = { module = "dev.inmo:plagubot.bot", version.ref = "plagubot" }
plagubot-plugin = { module = "dev.inmo:plagubot.plugin", version.ref = "plagubot" }
kslog = { module = "dev.inmo:kslog", version.ref = "kslog" }
# Libs for classpath
kotlin-gradle-plugin = { module = "org.jetbrains.kotlin:kotlin-gradle-plugin", version.ref = "kotlin" }

30
introduction/build.gradle Normal file
View File

@ -0,0 +1,30 @@
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
}
application {
mainClassName = 'dev.inmo.plagubot.AppKt'
}

View File

@ -0,0 +1,44 @@
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
@Serializable
class IntroductionPlugin : Plugin {
private val log = logger
@Serializable
private class Config(
val onStartCommandMessage: String
)
override fun Module.setupDI(database: Database, params: JsonObject) {
single { get<Json>().decodeFromJsonElement(Config.serializer(), params["introduction"] ?: return@single null) }
}
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 \"introduction\" field in config or some error during configuration loading")
return
}
onCommand("start", initialFilter = { it.chat is PrivateChat }) {
sendMessage(
it.chat,
config.onStartCommandMessage
)
}
}
}

View File

@ -1 +1,3 @@
rootProject.name = "bot"
include ":introduction"