This commit is contained in:
InsanusMokrassar 2021-03-31 00:02:18 +06:00
parent 1d4baa8be9
commit 4a7339afd9
2 changed files with 43 additions and 24 deletions

View File

@ -50,7 +50,7 @@ private const val enableSimpleCaptcha = "captcha_use_simple"
private const val enableExpressionCaptcha = "captcha_use_expression" private const val enableExpressionCaptcha = "captcha_use_expression"
private val changeCaptchaMethodCommandRegex = Regex( private val changeCaptchaMethodCommandRegex = Regex(
"captcha_use_(slot_machine)|(simple)|expression" "captcha_use_((slot_machine)|(simple)|(expression))"
) )
@Serializable @Serializable
@ -108,22 +108,24 @@ class CaptchaBotPlugin : Plugin {
if (adminsAPI != null) { if (adminsAPI != null) {
onCommand(changeCaptchaMethodCommandRegex) { onCommand(changeCaptchaMethodCommandRegex) {
val settings = it.chat.settings()
if (adminsAPI.sentByAdmin(it) != true) { if (adminsAPI.sentByAdmin(it) != true) {
return@onCommand return@onCommand
} }
val settings = it.chat.settings()
if (settings.autoRemoveCommands) { if (settings.autoRemoveCommands) {
safelyWithoutExceptions { deleteMessage(it) } safelyWithoutExceptions { deleteMessage(it) }
} }
val commands = it.parseCommandsWithParams() val commands = it.parseCommandsWithParams()
val changeCommand = commands.keys.first { val changeCommand = commands.keys.first {
println(it)
changeCaptchaMethodCommandRegex.matches(it) changeCaptchaMethodCommandRegex.matches(it)
} }
val captcha = when (changeCommand) { println(changeCommand)
enableSimpleCaptcha -> SimpleCaptchaProvider() val captcha = when {
enableExpressionCaptcha -> ExpressionCaptchaProvider() changeCommand.startsWith(enableSimpleCaptcha) -> SimpleCaptchaProvider()
enableSlotMachineCaptcha -> SlotMachineCaptchaProvider() changeCommand.startsWith(enableExpressionCaptcha) -> ExpressionCaptchaProvider()
changeCommand.startsWith(enableSlotMachineCaptcha) -> SlotMachineCaptchaProvider()
else -> return@onCommand else -> return@onCommand
} }
val newSettings = settings.copy(captchaProvider = captcha) val newSettings = settings.copy(captchaProvider = captcha)

View File

@ -33,7 +33,6 @@ import kotlinx.coroutines.channels.Channel
import kotlinx.coroutines.channels.toList import kotlinx.coroutines.channels.toList
import kotlinx.serialization.Serializable import kotlinx.serialization.Serializable
import kotlinx.serialization.Transient import kotlinx.serialization.Transient
import kotlin.math.abs
import kotlin.random.Random import kotlin.random.Random
@Serializable @Serializable
@ -230,7 +229,7 @@ private object ExpressionBuilder {
operations.forEach { operations.forEach {
val rightOne = createNumber(max) val rightOne = createNumber(max)
current = it.run { current.perform(rightOne) } current = it.run { current.perform(rightOne) }
numbersString = " ${it.asString()} $numbersString" numbersString += " ${it.asString()} $rightOne"
} }
return current to numbersString return current to numbersString
} }
@ -240,9 +239,11 @@ private object ExpressionBuilder {
data class ExpressionCaptchaProvider( data class ExpressionCaptchaProvider(
val checkTimeSeconds: Seconds = 60, 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 maxPerNumber: Int = 10,
val operations: Int = 2, val operations: Int = 2,
val answers: Int = 6, val answers: Int = 6,
val attempts: Int = 3,
val kick: Boolean = true val kick: Boolean = true
) : CaptchaProvider() { ) : CaptchaProvider() {
@Transient @Transient
@ -254,7 +255,7 @@ data class ExpressionCaptchaProvider(
newUsers: List<User> newUsers: List<User>
) { ) {
val userBanDateTime = eventDateTime + checkTimeSpan val userBanDateTime = eventDateTime + checkTimeSpan
newUsers.mapNotNull { newUsers.mapNotNull { user ->
safelyWithoutExceptions { safelyWithoutExceptions {
launch { launch {
doInSubContext { doInSubContext {
@ -272,7 +273,7 @@ data class ExpressionCaptchaProvider(
val sentMessage = sendTextMessage( val sentMessage = sendTextMessage(
chat, chat,
buildEntities { buildEntities {
+it.mention(it.firstName) +user.mention(user.firstName)
regular(", $captchaText ") regular(", $captchaText ")
bold(callbackData.second) bold(callbackData.second)
}, },
@ -289,28 +290,44 @@ data class ExpressionCaptchaProvider(
} }
} }
var passed = true
val callback: suspend (Boolean) -> Unit = {
removeRedundantMessages()
passed = it
if (it) {
safelyWithoutExceptions { restrictChatMember(chat, user, permissions = LeftRestrictionsChatPermissions) }
} else {
if (kick) {
safelyWithoutExceptions { kickChatMember(chat, user) }
}
}
}
val job = parallel { val job = parallel {
var leftAttempts = attempts
waitDataCallbackQuery { waitDataCallbackQuery {
if (it.id == user.id && this.data == correctAnswer) { when {
this this.user.id != user.id -> null
} else { this.data != correctAnswer -> {
null leftAttempts--
if (leftAttempts < 1) {
this
} else {
launch { answerCallbackQuery(this@waitDataCallbackQuery, leftRetriesText + leftAttempts) }
null
}
}
else -> this
} }
}.first() }.first()
removeRedundantMessages() callback(leftAttempts > 0)
safelyWithoutExceptions { restrictChatMember(chat, it, permissions = LeftRestrictionsChatPermissions) }
stop()
} }
delay((userBanDateTime - eventDateTime).millisecondsLong) delay((userBanDateTime - eventDateTime).millisecondsLong)
if (job.isActive) { if (job.isActive) job.cancel()
job.cancel() callback(passed)
if (kick) {
safelyWithoutExceptions { kickChatMember(chat, it) }
}
}
} }
} }
} }