mirror of
https://github.com/InsanusMokrassar/CaptchaPlaguBotPlugin.git
synced 2024-11-22 16:23:56 +00:00
updates
This commit is contained in:
parent
2165f665ec
commit
418d6b7f45
@ -7,11 +7,11 @@ kotlin.incremental=true
|
|||||||
kotlin_version=1.7.10
|
kotlin_version=1.7.10
|
||||||
kotlin_coroutines_version=1.6.4
|
kotlin_coroutines_version=1.6.4
|
||||||
kotlin_serialisation_runtime_version=1.4.0
|
kotlin_serialisation_runtime_version=1.4.0
|
||||||
plagubot_version=2.3.2
|
plagubot_version=2.3.3
|
||||||
|
|
||||||
micro_utils_version=0.12.13
|
micro_utils_version=0.12.13
|
||||||
tgbotapi_libraries_version=0.5.4
|
tgbotapi_libraries_version=0.5.4
|
||||||
commands_version=0.3.3
|
commands_version=0.3.4
|
||||||
|
|
||||||
project_group=dev.inmo
|
project_group=dev.inmo
|
||||||
project_version=0.1.6
|
project_version=0.1.6
|
||||||
|
@ -125,7 +125,7 @@ class CaptchaBotPlugin : Plugin {
|
|||||||
|
|
||||||
override suspend fun BehaviourContext.setupBotPlugin(koin: Koin) {
|
override suspend fun BehaviourContext.setupBotPlugin(koin: Koin) {
|
||||||
val repo: CaptchaChatsSettingsRepo by koin.inject()
|
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()
|
suspend fun Chat.settings() = repo.getById(id) ?: repo.create(ChatSettings(id)).first()
|
||||||
|
|
||||||
@ -142,7 +142,7 @@ class CaptchaBotPlugin : Plugin {
|
|||||||
deleteMessage(it)
|
deleteMessage(it)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
val chat = it.chat.requireGroupChat()
|
val chat = it.chat.groupChatOrThrow()
|
||||||
val newUsers = it.chatEvent.members
|
val newUsers = it.chatEvent.members
|
||||||
newUsers.forEach { user ->
|
newUsers.forEach { user ->
|
||||||
restrictChatMember(
|
restrictChatMember(
|
||||||
|
@ -14,7 +14,6 @@ import dev.inmo.tgbotapi.extensions.behaviour_builder.*
|
|||||||
import dev.inmo.tgbotapi.extensions.behaviour_builder.expectations.waitMessageDataCallbackQuery
|
import dev.inmo.tgbotapi.extensions.behaviour_builder.expectations.waitMessageDataCallbackQuery
|
||||||
import dev.inmo.tgbotapi.extensions.utils.asSlotMachineReelImage
|
import dev.inmo.tgbotapi.extensions.utils.asSlotMachineReelImage
|
||||||
import dev.inmo.tgbotapi.extensions.utils.calculateSlotMachineResult
|
import dev.inmo.tgbotapi.extensions.utils.calculateSlotMachineResult
|
||||||
import dev.inmo.tgbotapi.extensions.utils.formatting.*
|
|
||||||
import dev.inmo.tgbotapi.extensions.utils.shortcuts.executeUnsafe
|
import dev.inmo.tgbotapi.extensions.utils.shortcuts.executeUnsafe
|
||||||
import dev.inmo.tgbotapi.extensions.utils.types.buttons.*
|
import dev.inmo.tgbotapi.extensions.utils.types.buttons.*
|
||||||
import dev.inmo.tgbotapi.libraries.cache.admins.AdminsCacheAPI
|
import dev.inmo.tgbotapi.libraries.cache.admins.AdminsCacheAPI
|
||||||
@ -26,6 +25,10 @@ import dev.inmo.tgbotapi.types.chat.*
|
|||||||
import dev.inmo.tgbotapi.types.chat.User
|
import dev.inmo.tgbotapi.types.chat.User
|
||||||
import dev.inmo.tgbotapi.types.dice.SlotMachineDiceAnimationType
|
import dev.inmo.tgbotapi.types.dice.SlotMachineDiceAnimationType
|
||||||
import dev.inmo.tgbotapi.types.message.abstracts.Message
|
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.*
|
||||||
import kotlinx.coroutines.channels.Channel
|
import kotlinx.coroutines.channels.Channel
|
||||||
import kotlinx.coroutines.channels.toList
|
import kotlinx.coroutines.channels.toList
|
||||||
@ -56,7 +59,7 @@ private fun EntitiesBuilder.mention(user: User, defaultName: String = "User"): E
|
|||||||
user.lastName.takeIf { it.isNotBlank() }, user.firstName.takeIf { it.isNotBlank() }
|
user.lastName.takeIf { it.isNotBlank() }, user.firstName.takeIf { it.isNotBlank() }
|
||||||
).takeIf {
|
).takeIf {
|
||||||
it.isNotEmpty()
|
it.isNotEmpty()
|
||||||
} ?.joinToString(" ") ?: defaultName,
|
}?.joinToString(" ") ?: defaultName,
|
||||||
user
|
user
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
@ -67,14 +70,13 @@ private suspend fun BehaviourContext.sendAdminCanceledMessage(
|
|||||||
admin: User
|
admin: User
|
||||||
) {
|
) {
|
||||||
safelyWithoutExceptions {
|
safelyWithoutExceptions {
|
||||||
sendTextMessage(
|
send(
|
||||||
chat,
|
chat
|
||||||
buildEntities {
|
) {
|
||||||
mention(admin, "Admin")
|
mention(admin, "Admin")
|
||||||
regular(" cancelled captcha for ")
|
regular(" cancelled captcha for ")
|
||||||
mention(captchaSolver)
|
mention(captchaSolver)
|
||||||
}
|
}
|
||||||
)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -84,13 +86,12 @@ private suspend fun BehaviourContext.banUser(
|
|||||||
leftRestrictionsPermissions: ChatPermissions,
|
leftRestrictionsPermissions: ChatPermissions,
|
||||||
onFailure: suspend BehaviourContext.(Throwable) -> Unit = {
|
onFailure: suspend BehaviourContext.(Throwable) -> Unit = {
|
||||||
safelyWithResult {
|
safelyWithResult {
|
||||||
sendTextMessage(
|
send(
|
||||||
chat,
|
chat
|
||||||
buildEntities(" ") {
|
) {
|
||||||
mention(user)
|
mention(user)
|
||||||
+"failed captcha"
|
+"failed captcha"
|
||||||
}
|
}
|
||||||
)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
): Result<Boolean> = safelyWithResult {
|
): Result<Boolean> = safelyWithResult {
|
||||||
@ -121,14 +122,13 @@ data class SlotMachineCaptchaProvider(
|
|||||||
val authorized = Channel<User>(newUsers.size)
|
val authorized = Channel<User>(newUsers.size)
|
||||||
val messagesToDelete = Channel<Message>(Channel.UNLIMITED)
|
val messagesToDelete = Channel<Message>(Channel.UNLIMITED)
|
||||||
val subContexts = newUsers.map { user ->
|
val subContexts = newUsers.map { user ->
|
||||||
createSubContextAndDoWithUpdatesFilter (stopOnCompletion = false) {
|
createSubContextAndDoWithUpdatesFilter(stopOnCompletion = false) {
|
||||||
val sentMessage = sendTextMessage(
|
val sentMessage = send(
|
||||||
chat,
|
chat
|
||||||
buildEntities {
|
) {
|
||||||
mention(user)
|
mention(user)
|
||||||
regular(", $captchaText")
|
regular(", $captchaText")
|
||||||
}
|
}.also { messagesToDelete.send(it) }
|
||||||
).also { messagesToDelete.send(it) }
|
|
||||||
val sentDice = sendDice(
|
val sentDice = sendDice(
|
||||||
sentMessage.chat,
|
sentMessage.chat,
|
||||||
SlotMachineDiceAnimationType,
|
SlotMachineDiceAnimationType,
|
||||||
@ -145,25 +145,43 @@ data class SlotMachineCaptchaProvider(
|
|||||||
launch {
|
launch {
|
||||||
val clicked = arrayOf<String?>(null, null, null)
|
val clicked = arrayOf<String?>(null, null, null)
|
||||||
while (leftToClick.isNotEmpty()) {
|
while (leftToClick.isNotEmpty()) {
|
||||||
val userClicked = waitMessageDataCallbackQuery().filter { it.user.id == user.id && it.message.messageId == sentDice.messageId }.first()
|
val userClicked =
|
||||||
|
waitMessageDataCallbackQuery().filter { it.user.id == user.id && it.message.messageId == sentDice.messageId }
|
||||||
|
.first()
|
||||||
|
|
||||||
when {
|
when {
|
||||||
userClicked.data == leftToClick.first() -> {
|
userClicked.data == leftToClick.first() -> {
|
||||||
clicked[3 - leftToClick.size] = leftToClick.removeAt(0)
|
clicked[3 - leftToClick.size] = leftToClick.removeAt(0)
|
||||||
if (clicked.contains(null)) {
|
if (clicked.contains(null)) {
|
||||||
safelyWithoutExceptions { answerCallbackQuery(userClicked, "Ok, next one") }
|
safelyWithoutExceptions { answerCallbackQuery(userClicked, "Ok, next one") }
|
||||||
editMessageReplyMarkup(sentDice, slotMachineReplyMarkup(clicked[0], clicked[1], clicked[2]))
|
editMessageReplyMarkup(
|
||||||
|
sentDice,
|
||||||
|
slotMachineReplyMarkup(clicked[0], clicked[1], clicked[2])
|
||||||
|
)
|
||||||
} else {
|
} else {
|
||||||
safelyWithoutExceptions { answerCallbackQuery(userClicked, "Thank you and welcome", showAlert = true) }
|
safelyWithoutExceptions {
|
||||||
|
answerCallbackQuery(
|
||||||
|
userClicked,
|
||||||
|
"Thank you and welcome",
|
||||||
|
showAlert = true
|
||||||
|
)
|
||||||
|
}
|
||||||
safelyWithoutExceptions { deleteMessage(sentMessage) }
|
safelyWithoutExceptions { deleteMessage(sentMessage) }
|
||||||
safelyWithoutExceptions { deleteMessage(sentDice) }
|
safelyWithoutExceptions { deleteMessage(sentDice) }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
else -> safelyWithoutExceptions { answerCallbackQuery(userClicked, "Nope") }
|
else -> safelyWithoutExceptions { answerCallbackQuery(userClicked, "Nope") }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
authorized.send(user)
|
authorized.send(user)
|
||||||
safelyWithoutExceptions { restrictChatMember(chat, user, permissions = leftRestrictionsPermissions) }
|
safelyWithoutExceptions {
|
||||||
|
restrictChatMember(
|
||||||
|
chat,
|
||||||
|
user,
|
||||||
|
permissions = leftRestrictionsPermissions
|
||||||
|
)
|
||||||
|
}
|
||||||
stop()
|
stop()
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -213,12 +231,8 @@ data class SimpleCaptchaProvider(
|
|||||||
launchSafelyWithoutExceptions {
|
launchSafelyWithoutExceptions {
|
||||||
createSubContext(this).doInContext(stopOnCompletion = false) {
|
createSubContext(this).doInContext(stopOnCompletion = false) {
|
||||||
val callbackData = uuid4().toString()
|
val callbackData = uuid4().toString()
|
||||||
val sentMessage = sendTextMessage(
|
val sentMessage = send(
|
||||||
chat,
|
chat,
|
||||||
buildEntities {
|
|
||||||
mention(user)
|
|
||||||
regular(", $captchaText")
|
|
||||||
},
|
|
||||||
replyMarkup = inlineKeyboard {
|
replyMarkup = inlineKeyboard {
|
||||||
row {
|
row {
|
||||||
dataButton(buttonText, callbackData)
|
dataButton(buttonText, callbackData)
|
||||||
@ -229,7 +243,10 @@ data class SimpleCaptchaProvider(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
)
|
) {
|
||||||
|
mention(user)
|
||||||
|
regular(", $captchaText")
|
||||||
|
}
|
||||||
|
|
||||||
suspend fun removeRedundantMessages() {
|
suspend fun removeRedundantMessages() {
|
||||||
safelyWithoutExceptions {
|
safelyWithoutExceptions {
|
||||||
@ -241,7 +258,10 @@ data class SimpleCaptchaProvider(
|
|||||||
waitMessageDataCallbackQuery().filter { query ->
|
waitMessageDataCallbackQuery().filter { query ->
|
||||||
val baseCheck = query.message.messageId == sentMessage.messageId
|
val baseCheck = query.message.messageId == sentMessage.messageId
|
||||||
val userAnswered = query.user.id == user.id && query.data == callbackData
|
val userAnswered = query.user.id == user.id && query.data == callbackData
|
||||||
val adminCanceled = (query.data == cancelData && (adminsApi ?.isAdmin(sentMessage.chat.id, query.user.id)) == true)
|
val adminCanceled = (query.data == cancelData && (adminsApi?.isAdmin(
|
||||||
|
sentMessage.chat.id,
|
||||||
|
query.user.id
|
||||||
|
)) == true)
|
||||||
if (baseCheck && adminCanceled) {
|
if (baseCheck && adminCanceled) {
|
||||||
sendAdminCanceledMessage(
|
sendAdminCanceledMessage(
|
||||||
sentMessage.chat,
|
sentMessage.chat,
|
||||||
@ -253,7 +273,13 @@ data class SimpleCaptchaProvider(
|
|||||||
}.first()
|
}.first()
|
||||||
|
|
||||||
removeRedundantMessages()
|
removeRedundantMessages()
|
||||||
safelyWithoutExceptions { restrictChatMember(chat, user, permissions = leftRestrictionsPermissions) }
|
safelyWithoutExceptions {
|
||||||
|
restrictChatMember(
|
||||||
|
chat,
|
||||||
|
user,
|
||||||
|
permissions = leftRestrictionsPermissions
|
||||||
|
)
|
||||||
|
}
|
||||||
stop()
|
stop()
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -279,15 +305,19 @@ private object ExpressionBuilder {
|
|||||||
|
|
||||||
override fun Int.perform(other: Int): Int = plus(other)
|
override fun Int.perform(other: Int): Int = plus(other)
|
||||||
}
|
}
|
||||||
|
|
||||||
object MinusExpressionOperation : ExpressionOperation() {
|
object MinusExpressionOperation : ExpressionOperation() {
|
||||||
override fun asString(): String = "-"
|
override fun asString(): String = "-"
|
||||||
|
|
||||||
override fun Int.perform(other: Int): Int = minus(other)
|
override fun Int.perform(other: Int): Int = minus(other)
|
||||||
}
|
}
|
||||||
|
|
||||||
abstract fun asString(): String
|
abstract fun asString(): String
|
||||||
abstract fun Int.perform(other: Int): Int
|
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)
|
private fun createNumber(max: Int) = Random.nextInt(max + 1)
|
||||||
fun generateResult(max: Int, operationsNumber: Int = 1): Int {
|
fun generateResult(max: Int, operationsNumber: Int = 1): Int {
|
||||||
@ -299,6 +329,7 @@ private object ExpressionBuilder {
|
|||||||
}
|
}
|
||||||
return current
|
return current
|
||||||
}
|
}
|
||||||
|
|
||||||
fun createExpression(max: Int, operationsNumber: Int = 1): Pair<Int, String> {
|
fun createExpression(max: Int, operationsNumber: Int = 1): Pair<Int, String> {
|
||||||
val operations = (0 until operationsNumber).map { experssions.random() }
|
val operations = (0 until operationsNumber).map { experssions.random() }
|
||||||
var current = createNumber(max)
|
var current = createNumber(max)
|
||||||
@ -348,13 +379,8 @@ data class ExpressionCaptchaProvider(
|
|||||||
val correctAnswerPosition = Random.nextInt(orderedAnswers.size)
|
val correctAnswerPosition = Random.nextInt(orderedAnswers.size)
|
||||||
orderedAnswers.add(correctAnswerPosition, callbackData.first)
|
orderedAnswers.add(correctAnswerPosition, callbackData.first)
|
||||||
}.toList()
|
}.toList()
|
||||||
val sentMessage = sendTextMessage(
|
val sentMessage = send(
|
||||||
chat,
|
chat,
|
||||||
buildEntities {
|
|
||||||
mention(user)
|
|
||||||
regular(", $captchaText ")
|
|
||||||
bold(callbackData.second)
|
|
||||||
},
|
|
||||||
replyMarkup = inlineKeyboard {
|
replyMarkup = inlineKeyboard {
|
||||||
answers.map {
|
answers.map {
|
||||||
CallbackDataInlineKeyboardButton(it.toString(), it.toString())
|
CallbackDataInlineKeyboardButton(it.toString(), it.toString())
|
||||||
@ -365,7 +391,11 @@ data class ExpressionCaptchaProvider(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
)
|
) {
|
||||||
|
mention(user)
|
||||||
|
regular(", $captchaText ")
|
||||||
|
bold(callbackData.second)
|
||||||
|
}
|
||||||
|
|
||||||
suspend fun removeRedundantMessages() {
|
suspend fun removeRedundantMessages() {
|
||||||
safelyWithoutExceptions {
|
safelyWithoutExceptions {
|
||||||
@ -381,7 +411,14 @@ data class ExpressionCaptchaProvider(
|
|||||||
removeRedundantMessages()
|
removeRedundantMessages()
|
||||||
passed = it
|
passed = it
|
||||||
when {
|
when {
|
||||||
it -> safelyWithoutExceptions { restrictChatMember(chat, user, permissions = leftRestrictionsPermissions) }
|
it -> safelyWithoutExceptions {
|
||||||
|
restrictChatMember(
|
||||||
|
chat,
|
||||||
|
user,
|
||||||
|
permissions = leftRestrictionsPermissions
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
kickOnUnsuccess -> banUser(chat, user, leftRestrictionsPermissions)
|
kickOnUnsuccess -> banUser(chat, user, leftRestrictionsPermissions)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -401,7 +438,10 @@ data class ExpressionCaptchaProvider(
|
|||||||
waitMessageDataCallbackQuery().takeWhile { leftAttempts > 0 }.filter { query ->
|
waitMessageDataCallbackQuery().takeWhile { leftAttempts > 0 }.filter { query ->
|
||||||
val baseCheck = query.message.messageId == sentMessage.messageId
|
val baseCheck = query.message.messageId == sentMessage.messageId
|
||||||
val dataCorrect = (query.user.id == user.id && query.data == correctAnswer)
|
val dataCorrect = (query.user.id == user.id && query.data == correctAnswer)
|
||||||
val adminCanceled = (query.data == cancelData && (adminsApi ?.isAdmin(sentMessage.chat.id, query.user.id)) == true)
|
val adminCanceled = (query.data == cancelData && (adminsApi?.isAdmin(
|
||||||
|
sentMessage.chat.id,
|
||||||
|
query.user.id
|
||||||
|
)) == true)
|
||||||
baseCheck && if (dataCorrect || adminCanceled) {
|
baseCheck && if (dataCorrect || adminCanceled) {
|
||||||
banJob.cancel()
|
banJob.cancel()
|
||||||
if (adminCanceled) {
|
if (adminCanceled) {
|
||||||
|
Loading…
Reference in New Issue
Block a user