temporal progress

This commit is contained in:
InsanusMokrassar 2022-05-21 17:31:52 +06:00
parent e375170567
commit 39c7b42778
3 changed files with 81 additions and 85 deletions

View File

@ -7,9 +7,9 @@ kotlin.incremental=true
kotlin_version=1.6.21 kotlin_version=1.6.21
kotlin_coroutines_version=1.6.1 kotlin_coroutines_version=1.6.1
kotlin_serialisation_runtime_version=1.3.3 kotlin_serialisation_runtime_version=1.3.3
plagubot_version=1.0.0 plagubot_version=1.0.1
micro_utils_version=0.10.4 micro_utils_version=0.10.5
tgbotapi_libraries_version=0.1.0 tgbotapi_libraries_version=0.1.0
project_group=dev.inmo project_group=dev.inmo

View File

@ -106,13 +106,11 @@ class CaptchaBotPlugin : Plugin {
onNewChatMembers( onNewChatMembers(
initialFilter = { initialFilter = {
it.chat.asPublicChat() != null it.chat is GroupChat
}, }
subcontextUpdatesFilter = { m, u -> u.sourceChat() == m.chat },
) { ) {
launchSafelyWithoutExceptions {
val settings = it.chat.settings() val settings = it.chat.settings()
if (!settings.enabled) return@launchSafelyWithoutExceptions if (!settings.enabled) return@onNewChatMembers
safelyWithoutExceptions { safelyWithoutExceptions {
if (settings.autoRemoveEvents) { if (settings.autoRemoveEvents) {
@ -130,11 +128,8 @@ class CaptchaBotPlugin : Plugin {
} }
val defaultChatPermissions = (getChat(it.chat) as ExtendedGroupChat).permissions val defaultChatPermissions = (getChat(it.chat) as ExtendedGroupChat).permissions
createSubContextAndDoWithUpdatesFilter(stopOnCompletion = false) { with (settings.captchaProvider) {
launch { doAction(it.date, chat, newUsers, defaultChatPermissions)
settings.captchaProvider.apply { doAction(it.date, chat, newUsers, defaultChatPermissions) }
}
}
} }
} }

View File

@ -3,19 +3,20 @@ package dev.inmo.plagubot.plugins.captcha.provider
import com.benasher44.uuid.uuid4 import com.benasher44.uuid.uuid4
import com.soywiz.klock.DateTime import com.soywiz.klock.DateTime
import com.soywiz.klock.seconds import com.soywiz.klock.seconds
import dev.inmo.micro_utils.coroutines.safelyWithResult import dev.inmo.micro_utils.coroutines.*
import dev.inmo.micro_utils.coroutines.safelyWithoutExceptions
import dev.inmo.plagubot.plugins.captcha.slotMachineReplyMarkup import dev.inmo.plagubot.plugins.captcha.slotMachineReplyMarkup
import dev.inmo.tgbotapi.extensions.api.answers.answerCallbackQuery import dev.inmo.tgbotapi.extensions.api.answers.answerCallbackQuery
import dev.inmo.tgbotapi.extensions.api.chat.members.* import dev.inmo.tgbotapi.extensions.api.chat.members.*
import dev.inmo.tgbotapi.extensions.api.deleteMessage import dev.inmo.tgbotapi.extensions.api.deleteMessage
import dev.inmo.tgbotapi.extensions.api.edit.ReplyMarkup.editMessageReplyMarkup import dev.inmo.tgbotapi.extensions.api.edit.reply_markup.editMessageReplyMarkup
import dev.inmo.tgbotapi.extensions.api.send.sendDice import dev.inmo.tgbotapi.extensions.api.send.sendDice
import dev.inmo.tgbotapi.extensions.api.send.sendTextMessage import dev.inmo.tgbotapi.extensions.api.send.sendTextMessage
import dev.inmo.tgbotapi.extensions.behaviour_builder.* import dev.inmo.tgbotapi.extensions.behaviour_builder.*
import dev.inmo.tgbotapi.extensions.behaviour_builder.expectations.waitDataCallbackQuery import dev.inmo.tgbotapi.extensions.behaviour_builder.expectations.waitDataCallbackQuery
import dev.inmo.tgbotapi.extensions.behaviour_builder.expectations.waitMessageDataCallbackQuery
import dev.inmo.tgbotapi.extensions.utils.asSlotMachineReelImage import dev.inmo.tgbotapi.extensions.utils.asSlotMachineReelImage
import dev.inmo.tgbotapi.extensions.utils.calculateSlotMachineResult import dev.inmo.tgbotapi.extensions.utils.calculateSlotMachineResult
import dev.inmo.tgbotapi.extensions.utils.extensions.raw.message
import dev.inmo.tgbotapi.extensions.utils.formatting.* import dev.inmo.tgbotapi.extensions.utils.formatting.*
import dev.inmo.tgbotapi.extensions.utils.shortcuts.executeUnsafe import dev.inmo.tgbotapi.extensions.utils.shortcuts.executeUnsafe
import dev.inmo.tgbotapi.extensions.utils.types.buttons.InlineKeyboardMarkup import dev.inmo.tgbotapi.extensions.utils.types.buttons.InlineKeyboardMarkup
@ -118,7 +119,9 @@ data class SlotMachineCaptchaProvider(
launch { launch {
val clicked = arrayOf<String?>(null, null, null) val clicked = arrayOf<String?>(null, null, null)
while (leftToClick.isNotEmpty()) { while (leftToClick.isNotEmpty()) {
val userClicked = waitDataCallbackQuery { if (user.id == it.id) this else null }.first() val userClicked = waitMessageDataCallbackQuery {
if (user.id == it.id && this.message.messageId == sentDice.messageId) this else null
}.first()
if (userClicked.data == leftToClick.first()) { if (userClicked.data == leftToClick.first()) {
clicked[3 - leftToClick.size] = leftToClick.removeAt(0) clicked[3 - leftToClick.size] = leftToClick.removeAt(0)
if (clicked.contains(null)) { if (clicked.contains(null)) {
@ -179,10 +182,9 @@ data class SimpleCaptchaProvider(
leftRestrictionsPermissions: ChatPermissions leftRestrictionsPermissions: ChatPermissions
) { ) {
val userBanDateTime = eventDateTime + checkTimeSpan val userBanDateTime = eventDateTime + checkTimeSpan
newUsers.mapNotNull { newUsers.map {
safelyWithoutExceptions { launchSafelyWithoutExceptions {
launch { createSubContext(this).doInContext(stopOnCompletion = false) {
createSubContextAndDoWithUpdatesFilter(stopOnCompletion = false) {
val callbackData = uuid4().toString() val callbackData = uuid4().toString()
val sentMessage = sendTextMessage( val sentMessage = sendTextMessage(
chat, chat,
@ -201,14 +203,12 @@ data class SimpleCaptchaProvider(
} }
} }
val job = parallel { val job = launchSafely {
waitDataCallbackQuery { waitMessageDataCallbackQuery (
if (it.id == user.id && this.data == callbackData) { filter = { query ->
this query.user.id == it.id && query.data == callbackData && query.message.messageId == sentMessage.messageId
} else {
null
} }
}.first() ).first()
removeRedundantMessages() removeRedundantMessages()
safelyWithoutExceptions { restrictChatMember(chat, it, permissions = leftRestrictionsPermissions) } safelyWithoutExceptions { restrictChatMember(chat, it, permissions = leftRestrictionsPermissions) }
@ -226,7 +226,6 @@ data class SimpleCaptchaProvider(
stop() stop()
} }
} }
}
}.joinAll() }.joinAll()
} }
} }
@ -354,21 +353,23 @@ data class ExpressionCaptchaProvider(
} }
var leftAttempts = attempts var leftAttempts = attempts
waitDataCallbackQuery { waitMessageDataCallbackQuery (
when { filter = { query ->
this.user.id != user.id -> null query.user.id == user.id && query.message.messageId == sentMessage.messageId
this.data != correctAnswer -> { }
) {
if (this.data != correctAnswer) {
leftAttempts-- leftAttempts--
if (leftAttempts < 1) { if (leftAttempts < 1) {
this this
} else { } else {
answerCallbackQuery(this@waitDataCallbackQuery, leftRetriesText + leftAttempts) answerCallbackQuery(this@waitMessageDataCallbackQuery, leftRetriesText + leftAttempts)
null null
} }
} else {
this
} }
else -> this }.take(1)
}
}.first()
banJob.cancel() banJob.cancel()