From f1093d6944760127c70fe5edd6bc18fe4e4f3f72 Mon Sep 17 00:00:00 2001 From: InsanusMokrassar Date: Sun, 31 Jul 2022 00:17:46 +0600 Subject: [PATCH] add support of disable/enable kick on unsuccess --- .../inmo/plagubot/plugins/captcha/Plugin.kt | 50 +++++++++++++++++-- .../captcha/provider/CaptchaProvider.kt | 24 +++++---- .../plugins/captcha/settings/ChatSettings.kt | 1 + 3 files changed, 61 insertions(+), 14 deletions(-) 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 4f63511..7a371af 100644 --- a/src/main/kotlin/dev/inmo/plagubot/plugins/captcha/Plugin.kt +++ b/src/main/kotlin/dev/inmo/plagubot/plugins/captcha/Plugin.kt @@ -9,8 +9,8 @@ import dev.inmo.plagubot.plugins.captcha.provider.* import dev.inmo.plagubot.plugins.captcha.settings.ChatSettings import dev.inmo.plagubot.plugins.commands.BotCommandFullInfo import dev.inmo.plagubot.plugins.commands.CommandsKeeperKey -import dev.inmo.tgbotapi.extensions.api.chat.get.getChat import dev.inmo.tgbotapi.extensions.api.chat.members.* +import dev.inmo.tgbotapi.extensions.api.delete import dev.inmo.tgbotapi.extensions.api.deleteMessage import dev.inmo.tgbotapi.extensions.api.send.* import dev.inmo.tgbotapi.extensions.behaviour_builder.* @@ -151,10 +151,10 @@ class CaptchaBotPlugin : Plugin { permissions = RestrictionsChatPermissions ) } - val defaultChatPermissions = (getChat(it.chat) as ExtendedGroupChat).permissions + val defaultChatPermissions = LeftRestrictionsChatPermissions with (settings.captchaProvider) { - doAction(it.date, chat, newUsers, defaultChatPermissions, adminsAPI) + doAction(it.date, chat, newUsers, defaultChatPermissions, adminsAPI, settings.kickOnUnsuccess) } } @@ -289,6 +289,50 @@ class CaptchaBotPlugin : Plugin { } } } + + onCommand(enableKickOnUnsuccess) { message -> + message.doAfterVerification(adminsAPI) { + val settings = message.chat.settings() + + repo.update( + message.chat.id, + settings.copy(kickOnUnsuccess = true) + ) + + reply(message, "Ok, new users didn't passed captcha will be kicked").apply { + launchSafelyWithoutExceptions { + delay(5000L) + delete(this@apply) + } + } + + if (settings.autoRemoveCommands) { + deleteMessage(message) + } + } + } + + onCommand(disableKickOnUnsuccess) { message -> + message.doAfterVerification(adminsAPI) { + val settings = message.chat.settings() + + repo.update( + message.chat.id, + settings.copy(kickOnUnsuccess = false) + ) + + reply(message, "Ok, new users didn't passed captcha will NOT be kicked").apply { + launchSafelyWithoutExceptions { + delay(5000L) + delete(this@apply) + } + } + + if (settings.autoRemoveCommands) { + deleteMessage(message) + } + } + } } } } 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 7f077e7..4b549d6 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 @@ -43,7 +43,8 @@ sealed class CaptchaProvider { chat: GroupChat, newUsers: List, leftRestrictionsPermissions: ChatPermissions, - adminsApi: AdminsCacheAPI? + adminsApi: AdminsCacheAPI?, + kickOnUnsuccess: Boolean ) } @@ -113,7 +114,8 @@ data class SlotMachineCaptchaProvider( chat: GroupChat, newUsers: List, leftRestrictionsPermissions: ChatPermissions, - adminsApi: AdminsCacheAPI? + adminsApi: AdminsCacheAPI?, + kickOnUnsuccess: Boolean ) { val userBanDateTime = eventDateTime + checkTimeSpan val authorized = Channel(newUsers.size) @@ -175,7 +177,7 @@ data class SlotMachineCaptchaProvider( subContexts.forEach { (context, user) -> if (user !in authorizedUsers) { context.stop() - if (kick) { + if (kickOnUnsuccess) { banUser(chat, user, leftRestrictionsPermissions) } } @@ -191,8 +193,7 @@ data class SlotMachineCaptchaProvider( data class SimpleCaptchaProvider( val checkTimeSeconds: Seconds = 60, val captchaText: String = "press this button to pass captcha:", - val buttonText: String = "Press me\uD83D\uDE0A", - val kick: Boolean = true + val buttonText: String = "Press me\uD83D\uDE0A" ) : CaptchaProvider() { @Transient private val checkTimeSpan = checkTimeSeconds.seconds @@ -202,7 +203,8 @@ data class SimpleCaptchaProvider( chat: GroupChat, newUsers: List, leftRestrictionsPermissions: ChatPermissions, - adminsApi: AdminsCacheAPI? + adminsApi: AdminsCacheAPI?, + kickOnUnsuccess: Boolean ) { val userBanDateTime = eventDateTime + checkTimeSpan newUsers.map { user -> @@ -257,7 +259,7 @@ data class SimpleCaptchaProvider( if (job.isActive) { job.cancel() - if (kick) { + if (kickOnUnsuccess) { banUser(chat, user, leftRestrictionsPermissions) } } @@ -316,8 +318,7 @@ data class ExpressionCaptchaProvider( val maxPerNumber: Int = 10, val operations: Int = 2, val answers: Int = 6, - val attempts: Int = 3, - val kick: Boolean = true + val attempts: Int = 3 ) : CaptchaProvider() { @Transient private val checkTimeSpan = checkTimeSeconds.seconds @@ -327,7 +328,8 @@ data class ExpressionCaptchaProvider( chat: GroupChat, newUsers: List, leftRestrictionsPermissions: ChatPermissions, - adminsApi: AdminsCacheAPI? + adminsApi: AdminsCacheAPI?, + kickOnUnsuccess: Boolean ) { val userBanDateTime = eventDateTime + checkTimeSpan newUsers.map { user -> @@ -379,7 +381,7 @@ data class ExpressionCaptchaProvider( if (it) { safelyWithoutExceptions { restrictChatMember(chat, user, permissions = leftRestrictionsPermissions) } } else { - if (kick) { + if (kickOnUnsuccess) { banUser(chat, user, leftRestrictionsPermissions) } } diff --git a/src/main/kotlin/dev/inmo/plagubot/plugins/captcha/settings/ChatSettings.kt b/src/main/kotlin/dev/inmo/plagubot/plugins/captcha/settings/ChatSettings.kt index 9a9b1d0..94b5d62 100644 --- a/src/main/kotlin/dev/inmo/plagubot/plugins/captcha/settings/ChatSettings.kt +++ b/src/main/kotlin/dev/inmo/plagubot/plugins/captcha/settings/ChatSettings.kt @@ -11,5 +11,6 @@ data class ChatSettings( val captchaProvider: CaptchaProvider = SimpleCaptchaProvider(), val autoRemoveCommands: Boolean = false, val autoRemoveEvents: Boolean = true, + val kickOnUnsuccess: Boolean = true, val enabled: Boolean = true )