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,35 +106,30 @@ 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@onNewChatMembers
if (!settings.enabled) return@launchSafelyWithoutExceptions
safelyWithoutExceptions { safelyWithoutExceptions {
if (settings.autoRemoveEvents) { if (settings.autoRemoveEvents) {
deleteMessage(it) deleteMessage(it)
}
} }
val chat = it.chat.requireGroupChat() }
val newUsers = it.chatEvent.members val chat = it.chat.requireGroupChat()
newUsers.forEach { user -> val newUsers = it.chatEvent.members
restrictChatMember( newUsers.forEach { user ->
chat, restrictChatMember(
user, chat,
permissions = RestrictionsChatPermissions user,
) permissions = RestrictionsChatPermissions
} )
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,52 +182,48 @@ 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, buildEntities {
buildEntities { +it.mention(it.firstName)
+it.mention(it.firstName) regular(", $captchaText")
regular(", $captchaText") },
}, replyMarkup = InlineKeyboardMarkup(
replyMarkup = InlineKeyboardMarkup( CallbackDataInlineKeyboardButton(buttonText, callbackData)
CallbackDataInlineKeyboardButton(buttonText, callbackData)
)
) )
)
suspend fun removeRedundantMessages() { suspend fun removeRedundantMessages() {
safelyWithoutExceptions { safelyWithoutExceptions {
deleteMessage(sentMessage) deleteMessage(sentMessage)
}
}
val job = launchSafely {
waitMessageDataCallbackQuery (
filter = { query ->
query.user.id == it.id && query.data == callbackData && query.message.messageId == sentMessage.messageId
} }
} ).first()
val job = parallel { removeRedundantMessages()
waitDataCallbackQuery { safelyWithoutExceptions { restrictChatMember(chat, it, permissions = leftRestrictionsPermissions) }
if (it.id == user.id && this.data == callbackData) {
this
} else {
null
}
}.first()
removeRedundantMessages()
safelyWithoutExceptions { restrictChatMember(chat, it, permissions = leftRestrictionsPermissions) }
stop()
}
delay((userBanDateTime - eventDateTime).millisecondsLong)
if (job.isActive) {
job.cancel()
if (kick) {
banUser(chat, it, leftRestrictionsPermissions)
}
}
stop() stop()
} }
delay((userBanDateTime - eventDateTime).millisecondsLong)
if (job.isActive) {
job.cancel()
if (kick) {
banUser(chat, it, leftRestrictionsPermissions)
}
}
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 -> {
leftAttempts--
if (leftAttempts < 1) {
this
} else {
answerCallbackQuery(this@waitDataCallbackQuery, leftRetriesText + leftAttempts)
null
}
}
else -> this
} }
}.first() ) {
if (this.data != correctAnswer) {
leftAttempts--
if (leftAttempts < 1) {
this
} else {
answerCallbackQuery(this@waitMessageDataCallbackQuery, leftRetriesText + leftAttempts)
null
}
} else {
this
}
}.take(1)
banJob.cancel() banJob.cancel()