PlaguPoster/triggers/timer/src/jvmMain/kotlin/Plugin.kt

67 lines
2.6 KiB
Kotlin
Raw Normal View History

2022-12-13 06:32:18 +00:00
package dev.inmo.plaguposter.triggers.timer
2022-12-13 07:16:55 +00:00
import com.soywiz.klock.DateTime
import dev.inmo.micro_utils.coroutines.runCatchingSafely
2022-12-14 05:50:02 +00:00
import dev.inmo.micro_utils.koin.singleWithRandomQualifierAndBinds
2022-12-14 03:43:12 +00:00
import dev.inmo.micro_utils.repos.set
2022-12-13 06:32:18 +00:00
import dev.inmo.plagubot.Plugin
2022-12-14 05:50:02 +00:00
import dev.inmo.plaguposter.common.ChatConfig
import dev.inmo.plaguposter.posts.models.PostId
2022-12-13 07:16:55 +00:00
import dev.inmo.plaguposter.posts.repo.ReadPostsRepo
2022-12-14 03:43:12 +00:00
import dev.inmo.plaguposter.triggers.timer.repo.ExposedTimersRepo
2022-12-14 05:50:02 +00:00
import dev.inmo.tgbotapi.extensions.api.answers.answer
2022-12-13 07:16:55 +00:00
import dev.inmo.tgbotapi.extensions.api.edit.edit
2022-12-14 05:50:02 +00:00
import dev.inmo.tgbotapi.extensions.api.send.reply
2022-12-13 07:16:55 +00:00
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
2022-12-14 05:50:02 +00:00
import dev.inmo.tgbotapi.extensions.behaviour_builder.triggers_handling.onMessageDataCallbackQuery
2022-12-13 06:32:18 +00:00
import kotlinx.serialization.json.*
import org.jetbrains.exposed.sql.Database
2022-12-13 07:16:55 +00:00
import org.koin.core.Koin
2022-12-13 06:32:18 +00:00
import org.koin.core.module.Module
2022-12-14 03:43:12 +00:00
import org.koin.dsl.binds
2022-12-13 06:32:18 +00:00
object Plugin : Plugin {
override fun Module.setupDI(database: Database, params: JsonObject) {
2022-12-14 03:43:12 +00:00
single { ExposedTimersRepo(get(), get(), get()) } binds arrayOf(TimersRepo::class)
single(createdAtStart = true) { TimersHandler(get(), get(), get()) }
2022-12-14 05:50:02 +00:00
singleWithRandomQualifierAndBinds { TimerPanelButton(get()) }
2022-12-13 06:32:18 +00:00
}
2022-12-13 07:16:55 +00:00
override suspend fun BehaviourContext.setupBotPlugin(koin: Koin) {
2022-12-14 03:43:12 +00:00
val timersRepo = koin.get<TimersRepo>()
2022-12-14 05:50:02 +00:00
val chatsConfig = koin.get<ChatConfig>()
2022-12-13 07:16:55 +00:00
with(ButtonsBuilder) {
2022-12-14 05:50:02 +00:00
includeKeyboardHandling(timersRepo) { postId, dateTime ->
2022-12-14 03:43:12 +00:00
timersRepo.set(postId, dateTime)
true
}
2022-12-13 07:16:55 +00:00
}
2022-12-14 05:50:02 +00:00
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)
2022-12-13 07:16:55 +00:00
val buttons = ButtonsBuilder.buildTimerButtons(
postId,
2022-12-14 05:50:02 +00:00
now.local,
exists != null
2022-12-13 07:16:55 +00:00
)
2022-12-14 05:50:02 +00:00
reply(
it.message,
textSources,
replyMarkup = buttons
)
answer(it)
2022-12-13 07:16:55 +00:00
}
}
2022-12-13 06:32:18 +00:00
}