diff --git a/gradle/wrapper/gradle-wrapper.properties b/gradle/wrapper/gradle-wrapper.properties index 2a56324..e750102 100644 --- a/gradle/wrapper/gradle-wrapper.properties +++ b/gradle/wrapper/gradle-wrapper.properties @@ -1,5 +1,5 @@ distributionBase=GRADLE_USER_HOME distributionPath=wrapper/dists -distributionUrl=https\://services.gradle.org/distributions/gradle-6.8.2-bin.zip +distributionUrl=https\://services.gradle.org/distributions/gradle-7.3-bin.zip zipStoreBase=GRADLE_USER_HOME zipStorePath=wrapper/dists diff --git a/src/main/kotlin/dev/inmo/plagubot/plugins/captcha/Plugin.kt b/src/main/kotlin/dev/inmo/plagubot/plugins/captcha/Plugin.kt index 2b9ad84..bcd26b7 100644 --- a/src/main/kotlin/dev/inmo/plagubot/plugins/captcha/Plugin.kt +++ b/src/main/kotlin/dev/inmo/plagubot/plugins/captcha/Plugin.kt @@ -15,7 +15,6 @@ import dev.inmo.tgbotapi.extensions.api.chat.members.* import dev.inmo.tgbotapi.extensions.api.deleteMessage import dev.inmo.tgbotapi.extensions.api.edit.ReplyMarkup.editMessageReplyMarkup import dev.inmo.tgbotapi.extensions.api.send.* -import dev.inmo.tgbotapi.extensions.api.send.media.reply import dev.inmo.tgbotapi.extensions.behaviour_builder.* import dev.inmo.tgbotapi.extensions.behaviour_builder.expectations.waitBaseInlineQuery import dev.inmo.tgbotapi.extensions.behaviour_builder.expectations.waitDataCallbackQuery @@ -24,6 +23,7 @@ import dev.inmo.tgbotapi.updateshandlers.FlowsUpdatesFilter import dev.inmo.tgbotapi.extensions.behaviour_builder.triggers_handling.onNewChatMembers import dev.inmo.tgbotapi.extensions.utils.* import dev.inmo.tgbotapi.extensions.utils.extensions.parseCommandsWithParams +import dev.inmo.tgbotapi.extensions.utils.extensions.sourceChat import dev.inmo.tgbotapi.extensions.utils.formatting.buildEntities import dev.inmo.tgbotapi.extensions.utils.formatting.regular import dev.inmo.tgbotapi.extensions.utils.shortcuts.executeUnsafe @@ -90,10 +90,10 @@ class CaptchaBotPlugin : Plugin { suspend fun Chat.settings() = repo.getById(id) ?: repo.create(ChatSettings(id)).first() onNewChatMembers( - additionalFilter = { + initialFilter = { it.chat.asPublicChat() != null }, - includeFilterByChatInBehaviourSubContext = false + subcontextUpdatesFilter = { m, u -> u.sourceChat() == m.chat }, ) { launchSafelyWithoutExceptions { safelyWithoutExceptions { deleteMessage(it) } diff --git a/src/main/kotlin/dev/inmo/plagubot/plugins/captcha/provider/CaptchaProvider.kt b/src/main/kotlin/dev/inmo/plagubot/plugins/captcha/provider/CaptchaProvider.kt index b508bfa..a5012cd 100644 --- a/src/main/kotlin/dev/inmo/plagubot/plugins/captcha/provider/CaptchaProvider.kt +++ b/src/main/kotlin/dev/inmo/plagubot/plugins/captcha/provider/CaptchaProvider.kt @@ -3,11 +3,12 @@ package dev.inmo.plagubot.plugins.captcha.provider import com.benasher44.uuid.uuid4 import com.soywiz.klock.DateTime import com.soywiz.klock.seconds +import dev.inmo.micro_utils.common.joinTo +import dev.inmo.micro_utils.coroutines.safelyWithResult import dev.inmo.micro_utils.coroutines.safelyWithoutExceptions import dev.inmo.plagubot.plugins.captcha.slotMachineReplyMarkup import dev.inmo.tgbotapi.extensions.api.answers.answerCallbackQuery -import dev.inmo.tgbotapi.extensions.api.chat.members.kickChatMember -import dev.inmo.tgbotapi.extensions.api.chat.members.restrictChatMember +import dev.inmo.tgbotapi.extensions.api.chat.members.* import dev.inmo.tgbotapi.extensions.api.deleteMessage import dev.inmo.tgbotapi.extensions.api.edit.ReplyMarkup.editMessageReplyMarkup import dev.inmo.tgbotapi.extensions.api.send.sendDice @@ -20,12 +21,12 @@ import dev.inmo.tgbotapi.extensions.utils.formatting.* import dev.inmo.tgbotapi.extensions.utils.shortcuts.executeUnsafe import dev.inmo.tgbotapi.extensions.utils.types.buttons.InlineKeyboardMarkup import dev.inmo.tgbotapi.requests.DeleteMessage +import dev.inmo.tgbotapi.types.* import dev.inmo.tgbotapi.types.MessageEntity.textsources.mention -import dev.inmo.tgbotapi.types.Seconds -import dev.inmo.tgbotapi.types.User +import dev.inmo.tgbotapi.types.MessageEntity.textsources.plus import dev.inmo.tgbotapi.types.buttons.InlineKeyboardButtons.CallbackDataInlineKeyboardButton import dev.inmo.tgbotapi.types.chat.LeftRestrictionsChatPermissions -import dev.inmo.tgbotapi.types.chat.abstracts.GroupChat +import dev.inmo.tgbotapi.types.chat.abstracts.* import dev.inmo.tgbotapi.types.dice.SlotMachineDiceAnimationType import dev.inmo.tgbotapi.types.message.abstracts.Message import kotlinx.coroutines.* @@ -46,6 +47,33 @@ sealed class CaptchaProvider { ) } +private suspend fun BehaviourContext.banUser( + chat: PublicChat, + user: User, + onFailure: suspend BehaviourContext.(Throwable) -> Unit = { + safelyWithResult { + sendTextMessage( + chat, + buildEntities(" ") { + user.mention( + listOfNotNull( + user.lastName.takeIf { it.isNotBlank() }, user.firstName.takeIf { it.isNotBlank() } + ).takeIf { + it.isNotEmpty() + } ?.joinToString(" ") ?: "User" + ) + +"failed captcha" + } + ) + } + } +): Result = safelyWithResult { + restrictChatMember(chat, user, permissions = LeftRestrictionsChatPermissions) + banChatMember(chat, user) +}.onFailure { + onFailure(it) +} + @Serializable data class SlotMachineCaptchaProvider( val checkTimeSeconds: Seconds = 300, @@ -121,10 +149,7 @@ data class SlotMachineCaptchaProvider( if (user !in authorizedUsers) { context.stop() if (kick) { - safelyWithoutExceptions { - restrictChatMember(chat, user, permissions = LeftRestrictionsChatPermissions) - kickChatMember(chat, user) - } + banUser(chat, user) } } } @@ -192,10 +217,7 @@ data class SimpleCaptchaProvider( if (job.isActive) { job.cancel() if (kick) { - safelyWithoutExceptions { - restrictChatMember(chat, it, permissions = LeftRestrictionsChatPermissions) - kickChatMember(chat, it) - } + banUser(chat, it) } } stop() @@ -249,7 +271,7 @@ private object ExpressionBuilder { @Serializable data class ExpressionCaptchaProvider( val checkTimeSeconds: Seconds = 60, - val captchaText: String = "solve next captcha:", + val captchaText: String = "Solve next captcha:", val leftRetriesText: String = "Nope, left retries: ", val maxPerNumber: Int = 10, val operations: Int = 2, @@ -311,10 +333,7 @@ data class ExpressionCaptchaProvider( safelyWithoutExceptions { restrictChatMember(chat, user, permissions = LeftRestrictionsChatPermissions) } } else { if (kick) { - safelyWithoutExceptions { - restrictChatMember(chat, user, permissions = LeftRestrictionsChatPermissions) - kickChatMember(chat, user) - } + banUser(chat, user) } } }