Compare commits

..

No commits in common. "959b497280759da1fd61f4e3e4c7255c9ca0ea3f" and "39c7b42778406ade4b66a8c4608c67beea639ea5" have entirely different histories.

3 changed files with 63 additions and 108 deletions

View File

@ -7,10 +7,10 @@ kotlin.incremental=true
kotlin_version=1.6.21
kotlin_coroutines_version=1.6.1
kotlin_serialisation_runtime_version=1.3.3
plagubot_version=1.1.0
plagubot_version=1.0.1
micro_utils_version=0.10.5
tgbotapi_libraries_version=0.2.0
tgbotapi_libraries_version=0.1.0
project_group=dev.inmo
project_version=0.1.6

View File

@ -15,7 +15,9 @@ import dev.inmo.tgbotapi.extensions.behaviour_builder.triggers_handling.onComman
import dev.inmo.tgbotapi.extensions.behaviour_builder.triggers_handling.onNewChatMembers
import dev.inmo.tgbotapi.extensions.utils.*
import dev.inmo.tgbotapi.extensions.utils.extensions.parseCommandsWithParams
import dev.inmo.tgbotapi.extensions.utils.extensions.sourceChat
import dev.inmo.tgbotapi.libraries.cache.admins.*
import dev.inmo.tgbotapi.types.BotCommand
import dev.inmo.tgbotapi.types.chat.*
import kotlinx.coroutines.*
import kotlinx.serialization.Serializable
@ -127,7 +129,7 @@ class CaptchaBotPlugin : Plugin {
val defaultChatPermissions = (getChat(it.chat) as ExtendedGroupChat).permissions
with (settings.captchaProvider) {
doAction(it.date, chat, newUsers, defaultChatPermissions, adminsAPI)
doAction(it.date, chat, newUsers, defaultChatPermissions)
}
}

View File

@ -9,15 +9,17 @@ import dev.inmo.tgbotapi.extensions.api.answers.answerCallbackQuery
import dev.inmo.tgbotapi.extensions.api.chat.members.*
import dev.inmo.tgbotapi.extensions.api.deleteMessage
import dev.inmo.tgbotapi.extensions.api.edit.reply_markup.editMessageReplyMarkup
import dev.inmo.tgbotapi.extensions.api.send.*
import dev.inmo.tgbotapi.extensions.api.send.sendDice
import dev.inmo.tgbotapi.extensions.api.send.sendTextMessage
import dev.inmo.tgbotapi.extensions.behaviour_builder.*
import dev.inmo.tgbotapi.extensions.behaviour_builder.expectations.waitDataCallbackQuery
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.extensions.raw.message
import dev.inmo.tgbotapi.extensions.utils.formatting.*
import dev.inmo.tgbotapi.extensions.utils.shortcuts.executeUnsafe
import dev.inmo.tgbotapi.extensions.utils.types.buttons.*
import dev.inmo.tgbotapi.libraries.cache.admins.AdminsCacheAPI
import dev.inmo.tgbotapi.extensions.utils.types.buttons.InlineKeyboardMarkup
import dev.inmo.tgbotapi.requests.DeleteMessage
import dev.inmo.tgbotapi.types.*
import dev.inmo.tgbotapi.types.buttons.InlineKeyboardButtons.CallbackDataInlineKeyboardButton
@ -26,10 +28,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.types.message.textsources.mention
import kotlinx.coroutines.*
import kotlinx.coroutines.channels.Channel
import kotlinx.coroutines.channels.toList
import kotlinx.coroutines.flow.*
import kotlinx.coroutines.sync.Mutex
import kotlinx.coroutines.sync.withLock
import kotlinx.serialization.Serializable
@ -42,41 +44,10 @@ sealed class CaptchaProvider {
eventDateTime: DateTime,
chat: GroupChat,
newUsers: List<User>,
leftRestrictionsPermissions: ChatPermissions,
adminsApi: AdminsCacheAPI?
leftRestrictionsPermissions: ChatPermissions
)
}
private const val cancelData = "cancel"
private fun EntitiesBuilder.mention(user: User, defaultName: String = "User"): EntitiesBuilder {
return mention(
listOfNotNull(
user.lastName.takeIf { it.isNotBlank() }, user.firstName.takeIf { it.isNotBlank() }
).takeIf {
it.isNotEmpty()
} ?.joinToString(" ") ?: defaultName,
user
)
}
private suspend fun BehaviourContext.sendAdminCanceledMessage(
chat: Chat,
captchaSolver: User,
admin: User
) {
safelyWithoutExceptions {
sendTextMessage(
chat,
buildEntities {
mention(admin, "Admin")
regular(" cancelled captcha for ")
mention(captchaSolver)
}
)
}
}
private suspend fun BehaviourContext.banUser(
chat: PublicChat,
user: User,
@ -86,7 +57,13 @@ private suspend fun BehaviourContext.banUser(
sendTextMessage(
chat,
buildEntities(" ") {
mention(user)
user.mention(
listOfNotNull(
user.lastName.takeIf { it.isNotBlank() }, user.firstName.takeIf { it.isNotBlank() }
).takeIf {
it.isNotEmpty()
} ?.joinToString(" ") ?: "User"
)
+"failed captcha"
}
)
@ -112,18 +89,17 @@ data class SlotMachineCaptchaProvider(
eventDateTime: DateTime,
chat: GroupChat,
newUsers: List<User>,
leftRestrictionsPermissions: ChatPermissions,
adminsApi: AdminsCacheAPI?
leftRestrictionsPermissions: ChatPermissions
) {
val userBanDateTime = eventDateTime + checkTimeSpan
val authorized = Channel<User>(newUsers.size)
val messagesToDelete = Channel<Message>(Channel.UNLIMITED)
val subContexts = newUsers.map { user ->
val subContexts = newUsers.map {
createSubContextAndDoWithUpdatesFilter (stopOnCompletion = false) {
val sentMessage = sendTextMessage(
chat,
buildEntities {
mention(user)
+it.mention(it.firstName)
regular(", $captchaText")
}
).also { messagesToDelete.send(it) }
@ -143,7 +119,9 @@ data class SlotMachineCaptchaProvider(
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()
val userClicked = waitMessageDataCallbackQuery {
if (user.id == it.id && this.message.messageId == sentDice.messageId) this else null
}.first()
if (userClicked.data == leftToClick.first()) {
clicked[3 - leftToClick.size] = leftToClick.removeAt(0)
if (clicked.contains(null)) {
@ -158,12 +136,12 @@ data class SlotMachineCaptchaProvider(
safelyWithoutExceptions { answerCallbackQuery(userClicked, "Nope") }
}
}
authorized.send(user)
safelyWithoutExceptions { restrictChatMember(chat, user, permissions = leftRestrictionsPermissions) }
authorized.send(it)
safelyWithoutExceptions { restrictChatMember(chat, it, permissions = leftRestrictionsPermissions) }
stop()
}
this to user
this to it
}
}
@ -201,30 +179,22 @@ data class SimpleCaptchaProvider(
eventDateTime: DateTime,
chat: GroupChat,
newUsers: List<User>,
leftRestrictionsPermissions: ChatPermissions,
adminsApi: AdminsCacheAPI?
leftRestrictionsPermissions: ChatPermissions
) {
val userBanDateTime = eventDateTime + checkTimeSpan
newUsers.map { user ->
newUsers.map {
launchSafelyWithoutExceptions {
createSubContext(this).doInContext(stopOnCompletion = false) {
val callbackData = uuid4().toString()
val sentMessage = sendTextMessage(
chat,
buildEntities {
mention(user)
+it.mention(it.firstName)
regular(", $captchaText")
},
replyMarkup = inlineKeyboard {
row {
dataButton(buttonText, callbackData)
}
if (adminsApi != null) {
row {
dataButton("Cancel (Admins only)", cancelData)
}
}
}
replyMarkup = InlineKeyboardMarkup(
CallbackDataInlineKeyboardButton(buttonText, callbackData)
)
)
suspend fun removeRedundantMessages() {
@ -234,22 +204,14 @@ data class SimpleCaptchaProvider(
}
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
)
waitMessageDataCallbackQuery (
filter = { query ->
query.user.id == it.id && query.data == callbackData && query.message.messageId == sentMessage.messageId
}
baseCheck && (adminCanceled || userAnswered)
}.first()
).first()
removeRedundantMessages()
safelyWithoutExceptions { restrictChatMember(chat, user, permissions = leftRestrictionsPermissions) }
safelyWithoutExceptions { restrictChatMember(chat, it, permissions = leftRestrictionsPermissions) }
stop()
}
@ -258,7 +220,7 @@ data class SimpleCaptchaProvider(
if (job.isActive) {
job.cancel()
if (kick) {
banUser(chat, user, leftRestrictionsPermissions)
banUser(chat, it, leftRestrictionsPermissions)
}
}
stop()
@ -326,8 +288,7 @@ data class ExpressionCaptchaProvider(
eventDateTime: DateTime,
chat: GroupChat,
newUsers: List<User>,
leftRestrictionsPermissions: ChatPermissions,
adminsApi: AdminsCacheAPI?
leftRestrictionsPermissions: ChatPermissions
) {
val userBanDateTime = eventDateTime + checkTimeSpan
newUsers.map { user ->
@ -347,20 +308,15 @@ data class ExpressionCaptchaProvider(
val sentMessage = sendTextMessage(
chat,
buildEntities {
mention(user)
+user.mention(user.firstName)
regular(", $captchaText ")
bold(callbackData.second)
},
replyMarkup = inlineKeyboard {
replyMarkup = dev.inmo.tgbotapi.types.buttons.InlineKeyboardMarkup(
answers.map {
CallbackDataInlineKeyboardButton(it.toString(), it.toString())
}.chunked(3).forEach(::add)
if (adminsApi != null) {
row {
dataButton("Cancel (Admins only)", cancelData)
}
}
}
}.chunked(3)
)
)
suspend fun removeRedundantMessages() {
@ -397,28 +353,25 @@ data class ExpressionCaptchaProvider(
}
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
)
waitMessageDataCallbackQuery (
filter = { query ->
query.user.id == user.id && query.message.messageId == sentMessage.messageId
}
true
} else {
) {
if (this.data != correctAnswer) {
leftAttempts--
if (leftAttempts > 0) {
answerCallbackQuery(query, leftRetriesText + leftAttempts)
if (leftAttempts < 1) {
this
} else {
answerCallbackQuery(this@waitMessageDataCallbackQuery, leftRetriesText + leftAttempts)
null
}
false
} else {
this
}
}.firstOrNull()
}.take(1)
banJob.cancel()
callback(leftAttempts > 0)
}