mirror of
https://github.com/InsanusMokrassar/PlaguPoster.git
synced 2025-12-08 14:05:40 +00:00
complete timer plugin
This commit is contained in:
@@ -2,14 +2,20 @@ package dev.inmo.plaguposter.triggers.timer
|
||||
|
||||
import com.soywiz.klock.DateTime
|
||||
import dev.inmo.micro_utils.coroutines.runCatchingSafely
|
||||
import dev.inmo.micro_utils.koin.singleWithRandomQualifierAndBinds
|
||||
import dev.inmo.micro_utils.repos.set
|
||||
import dev.inmo.plagubot.Plugin
|
||||
import dev.inmo.plaguposter.common.ChatConfig
|
||||
import dev.inmo.plaguposter.posts.models.PostId
|
||||
import dev.inmo.plaguposter.posts.repo.ReadPostsRepo
|
||||
import dev.inmo.plaguposter.triggers.timer.repo.ExposedTimersRepo
|
||||
import dev.inmo.tgbotapi.extensions.api.answers.answer
|
||||
import dev.inmo.tgbotapi.extensions.api.edit.edit
|
||||
import dev.inmo.tgbotapi.extensions.api.send.reply
|
||||
import dev.inmo.tgbotapi.extensions.api.send.send
|
||||
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.onMessageDataCallbackQuery
|
||||
import kotlinx.serialization.json.*
|
||||
import org.jetbrains.exposed.sql.Database
|
||||
import org.koin.core.Koin
|
||||
@@ -20,39 +26,41 @@ object Plugin : Plugin {
|
||||
override fun Module.setupDI(database: Database, params: JsonObject) {
|
||||
single { ExposedTimersRepo(get(), get(), get()) } binds arrayOf(TimersRepo::class)
|
||||
single(createdAtStart = true) { TimersHandler(get(), get(), get()) }
|
||||
singleWithRandomQualifierAndBinds { TimerPanelButton(get()) }
|
||||
}
|
||||
|
||||
override suspend fun BehaviourContext.setupBotPlugin(koin: Koin) {
|
||||
val postsRepo = koin.get<ReadPostsRepo>()
|
||||
val timersRepo = koin.get<TimersRepo>()
|
||||
val chatsConfig = koin.get<ChatConfig>()
|
||||
with(ButtonsBuilder) {
|
||||
includeKeyboardHandling { postId, dateTime ->
|
||||
includeKeyboardHandling(timersRepo) { postId, dateTime ->
|
||||
timersRepo.set(postId, dateTime)
|
||||
true
|
||||
}
|
||||
}
|
||||
onCommand("test") {
|
||||
val reply = it.replyTo ?: return@onCommand
|
||||
val postId = postsRepo.getIdByChatAndMessage(
|
||||
reply.chat.id,
|
||||
reply.messageId
|
||||
) ?: return@onCommand
|
||||
onMessageDataCallbackQuery(
|
||||
Regex("${TimerPanelButton.timerSetPrefix} [^\\s]+"),
|
||||
initialFilter = {
|
||||
chatsConfig.check(it.message.chat.id)
|
||||
}
|
||||
) {
|
||||
val (_, postIdRaw) = it.data.split(" ")
|
||||
val postId = PostId(postIdRaw)
|
||||
val now = nearestAvailableTimerTime()
|
||||
val exists = timersRepo.get(postId)
|
||||
val textSources = ButtonsBuilder.buildTimerTextSources(now, exists)
|
||||
val buttons = ButtonsBuilder.buildTimerButtons(
|
||||
postId,
|
||||
DateTime.nowLocal()
|
||||
now.local,
|
||||
exists != null
|
||||
)
|
||||
runCatchingSafely {
|
||||
edit(
|
||||
it,
|
||||
buttons
|
||||
)
|
||||
}.onFailure { _ ->
|
||||
send(
|
||||
it.chat,
|
||||
"Buttons",
|
||||
replyMarkup = buttons
|
||||
)
|
||||
}
|
||||
reply(
|
||||
it.message,
|
||||
textSources,
|
||||
replyMarkup = buttons
|
||||
)
|
||||
|
||||
answer(it)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user