mirror of
https://github.com/InsanusMokrassar/PlaguBot.git
synced 2025-09-16 13:59:26 +00:00
rework of plagubot system
This commit is contained in:
64
plugin/src/main/kotlin/dev/inmo/plagubot/HelloPlugin.kt
Normal file
64
plugin/src/main/kotlin/dev/inmo/plagubot/HelloPlugin.kt
Normal file
@@ -0,0 +1,64 @@
|
||||
package dev.inmo.plagubot
|
||||
|
||||
import dev.inmo.kslog.common.*
|
||||
import dev.inmo.micro_utils.fsm.common.State
|
||||
import dev.inmo.tgbotapi.extensions.api.bot.getMe
|
||||
import dev.inmo.tgbotapi.extensions.api.send.reply
|
||||
import dev.inmo.tgbotapi.extensions.api.send.sendMessage
|
||||
import dev.inmo.tgbotapi.extensions.behaviour_builder.*
|
||||
import dev.inmo.tgbotapi.extensions.behaviour_builder.expectations.waitTextMessage
|
||||
import dev.inmo.tgbotapi.extensions.behaviour_builder.triggers_handling.onCommand
|
||||
import dev.inmo.tgbotapi.types.IdChatIdentifier
|
||||
import kotlinx.coroutines.flow.first
|
||||
import kotlinx.serialization.SerialName
|
||||
import kotlinx.serialization.Serializable
|
||||
import kotlinx.serialization.json.Json
|
||||
import kotlinx.serialization.json.JsonObject
|
||||
import org.koin.core.Koin
|
||||
import org.koin.core.module.Module
|
||||
|
||||
@Serializable
|
||||
@SerialName("Hello")
|
||||
object HelloPlugin : Plugin {
|
||||
@Serializable
|
||||
data class HelloPluginConfig(
|
||||
val print: String
|
||||
)
|
||||
|
||||
override fun Module.setupDI(config: JsonObject) {
|
||||
single {
|
||||
get<Json>().decodeFromJsonElement(HelloPluginConfig.serializer(), config["helloPlugin"] ?: return@single null)
|
||||
}
|
||||
}
|
||||
|
||||
private sealed interface InternalFSMState : State {
|
||||
override val context: IdChatIdentifier
|
||||
data class DidntSaidHello(override val context: IdChatIdentifier) : InternalFSMState
|
||||
data class SaidHelloOnce(override val context: IdChatIdentifier) : InternalFSMState
|
||||
}
|
||||
|
||||
override suspend fun startPlugin(koin: Koin) {
|
||||
super.startPlugin(koin)
|
||||
logger.i { "This logic called BEFORE the bot will be started and setup" }
|
||||
}
|
||||
|
||||
override suspend fun BehaviourContextWithFSM<State>.setupBotPlugin(koin: Koin) {
|
||||
val toPrint = koin.getOrNull<HelloPluginConfig>() ?.print ?: "Hello :)"
|
||||
logger.d { toPrint }
|
||||
logger.dS { getMe().toString() }
|
||||
onCommand("hello_world") {
|
||||
startChain(InternalFSMState.DidntSaidHello(it.chat.id))
|
||||
}
|
||||
|
||||
strictlyOn { state: InternalFSMState.DidntSaidHello ->
|
||||
sendMessage(state.context, toPrint)
|
||||
InternalFSMState.SaidHelloOnce(state.context)
|
||||
}
|
||||
|
||||
strictlyOn { state: InternalFSMState.SaidHelloOnce ->
|
||||
val message = waitTextMessage().first()
|
||||
reply(message, "Sorry, I can answer only this: $toPrint")
|
||||
InternalFSMState.SaidHelloOnce(state.context)
|
||||
}
|
||||
}
|
||||
}
|
11
plugin/src/main/kotlin/dev/inmo/plagubot/KoinExtensions.kt
Normal file
11
plugin/src/main/kotlin/dev/inmo/plagubot/KoinExtensions.kt
Normal file
@@ -0,0 +1,11 @@
|
||||
package dev.inmo.plagubot
|
||||
|
||||
import org.jetbrains.exposed.sql.Database
|
||||
import org.koin.core.Koin
|
||||
import org.koin.core.scope.Scope
|
||||
|
||||
val Scope.database: Database
|
||||
get() = get()
|
||||
|
||||
val Koin.database: Database
|
||||
get() = get()
|
@@ -7,9 +7,7 @@ import dev.inmo.tgbotapi.extensions.behaviour_builder.BehaviourContext
|
||||
import dev.inmo.tgbotapi.extensions.behaviour_builder.BehaviourContextWithFSM
|
||||
import kotlinx.serialization.Serializable
|
||||
import kotlinx.serialization.json.JsonObject
|
||||
import org.jetbrains.exposed.sql.Database
|
||||
import org.koin.core.Koin
|
||||
import org.koin.core.module.Module
|
||||
import org.koin.core.scope.Scope
|
||||
|
||||
/**
|
||||
@@ -32,27 +30,13 @@ interface Plugin : StartPlugin {
|
||||
*/
|
||||
fun KtorRequestsExecutorBuilder.setupBotClient(scope: Scope, params: JsonObject) = setupBotClient()
|
||||
|
||||
/**
|
||||
* This method will be called when this plugin should configure di module based on the incoming params
|
||||
*/
|
||||
fun Module.setupDI(
|
||||
database: Database,
|
||||
params: JsonObject
|
||||
) {
|
||||
setupDI(params)
|
||||
}
|
||||
|
||||
/**
|
||||
* Override this method in cases when you want to declare common bot behaviour. In case you wish to use FSM, you
|
||||
* should override the method with receiver [BehaviourContextWithFSM]
|
||||
*
|
||||
* Besides, this method by default will call [startPlugin]
|
||||
*/
|
||||
suspend fun BehaviourContext.setupBotPlugin(
|
||||
koin: Koin
|
||||
) {
|
||||
startPlugin(koin)
|
||||
}
|
||||
) {}
|
||||
/**
|
||||
* Override this method in cases when you want to declare full behaviour of the plugin. It is recommended to declare
|
||||
* common logic of plugin in the [setupBotPlugin] with [BehaviourContext] receiver and use override this one
|
||||
|
Reference in New Issue
Block a user