add support of disable/enable kick on unsuccess

This commit is contained in:
InsanusMokrassar 2022-07-31 00:17:46 +06:00
parent 58c6d12bdd
commit f1093d6944
3 changed files with 61 additions and 14 deletions

View File

@ -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)
}
}
}
}
}
}

View File

@ -43,7 +43,8 @@ sealed class CaptchaProvider {
chat: GroupChat,
newUsers: List<User>,
leftRestrictionsPermissions: ChatPermissions,
adminsApi: AdminsCacheAPI?
adminsApi: AdminsCacheAPI?,
kickOnUnsuccess: Boolean
)
}
@ -113,7 +114,8 @@ data class SlotMachineCaptchaProvider(
chat: GroupChat,
newUsers: List<User>,
leftRestrictionsPermissions: ChatPermissions,
adminsApi: AdminsCacheAPI?
adminsApi: AdminsCacheAPI?,
kickOnUnsuccess: Boolean
) {
val userBanDateTime = eventDateTime + checkTimeSpan
val authorized = Channel<User>(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<User>,
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<User>,
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)
}
}

View File

@ -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
)