Compare commits
6 Commits
2165f665ec
...
de5391b121
Author | SHA1 | Date |
---|---|---|
InsanusMokrassar | de5391b121 | |
InsanusMokrassar | 823c895e42 | |
InsanusMokrassar | de6a8241c3 | |
InsanusMokrassar | 7dead36cd9 | |
InsanusMokrassar | c84cff9ddc | |
InsanusMokrassar | 418d6b7f45 |
|
@ -7,11 +7,11 @@ kotlin.incremental=true
|
|||
kotlin_version=1.7.10
|
||||
kotlin_coroutines_version=1.6.4
|
||||
kotlin_serialisation_runtime_version=1.4.0
|
||||
plagubot_version=2.3.2
|
||||
plagubot_version=2.3.3
|
||||
|
||||
micro_utils_version=0.12.13
|
||||
tgbotapi_libraries_version=0.5.4
|
||||
commands_version=0.3.3
|
||||
commands_version=0.3.4
|
||||
|
||||
project_group=dev.inmo
|
||||
project_version=0.1.6
|
||||
|
|
|
@ -125,7 +125,7 @@ class CaptchaBotPlugin : Plugin {
|
|||
|
||||
override suspend fun BehaviourContext.setupBotPlugin(koin: Koin) {
|
||||
val repo: CaptchaChatsSettingsRepo by koin.inject()
|
||||
val adminsAPI = koin.get<AdminsCacheAPI>()
|
||||
val adminsAPI = koin.getOrNull<AdminsCacheAPI>()
|
||||
|
||||
suspend fun Chat.settings() = repo.getById(id) ?: repo.create(ChatSettings(id)).first()
|
||||
|
||||
|
@ -142,7 +142,7 @@ class CaptchaBotPlugin : Plugin {
|
|||
deleteMessage(it)
|
||||
}
|
||||
}
|
||||
val chat = it.chat.requireGroupChat()
|
||||
val chat = it.chat.groupChatOrThrow()
|
||||
val newUsers = it.chatEvent.members
|
||||
newUsers.forEach { user ->
|
||||
restrictChatMember(
|
||||
|
|
|
@ -1,33 +1,30 @@
|
|||
package dev.inmo.plagubot.plugins.captcha
|
||||
|
||||
import dev.inmo.plagubot.plugins.captcha.provider.cancelData
|
||||
import dev.inmo.tgbotapi.extensions.utils.SlotMachineReelImage
|
||||
import dev.inmo.tgbotapi.extensions.utils.types.buttons.*
|
||||
import dev.inmo.tgbotapi.libraries.cache.admins.AdminsCacheAPI
|
||||
import dev.inmo.tgbotapi.types.buttons.InlineKeyboardButtons.CallbackDataInlineKeyboardButton
|
||||
import dev.inmo.tgbotapi.types.buttons.InlineKeyboardButtons.InlineKeyboardButton
|
||||
import dev.inmo.tgbotapi.types.buttons.InlineKeyboardMarkup
|
||||
|
||||
infix fun String.startingOf(target: String) = target.startsWith(this)
|
||||
|
||||
fun slotMachineReplyMarkup(
|
||||
first: String? = null,
|
||||
second: String? = null,
|
||||
third: String? = null,
|
||||
): InlineKeyboardMarkup {
|
||||
val texts = when {
|
||||
first == null -> SlotMachineReelImage.values().map {
|
||||
CallbackDataInlineKeyboardButton("${it.text}**", it.text)
|
||||
}
|
||||
second == null -> SlotMachineReelImage.values().map {
|
||||
CallbackDataInlineKeyboardButton("$first${it.text}*", it.text)
|
||||
}
|
||||
third == null -> SlotMachineReelImage.values().map {
|
||||
CallbackDataInlineKeyboardButton("$first$second${it.text}", it.text)
|
||||
}
|
||||
else -> listOf(CallbackDataInlineKeyboardButton("$first$second$third", "$first$second$third"))
|
||||
}
|
||||
return inlineKeyboard {
|
||||
texts.chunked(2).forEach { add(it) }
|
||||
// row {
|
||||
// dataButton("Cancel (Admins only)", "cancel")
|
||||
// }
|
||||
private val buttonsPreset: List<List<InlineKeyboardButton>> = SlotMachineReelImage.values().toList().chunked(2).map {
|
||||
it.map {
|
||||
CallbackDataInlineKeyboardButton(it.text, it.text)
|
||||
}
|
||||
}
|
||||
|
||||
fun slotMachineReplyMarkup(
|
||||
adminCancelButton: Boolean = false
|
||||
): InlineKeyboardMarkup {
|
||||
return inlineKeyboard {
|
||||
buttonsPreset.forEach(::add)
|
||||
if (adminCancelButton) {
|
||||
row {
|
||||
dataButton("Cancel (Admins only)", cancelData)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -33,6 +33,7 @@ class CaptchaChatsSettingsRepo(
|
|||
private val autoRemoveCommandsColumn = bool("autoRemoveCommands")
|
||||
private val autoRemoveEventsColumn = bool("autoRemoveEvents").apply { default(true) }
|
||||
private val enabledColumn = bool("enabled").default(true)
|
||||
private val kickOnUnsuccessColumn = bool("kick").default(true)
|
||||
|
||||
override val primaryKey = PrimaryKey(chatIdColumn)
|
||||
|
||||
|
@ -46,6 +47,7 @@ class CaptchaChatsSettingsRepo(
|
|||
it[autoRemoveCommandsColumn] = value.autoRemoveCommands
|
||||
it[autoRemoveEventsColumn] = value.autoRemoveEvents
|
||||
it[enabledColumn] = value.enabled
|
||||
it[kickOnUnsuccessColumn] = value.kickOnUnsuccess
|
||||
}
|
||||
|
||||
override fun update(id: ChatId, value: ChatSettings, it: UpdateStatement) {
|
||||
|
@ -54,6 +56,7 @@ class CaptchaChatsSettingsRepo(
|
|||
it[autoRemoveCommandsColumn] = value.autoRemoveCommands
|
||||
it[autoRemoveEventsColumn] = value.autoRemoveEvents
|
||||
it[enabledColumn] = value.enabled
|
||||
it[kickOnUnsuccessColumn] = value.kickOnUnsuccess
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -62,7 +65,8 @@ class CaptchaChatsSettingsRepo(
|
|||
captchaProvider = captchaProviderSerialFormat.decodeFromString(CaptchaProvider.serializer(), get(captchaProviderColumn)),
|
||||
autoRemoveCommands = get(autoRemoveCommandsColumn),
|
||||
autoRemoveEvents = get(autoRemoveEventsColumn),
|
||||
enabled = get(enabledColumn)
|
||||
enabled = get(enabledColumn),
|
||||
kickOnUnsuccess = get(kickOnUnsuccessColumn)
|
||||
)
|
||||
|
||||
override val selectById: SqlExpressionBuilder.(ChatId) -> Op<Boolean> = { chatIdColumn.eq(it.chatId) }
|
||||
|
@ -72,7 +76,8 @@ class CaptchaChatsSettingsRepo(
|
|||
captchaProvider = captchaProviderSerialFormat.decodeFromString(CaptchaProvider.serializer(), get(captchaProviderColumn)),
|
||||
autoRemoveCommands = get(autoRemoveCommandsColumn),
|
||||
autoRemoveEvents = get(autoRemoveEventsColumn),
|
||||
enabled = get(enabledColumn)
|
||||
enabled = get(enabledColumn),
|
||||
kickOnUnsuccess = get(kickOnUnsuccessColumn)
|
||||
)
|
||||
|
||||
init {
|
||||
|
|
|
@ -1,21 +1,26 @@
|
|||
package dev.inmo.plagubot.plugins.captcha.provider
|
||||
|
||||
import com.benasher44.uuid.uuid4
|
||||
import com.soywiz.klock.DateTime
|
||||
import com.soywiz.klock.seconds
|
||||
import com.soywiz.klock.*
|
||||
import dev.inmo.kslog.common.e
|
||||
import dev.inmo.kslog.common.logger
|
||||
import dev.inmo.micro_utils.coroutines.*
|
||||
import dev.inmo.plagubot.plugins.captcha.slotMachineReplyMarkup
|
||||
import dev.inmo.tgbotapi.extensions.api.answers.answer
|
||||
import dev.inmo.tgbotapi.extensions.api.answers.answerCallbackQuery
|
||||
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.edit.edit
|
||||
import dev.inmo.tgbotapi.extensions.api.edit.reply_markup.editMessageReplyMarkup
|
||||
import dev.inmo.tgbotapi.extensions.api.send.*
|
||||
import dev.inmo.tgbotapi.extensions.behaviour_builder.*
|
||||
import dev.inmo.tgbotapi.extensions.behaviour_builder.expectations.waitMessageDataCallbackQuery
|
||||
import dev.inmo.tgbotapi.extensions.utils.asSlotMachineReelImage
|
||||
import dev.inmo.tgbotapi.extensions.utils.calculateSlotMachineResult
|
||||
import dev.inmo.tgbotapi.extensions.utils.formatting.*
|
||||
import dev.inmo.tgbotapi.extensions.utils.extensions.sameMessage
|
||||
import dev.inmo.tgbotapi.extensions.utils.shortcuts.executeUnsafe
|
||||
import dev.inmo.tgbotapi.extensions.utils.shortcuts.sentMessages
|
||||
import dev.inmo.tgbotapi.extensions.utils.types.buttons.*
|
||||
import dev.inmo.tgbotapi.libraries.cache.admins.AdminsCacheAPI
|
||||
import dev.inmo.tgbotapi.requests.DeleteMessage
|
||||
|
@ -26,6 +31,10 @@ import dev.inmo.tgbotapi.types.chat.*
|
|||
import dev.inmo.tgbotapi.types.chat.User
|
||||
import dev.inmo.tgbotapi.types.dice.SlotMachineDiceAnimationType
|
||||
import dev.inmo.tgbotapi.types.message.abstracts.Message
|
||||
import dev.inmo.tgbotapi.utils.*
|
||||
import dev.inmo.tgbotapi.utils.EntitiesBuilder
|
||||
import dev.inmo.tgbotapi.utils.bold
|
||||
import dev.inmo.tgbotapi.utils.regular
|
||||
import kotlinx.coroutines.*
|
||||
import kotlinx.coroutines.channels.Channel
|
||||
import kotlinx.coroutines.channels.toList
|
||||
|
@ -38,17 +47,100 @@ import kotlin.random.Random
|
|||
|
||||
@Serializable
|
||||
sealed class CaptchaProvider {
|
||||
abstract suspend fun BehaviourContext.doAction(
|
||||
abstract val checkTimeSpan: TimeSpan
|
||||
|
||||
interface CaptchaProviderWorker {
|
||||
suspend fun BehaviourContext.doCaptcha(): Boolean
|
||||
|
||||
suspend fun BehaviourContext.onCloseCaptcha(passed: Boolean)
|
||||
}
|
||||
|
||||
protected abstract suspend fun allocateWorker(
|
||||
eventDateTime: DateTime,
|
||||
chat: GroupChat,
|
||||
user: User,
|
||||
leftRestrictionsPermissions: ChatPermissions,
|
||||
adminsApi: AdminsCacheAPI?,
|
||||
kickOnUnsuccess: Boolean
|
||||
): CaptchaProviderWorker
|
||||
|
||||
suspend fun BehaviourContext.doAction(
|
||||
eventDateTime: DateTime,
|
||||
chat: GroupChat,
|
||||
newUsers: List<User>,
|
||||
leftRestrictionsPermissions: ChatPermissions,
|
||||
adminsApi: AdminsCacheAPI?,
|
||||
kickOnUnsuccess: Boolean
|
||||
)
|
||||
) {
|
||||
val userBanDateTime = eventDateTime + checkTimeSpan
|
||||
newUsers.map { user ->
|
||||
launch {
|
||||
createSubContextAndDoWithUpdatesFilter {
|
||||
val worker = allocateWorker(
|
||||
eventDateTime,
|
||||
chat,
|
||||
user,
|
||||
leftRestrictionsPermissions,
|
||||
adminsApi,
|
||||
kickOnUnsuccess
|
||||
)
|
||||
val deferred = async {
|
||||
runCatchingSafely {
|
||||
with(worker) {
|
||||
doCaptcha()
|
||||
}
|
||||
}.onFailure {
|
||||
this@CaptchaProvider.logger.e("Unable to do captcha", it)
|
||||
}.getOrElse { false }
|
||||
}
|
||||
|
||||
val subscope = LinkedSupervisorScope()
|
||||
subscope.launch {
|
||||
delay((userBanDateTime - eventDateTime).millisecondsLong)
|
||||
subscope.cancel()
|
||||
}
|
||||
subscope.launch {
|
||||
deferred.await()
|
||||
subscope.cancel()
|
||||
}
|
||||
|
||||
subscope.coroutineContext.job.join()
|
||||
|
||||
val passed = runCatching {
|
||||
deferred.getCompleted()
|
||||
}.onFailure {
|
||||
deferred.cancel()
|
||||
}.getOrElse { false }
|
||||
|
||||
when {
|
||||
passed -> {
|
||||
safelyWithoutExceptions {
|
||||
restrictChatMember(
|
||||
chat,
|
||||
user,
|
||||
permissions = leftRestrictionsPermissions
|
||||
)
|
||||
}
|
||||
}
|
||||
else -> {
|
||||
send(chat, " ") {
|
||||
+"User" + mention(user) + underline("didn't passed") + "captcha"
|
||||
}
|
||||
if (kickOnUnsuccess) {
|
||||
banUser(chat, user, leftRestrictionsPermissions)
|
||||
}
|
||||
}
|
||||
}
|
||||
with(worker) {
|
||||
onCloseCaptcha(passed)
|
||||
}
|
||||
}
|
||||
}
|
||||
}.joinAll()
|
||||
}
|
||||
}
|
||||
|
||||
private const val cancelData = "cancel"
|
||||
internal const val cancelData = "cancel"
|
||||
|
||||
private fun EntitiesBuilder.mention(user: User, defaultName: String = "User"): EntitiesBuilder {
|
||||
return mention(
|
||||
|
@ -56,7 +148,7 @@ private fun EntitiesBuilder.mention(user: User, defaultName: String = "User"): E
|
|||
user.lastName.takeIf { it.isNotBlank() }, user.firstName.takeIf { it.isNotBlank() }
|
||||
).takeIf {
|
||||
it.isNotEmpty()
|
||||
} ?.joinToString(" ") ?: defaultName,
|
||||
}?.joinToString(" ") ?: defaultName,
|
||||
user
|
||||
)
|
||||
}
|
||||
|
@ -67,14 +159,13 @@ private suspend fun BehaviourContext.sendAdminCanceledMessage(
|
|||
admin: User
|
||||
) {
|
||||
safelyWithoutExceptions {
|
||||
sendTextMessage(
|
||||
chat,
|
||||
buildEntities {
|
||||
mention(admin, "Admin")
|
||||
regular(" cancelled captcha for ")
|
||||
mention(captchaSolver)
|
||||
}
|
||||
)
|
||||
send(
|
||||
chat
|
||||
) {
|
||||
mention(admin, "Admin")
|
||||
regular(" cancelled captcha for ")
|
||||
mention(captchaSolver)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -84,13 +175,12 @@ private suspend fun BehaviourContext.banUser(
|
|||
leftRestrictionsPermissions: ChatPermissions,
|
||||
onFailure: suspend BehaviourContext.(Throwable) -> Unit = {
|
||||
safelyWithResult {
|
||||
sendTextMessage(
|
||||
chat,
|
||||
buildEntities(" ") {
|
||||
mention(user)
|
||||
+"failed captcha"
|
||||
}
|
||||
)
|
||||
send(
|
||||
chat
|
||||
) {
|
||||
mention(user)
|
||||
+"failed captcha"
|
||||
}
|
||||
}
|
||||
}
|
||||
): Result<Boolean> = safelyWithResult {
|
||||
|
@ -102,93 +192,95 @@ private suspend fun BehaviourContext.banUser(
|
|||
|
||||
@Serializable
|
||||
data class SlotMachineCaptchaProvider(
|
||||
val checkTimeSeconds: Seconds = 300,
|
||||
val captchaText: String = "solve this captcha: ",
|
||||
val kick: Boolean = true
|
||||
val checkTimeSeconds: Seconds = 60,
|
||||
val captchaText: String = "Solve this captcha: "
|
||||
) : CaptchaProvider() {
|
||||
@Transient
|
||||
private val checkTimeSpan = checkTimeSeconds.seconds
|
||||
override val checkTimeSpan = checkTimeSeconds.seconds
|
||||
|
||||
override suspend fun BehaviourContext.doAction(
|
||||
private inner class Worker(
|
||||
private val chat: GroupChat,
|
||||
private val user: User,
|
||||
private val adminsApi: AdminsCacheAPI?
|
||||
) : CaptchaProviderWorker {
|
||||
private val messagesToDelete = mutableListOf<Message>()
|
||||
|
||||
override suspend fun BehaviourContext.doCaptcha(): Boolean {
|
||||
val baseBuilder: EntitiesBuilderBody = {
|
||||
mention(user)
|
||||
regular(", $captchaText")
|
||||
}
|
||||
val sentMessage = send(
|
||||
chat
|
||||
) {
|
||||
baseBuilder()
|
||||
+": ✖✖✖"
|
||||
}.also { messagesToDelete.add(it) }
|
||||
val sentDice = sendDice(
|
||||
sentMessage.chat,
|
||||
SlotMachineDiceAnimationType,
|
||||
replyToMessageId = sentMessage.messageId,
|
||||
replyMarkup = slotMachineReplyMarkup(adminsApi != null)
|
||||
).also { messagesToDelete.add(it) }
|
||||
val reels = sentDice.content.dice.calculateSlotMachineResult()!!
|
||||
val leftToClick = mutableListOf(
|
||||
reels.left.asSlotMachineReelImage.text,
|
||||
reels.center.asSlotMachineReelImage.text,
|
||||
reels.right.asSlotMachineReelImage.text
|
||||
)
|
||||
val clicked = mutableListOf<String>()
|
||||
fun buildTemplate() = "${clicked.joinToString("")}${leftToClick.joinToString("") { "✖" }}"
|
||||
|
||||
waitMessageDataCallbackQuery().filter {
|
||||
when {
|
||||
!it.message.sameMessage(sentDice) -> false
|
||||
it.data == cancelData && adminsApi ?.isAdmin(chat.id, it.user.id) == true -> return@filter true
|
||||
it.data == cancelData && adminsApi ?.isAdmin(chat.id, it.user.id) != true -> {
|
||||
answer(
|
||||
it,
|
||||
"This button is only for admins"
|
||||
)
|
||||
false
|
||||
}
|
||||
it.user.id != user.id -> {
|
||||
answer(it, "This button is not for you")
|
||||
false
|
||||
}
|
||||
it.data != leftToClick.first() -> {
|
||||
answer(it, "Nope")
|
||||
false
|
||||
}
|
||||
else -> {
|
||||
clicked.add(leftToClick.removeFirst())
|
||||
answer(it, "Ok, next one")
|
||||
edit(sentMessage) {
|
||||
baseBuilder()
|
||||
+": ${buildTemplate()}"
|
||||
}
|
||||
leftToClick.isEmpty()
|
||||
}
|
||||
}
|
||||
}.first()
|
||||
|
||||
return true
|
||||
}
|
||||
|
||||
override suspend fun BehaviourContext.onCloseCaptcha(passed: Boolean) {
|
||||
while (messagesToDelete.isNotEmpty()) {
|
||||
runCatchingSafely { delete(messagesToDelete.removeFirst()) }
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
override suspend fun allocateWorker(
|
||||
eventDateTime: DateTime,
|
||||
chat: GroupChat,
|
||||
newUsers: List<User>,
|
||||
user: User,
|
||||
leftRestrictionsPermissions: ChatPermissions,
|
||||
adminsApi: AdminsCacheAPI?,
|
||||
kickOnUnsuccess: Boolean
|
||||
) {
|
||||
val userBanDateTime = eventDateTime + checkTimeSpan
|
||||
val authorized = Channel<User>(newUsers.size)
|
||||
val messagesToDelete = Channel<Message>(Channel.UNLIMITED)
|
||||
val subContexts = newUsers.map { user ->
|
||||
createSubContextAndDoWithUpdatesFilter (stopOnCompletion = false) {
|
||||
val sentMessage = sendTextMessage(
|
||||
chat,
|
||||
buildEntities {
|
||||
mention(user)
|
||||
regular(", $captchaText")
|
||||
}
|
||||
).also { messagesToDelete.send(it) }
|
||||
val sentDice = sendDice(
|
||||
sentMessage.chat,
|
||||
SlotMachineDiceAnimationType,
|
||||
replyToMessageId = sentMessage.messageId,
|
||||
replyMarkup = slotMachineReplyMarkup()
|
||||
).also { messagesToDelete.send(it) }
|
||||
val reels = sentDice.content.dice.calculateSlotMachineResult()!!
|
||||
val leftToClick = mutableListOf(
|
||||
reels.left.asSlotMachineReelImage.text,
|
||||
reels.center.asSlotMachineReelImage.text,
|
||||
reels.right.asSlotMachineReelImage.text
|
||||
)
|
||||
|
||||
launch {
|
||||
val clicked = arrayOf<String?>(null, null, null)
|
||||
while (leftToClick.isNotEmpty()) {
|
||||
val userClicked = waitMessageDataCallbackQuery().filter { it.user.id == user.id && it.message.messageId == sentDice.messageId }.first()
|
||||
|
||||
when {
|
||||
userClicked.data == leftToClick.first() -> {
|
||||
clicked[3 - leftToClick.size] = leftToClick.removeAt(0)
|
||||
if (clicked.contains(null)) {
|
||||
safelyWithoutExceptions { answerCallbackQuery(userClicked, "Ok, next one") }
|
||||
editMessageReplyMarkup(sentDice, slotMachineReplyMarkup(clicked[0], clicked[1], clicked[2]))
|
||||
} else {
|
||||
safelyWithoutExceptions { answerCallbackQuery(userClicked, "Thank you and welcome", showAlert = true) }
|
||||
safelyWithoutExceptions { deleteMessage(sentMessage) }
|
||||
safelyWithoutExceptions { deleteMessage(sentDice) }
|
||||
}
|
||||
}
|
||||
else -> safelyWithoutExceptions { answerCallbackQuery(userClicked, "Nope") }
|
||||
}
|
||||
}
|
||||
authorized.send(user)
|
||||
safelyWithoutExceptions { restrictChatMember(chat, user, permissions = leftRestrictionsPermissions) }
|
||||
stop()
|
||||
}
|
||||
|
||||
this to user
|
||||
}
|
||||
}
|
||||
|
||||
delay((userBanDateTime - eventDateTime).millisecondsLong)
|
||||
|
||||
authorized.close()
|
||||
val authorizedUsers = authorized.toList()
|
||||
|
||||
subContexts.forEach { (context, user) ->
|
||||
if (user !in authorizedUsers) {
|
||||
context.stop()
|
||||
if (kickOnUnsuccess) {
|
||||
banUser(chat, user, leftRestrictionsPermissions)
|
||||
}
|
||||
}
|
||||
}
|
||||
messagesToDelete.close()
|
||||
for (message in messagesToDelete) {
|
||||
executeUnsafe(DeleteMessage(message.chat.id, message.messageId), retries = 0)
|
||||
}
|
||||
}
|
||||
): CaptchaProviderWorker = Worker(chat, user, adminsApi)
|
||||
}
|
||||
|
||||
@Serializable
|
||||
|
@ -198,78 +290,78 @@ data class SimpleCaptchaProvider(
|
|||
val buttonText: String = "Press me\uD83D\uDE0A"
|
||||
) : CaptchaProvider() {
|
||||
@Transient
|
||||
private val checkTimeSpan = checkTimeSeconds.seconds
|
||||
override val checkTimeSpan = checkTimeSeconds.seconds
|
||||
|
||||
override suspend fun BehaviourContext.doAction(
|
||||
private inner class Worker(
|
||||
private val chat: GroupChat,
|
||||
private val user: User,
|
||||
private val adminsApi: AdminsCacheAPI?
|
||||
) : CaptchaProviderWorker {
|
||||
private var sentMessage: Message? = null
|
||||
override suspend fun BehaviourContext.doCaptcha(): Boolean {
|
||||
val callbackData = uuid4().toString()
|
||||
val sentMessage = send(
|
||||
chat,
|
||||
replyMarkup = inlineKeyboard {
|
||||
row {
|
||||
dataButton(buttonText, callbackData)
|
||||
}
|
||||
if (adminsApi != null) {
|
||||
row {
|
||||
dataButton("Cancel (Admins only)", cancelData)
|
||||
}
|
||||
}
|
||||
}
|
||||
) {
|
||||
mention(user)
|
||||
regular(", $captchaText")
|
||||
}
|
||||
this@Worker.sentMessage = sentMessage
|
||||
|
||||
val pushed = waitMessageDataCallbackQuery().filter {
|
||||
when {
|
||||
!it.message.sameMessage(sentMessage) -> false
|
||||
it.data == callbackData && it.user.id == user.id -> true
|
||||
it.data == cancelData && (adminsApi ?.isAdmin(chat.id, it.user.id) == true) -> true
|
||||
it.data == callbackData -> {
|
||||
answer(it, "This button is not for you")
|
||||
false
|
||||
}
|
||||
it.data == cancelData -> {
|
||||
answer(it, "This button is for admins only")
|
||||
false
|
||||
}
|
||||
else -> false
|
||||
}
|
||||
}.first()
|
||||
|
||||
answer(
|
||||
pushed,
|
||||
when (pushed.data) {
|
||||
cancelData -> "You have cancelled captcha"
|
||||
else -> "Ok, thanks"
|
||||
}
|
||||
)
|
||||
|
||||
return true
|
||||
}
|
||||
|
||||
override suspend fun BehaviourContext.onCloseCaptcha(passed: Boolean) {
|
||||
sentMessage ?.let {
|
||||
delete(it)
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
override suspend fun allocateWorker(
|
||||
eventDateTime: DateTime,
|
||||
chat: GroupChat,
|
||||
newUsers: List<User>,
|
||||
user: User,
|
||||
leftRestrictionsPermissions: ChatPermissions,
|
||||
adminsApi: AdminsCacheAPI?,
|
||||
kickOnUnsuccess: Boolean
|
||||
) {
|
||||
val userBanDateTime = eventDateTime + checkTimeSpan
|
||||
newUsers.map { user ->
|
||||
launchSafelyWithoutExceptions {
|
||||
createSubContext(this).doInContext(stopOnCompletion = false) {
|
||||
val callbackData = uuid4().toString()
|
||||
val sentMessage = sendTextMessage(
|
||||
chat,
|
||||
buildEntities {
|
||||
mention(user)
|
||||
regular(", $captchaText")
|
||||
},
|
||||
replyMarkup = inlineKeyboard {
|
||||
row {
|
||||
dataButton(buttonText, callbackData)
|
||||
}
|
||||
if (adminsApi != null) {
|
||||
row {
|
||||
dataButton("Cancel (Admins only)", cancelData)
|
||||
}
|
||||
}
|
||||
}
|
||||
)
|
||||
|
||||
suspend fun removeRedundantMessages() {
|
||||
safelyWithoutExceptions {
|
||||
deleteMessage(sentMessage)
|
||||
}
|
||||
}
|
||||
|
||||
val job = launchSafely {
|
||||
waitMessageDataCallbackQuery().filter { query ->
|
||||
val baseCheck = query.message.messageId == sentMessage.messageId
|
||||
val userAnswered = query.user.id == user.id && query.data == callbackData
|
||||
val adminCanceled = (query.data == cancelData && (adminsApi ?.isAdmin(sentMessage.chat.id, query.user.id)) == true)
|
||||
if (baseCheck && adminCanceled) {
|
||||
sendAdminCanceledMessage(
|
||||
sentMessage.chat,
|
||||
user,
|
||||
query.user
|
||||
)
|
||||
}
|
||||
baseCheck && (adminCanceled || userAnswered)
|
||||
}.first()
|
||||
|
||||
removeRedundantMessages()
|
||||
safelyWithoutExceptions { restrictChatMember(chat, user, permissions = leftRestrictionsPermissions) }
|
||||
stop()
|
||||
}
|
||||
|
||||
delay((userBanDateTime - eventDateTime).millisecondsLong)
|
||||
|
||||
if (job.isActive) {
|
||||
job.cancel()
|
||||
if (kickOnUnsuccess) {
|
||||
banUser(chat, user, leftRestrictionsPermissions)
|
||||
}
|
||||
}
|
||||
stop()
|
||||
}
|
||||
}
|
||||
}.joinAll()
|
||||
}
|
||||
): CaptchaProviderWorker = Worker(chat, user, adminsApi)
|
||||
}
|
||||
|
||||
private object ExpressionBuilder {
|
||||
|
@ -279,15 +371,19 @@ private object ExpressionBuilder {
|
|||
|
||||
override fun Int.perform(other: Int): Int = plus(other)
|
||||
}
|
||||
|
||||
object MinusExpressionOperation : ExpressionOperation() {
|
||||
override fun asString(): String = "-"
|
||||
|
||||
override fun Int.perform(other: Int): Int = minus(other)
|
||||
}
|
||||
|
||||
abstract fun asString(): String
|
||||
abstract fun Int.perform(other: Int): Int
|
||||
}
|
||||
private val experssions = listOf(ExpressionOperation.PlusExpressionOperation, ExpressionOperation.MinusExpressionOperation)
|
||||
|
||||
private val experssions =
|
||||
listOf(ExpressionOperation.PlusExpressionOperation, ExpressionOperation.MinusExpressionOperation)
|
||||
|
||||
private fun createNumber(max: Int) = Random.nextInt(max + 1)
|
||||
fun generateResult(max: Int, operationsNumber: Int = 1): Int {
|
||||
|
@ -299,6 +395,7 @@ private object ExpressionBuilder {
|
|||
}
|
||||
return current
|
||||
}
|
||||
|
||||
fun createExpression(max: Int, operationsNumber: Int = 1): Pair<Int, String> {
|
||||
val operations = (0 until operationsNumber).map { experssions.random() }
|
||||
var current = createNumber(max)
|
||||
|
@ -323,108 +420,87 @@ data class ExpressionCaptchaProvider(
|
|||
val attempts: Int = 3
|
||||
) : CaptchaProvider() {
|
||||
@Transient
|
||||
private val checkTimeSpan = checkTimeSeconds.seconds
|
||||
override val checkTimeSpan = checkTimeSeconds.seconds
|
||||
|
||||
override suspend fun BehaviourContext.doAction(
|
||||
private inner class Worker(
|
||||
private val chat: GroupChat,
|
||||
private val user: User,
|
||||
private val adminsApi: AdminsCacheAPI?
|
||||
) : CaptchaProviderWorker {
|
||||
private var sentMessage: Message? = null
|
||||
override suspend fun BehaviourContext.doCaptcha(): Boolean {
|
||||
val callbackData = ExpressionBuilder.createExpression(
|
||||
maxPerNumber,
|
||||
operations
|
||||
)
|
||||
val correctAnswer = callbackData.first.toString()
|
||||
val answers = (0 until answers - 1).map {
|
||||
ExpressionBuilder.generateResult(maxPerNumber, operations)
|
||||
}.toMutableList().also { orderedAnswers ->
|
||||
val correctAnswerPosition = Random.nextInt(orderedAnswers.size)
|
||||
orderedAnswers.add(correctAnswerPosition, callbackData.first)
|
||||
}.toList()
|
||||
val sentMessage = send(
|
||||
chat,
|
||||
replyMarkup = inlineKeyboard {
|
||||
answers.map {
|
||||
CallbackDataInlineKeyboardButton(it.toString(), it.toString())
|
||||
}.chunked(3).forEach(::add)
|
||||
if (adminsApi != null) {
|
||||
row {
|
||||
dataButton("Cancel (Admins only)", cancelData)
|
||||
}
|
||||
}
|
||||
}
|
||||
) {
|
||||
mention(user)
|
||||
regular(", $captchaText ")
|
||||
bold(callbackData.second)
|
||||
}.also {
|
||||
sentMessage = it
|
||||
}
|
||||
|
||||
var leftAttempts = attempts
|
||||
return waitMessageDataCallbackQuery().takeWhile { leftAttempts > 0 }.map { query ->
|
||||
val baseCheck = query.message.messageId == sentMessage.messageId
|
||||
val dataCorrect = (query.user.id == user.id && query.data == correctAnswer)
|
||||
val adminCanceled = (query.data == cancelData && (adminsApi?.isAdmin(
|
||||
sentMessage.chat.id,
|
||||
query.user.id
|
||||
)) == true)
|
||||
baseCheck && if (dataCorrect || adminCanceled) {
|
||||
if (adminCanceled) {
|
||||
sendAdminCanceledMessage(
|
||||
sentMessage.chat,
|
||||
user,
|
||||
query.user
|
||||
)
|
||||
}
|
||||
true
|
||||
} else {
|
||||
leftAttempts--
|
||||
if (leftAttempts > 0) {
|
||||
answerCallbackQuery(query, leftRetriesText + leftAttempts)
|
||||
}
|
||||
false
|
||||
}
|
||||
}.firstOrNull() ?: false
|
||||
}
|
||||
|
||||
override suspend fun BehaviourContext.onCloseCaptcha(passed: Boolean) {
|
||||
sentMessage ?.let {
|
||||
delete(it)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
override suspend fun allocateWorker(
|
||||
eventDateTime: DateTime,
|
||||
chat: GroupChat,
|
||||
newUsers: List<User>,
|
||||
user: User,
|
||||
leftRestrictionsPermissions: ChatPermissions,
|
||||
adminsApi: AdminsCacheAPI?,
|
||||
kickOnUnsuccess: Boolean
|
||||
) {
|
||||
val userBanDateTime = eventDateTime + checkTimeSpan
|
||||
newUsers.map { user ->
|
||||
launch {
|
||||
createSubContextAndDoWithUpdatesFilter {
|
||||
val callbackData = ExpressionBuilder.createExpression(
|
||||
maxPerNumber,
|
||||
operations
|
||||
)
|
||||
val correctAnswer = callbackData.first.toString()
|
||||
val answers = (0 until answers - 1).map {
|
||||
ExpressionBuilder.generateResult(maxPerNumber, operations)
|
||||
}.toMutableList().also { orderedAnswers ->
|
||||
val correctAnswerPosition = Random.nextInt(orderedAnswers.size)
|
||||
orderedAnswers.add(correctAnswerPosition, callbackData.first)
|
||||
}.toList()
|
||||
val sentMessage = sendTextMessage(
|
||||
chat,
|
||||
buildEntities {
|
||||
mention(user)
|
||||
regular(", $captchaText ")
|
||||
bold(callbackData.second)
|
||||
},
|
||||
replyMarkup = inlineKeyboard {
|
||||
answers.map {
|
||||
CallbackDataInlineKeyboardButton(it.toString(), it.toString())
|
||||
}.chunked(3).forEach(::add)
|
||||
if (adminsApi != null) {
|
||||
row {
|
||||
dataButton("Cancel (Admins only)", cancelData)
|
||||
}
|
||||
}
|
||||
}
|
||||
)
|
||||
|
||||
suspend fun removeRedundantMessages() {
|
||||
safelyWithoutExceptions {
|
||||
deleteMessage(sentMessage)
|
||||
}
|
||||
}
|
||||
|
||||
var passed: Boolean? = null
|
||||
val passedMutex = Mutex()
|
||||
val callback: suspend (Boolean) -> Unit = {
|
||||
passedMutex.withLock {
|
||||
if (passed == null) {
|
||||
removeRedundantMessages()
|
||||
passed = it
|
||||
when {
|
||||
it -> safelyWithoutExceptions { restrictChatMember(chat, user, permissions = leftRestrictionsPermissions) }
|
||||
kickOnUnsuccess -> banUser(chat, user, leftRestrictionsPermissions)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
val banJob = launch {
|
||||
delay((userBanDateTime - eventDateTime).millisecondsLong)
|
||||
|
||||
if (passed == null) {
|
||||
callback(false)
|
||||
stop()
|
||||
}
|
||||
}
|
||||
|
||||
var leftAttempts = attempts
|
||||
waitMessageDataCallbackQuery().takeWhile { leftAttempts > 0 }.filter { query ->
|
||||
val baseCheck = query.message.messageId == sentMessage.messageId
|
||||
val dataCorrect = (query.user.id == user.id && query.data == correctAnswer)
|
||||
val adminCanceled = (query.data == cancelData && (adminsApi ?.isAdmin(sentMessage.chat.id, query.user.id)) == true)
|
||||
baseCheck && if (dataCorrect || adminCanceled) {
|
||||
banJob.cancel()
|
||||
if (adminCanceled) {
|
||||
sendAdminCanceledMessage(
|
||||
sentMessage.chat,
|
||||
user,
|
||||
query.user
|
||||
)
|
||||
}
|
||||
true
|
||||
} else {
|
||||
leftAttempts--
|
||||
if (leftAttempts > 0) {
|
||||
answerCallbackQuery(query, leftRetriesText + leftAttempts)
|
||||
}
|
||||
false
|
||||
}
|
||||
}.firstOrNull()
|
||||
|
||||
callback(leftAttempts > 0)
|
||||
}
|
||||
}
|
||||
}.joinAll()
|
||||
}
|
||||
): CaptchaProviderWorker = Worker(chat, user, adminsApi)
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue