mirror of
https://github.com/InsanusMokrassar/CaptchaPlaguBotPlugin.git
synced 2026-01-05 03:29:14 +00:00
Compare commits
33 Commits
master
...
e375170567
| Author | SHA1 | Date | |
|---|---|---|---|
| e375170567 | |||
| 8017678da8 | |||
| 317db251b9 | |||
| 43cdef96d0 | |||
| 7d76814a41 | |||
| 4fbb752b8f | |||
| 306eccf380 | |||
| d375875067 | |||
| 0e097fc9ba | |||
| 852262853e | |||
| 88047703ad | |||
| 867a2b6fe5 | |||
| f4019f67e2 | |||
| 6235837cee | |||
| f952db018e | |||
| 5c7d9dce05 | |||
| 4a0e2cc843 | |||
| 5e52e2c32e | |||
| 98f07d6611 | |||
| 0cb1b45c0e | |||
| 9fa72f8716 | |||
| 6fe5f96e4e | |||
| 338e97770d | |||
| 1520a670c4 | |||
| 3a7ef56565 | |||
| 4a7339afd9 | |||
| 1d4baa8be9 | |||
| b3a9a9875f | |||
| 8cc2503934 | |||
| 87a6cab33e | |||
| f84edb7860 | |||
| 506f319c88 | |||
| b09bcd1d75 |
2
.github/workflows/publish_package.yml
vendored
2
.github/workflows/publish_package.yml
vendored
@@ -8,7 +8,7 @@ jobs:
|
|||||||
- uses: actions/checkout@v2
|
- uses: actions/checkout@v2
|
||||||
- uses: actions/setup-java@v1
|
- uses: actions/setup-java@v1
|
||||||
with:
|
with:
|
||||||
java-version: 1.8
|
java-version: 11
|
||||||
- name: Update version
|
- name: Update version
|
||||||
run: |
|
run: |
|
||||||
branch="`echo "${{ github.ref }}" | grep -o "[^/]*$"`"
|
branch="`echo "${{ github.ref }}" | grep -o "[^/]*$"`"
|
||||||
|
|||||||
@@ -32,6 +32,14 @@ repositories {
|
|||||||
password = project.hasProperty("GITHUB_TOKEN") ? project.getProperty("GITHUB_TOKEN") : System.getenv("GITHUB_TOKEN")
|
password = project.hasProperty("GITHUB_TOKEN") ? project.getProperty("GITHUB_TOKEN") : System.getenv("GITHUB_TOKEN")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
maven {
|
||||||
|
name = "GitHubPackages"
|
||||||
|
url = uri("https://maven.pkg.github.com/InsanusMokrassar/MicroUtils")
|
||||||
|
credentials {
|
||||||
|
username = project.hasProperty("GITHUB_USER") ? project.getProperty("GITHUB_USER") : System.getenv("GITHUB_USER")
|
||||||
|
password = project.hasProperty("GITHUB_TOKEN") ? project.getProperty("GITHUB_TOKEN") : System.getenv("GITHUB_TOKEN")
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -4,13 +4,13 @@ org.gradle.parallel=true
|
|||||||
kotlin.js.generate.externals=true
|
kotlin.js.generate.externals=true
|
||||||
kotlin.incremental=true
|
kotlin.incremental=true
|
||||||
|
|
||||||
kotlin_version=1.4.31
|
kotlin_version=1.6.21
|
||||||
kotlin_coroutines_version=1.4.3
|
kotlin_coroutines_version=1.6.1
|
||||||
kotlin_serialisation_runtime_version=1.1.0
|
kotlin_serialisation_runtime_version=1.3.3
|
||||||
plagubot_version=0.1.5
|
plagubot_version=1.0.0
|
||||||
|
|
||||||
micro_utils_version=0.4.30
|
micro_utils_version=0.10.4
|
||||||
tgbotapi_libraries_version=0.0.2-branch_master-build12
|
tgbotapi_libraries_version=0.1.0
|
||||||
|
|
||||||
project_group=dev.inmo
|
project_group=dev.inmo
|
||||||
project_version=0.1.5
|
project_version=0.1.6
|
||||||
|
|||||||
2
gradle/wrapper/gradle-wrapper.properties
vendored
2
gradle/wrapper/gradle-wrapper.properties
vendored
@@ -1,5 +1,5 @@
|
|||||||
distributionBase=GRADLE_USER_HOME
|
distributionBase=GRADLE_USER_HOME
|
||||||
distributionPath=wrapper/dists
|
distributionPath=wrapper/dists
|
||||||
distributionUrl=https\://services.gradle.org/distributions/gradle-6.8.2-bin.zip
|
distributionUrl=https\://services.gradle.org/distributions/gradle-7.3.3-bin.zip
|
||||||
zipStoreBase=GRADLE_USER_HOME
|
zipStoreBase=GRADLE_USER_HOME
|
||||||
zipStorePath=wrapper/dists
|
zipStorePath=wrapper/dists
|
||||||
|
|||||||
@@ -4,155 +4,175 @@ import dev.inmo.micro_utils.coroutines.*
|
|||||||
import dev.inmo.micro_utils.repos.create
|
import dev.inmo.micro_utils.repos.create
|
||||||
import dev.inmo.plagubot.Plugin
|
import dev.inmo.plagubot.Plugin
|
||||||
import dev.inmo.plagubot.plugins.captcha.db.CaptchaChatsSettingsRepo
|
import dev.inmo.plagubot.plugins.captcha.db.CaptchaChatsSettingsRepo
|
||||||
|
import dev.inmo.plagubot.plugins.captcha.provider.*
|
||||||
import dev.inmo.plagubot.plugins.captcha.settings.ChatSettings
|
import dev.inmo.plagubot.plugins.captcha.settings.ChatSettings
|
||||||
import dev.inmo.tgbotapi.bot.TelegramBot
|
import dev.inmo.tgbotapi.extensions.api.chat.get.getChat
|
||||||
import dev.inmo.tgbotapi.extensions.api.answers.answerCallbackQuery
|
|
||||||
import dev.inmo.tgbotapi.extensions.api.chat.members.*
|
import dev.inmo.tgbotapi.extensions.api.chat.members.*
|
||||||
import dev.inmo.tgbotapi.extensions.api.deleteMessage
|
import dev.inmo.tgbotapi.extensions.api.deleteMessage
|
||||||
import dev.inmo.tgbotapi.extensions.api.edit.ReplyMarkup.editMessageReplyMarkup
|
import dev.inmo.tgbotapi.extensions.api.send.*
|
||||||
import dev.inmo.tgbotapi.extensions.api.send.media.reply
|
|
||||||
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.*
|
||||||
import dev.inmo.tgbotapi.extensions.behaviour_builder.expectations.waitBaseInlineQuery
|
|
||||||
import dev.inmo.tgbotapi.extensions.behaviour_builder.expectations.waitDataCallbackQuery
|
|
||||||
import dev.inmo.tgbotapi.extensions.behaviour_builder.triggers_handling.onCommand
|
import dev.inmo.tgbotapi.extensions.behaviour_builder.triggers_handling.onCommand
|
||||||
import dev.inmo.tgbotapi.updateshandlers.FlowsUpdatesFilter
|
|
||||||
import dev.inmo.tgbotapi.extensions.behaviour_builder.triggers_handling.onNewChatMembers
|
import dev.inmo.tgbotapi.extensions.behaviour_builder.triggers_handling.onNewChatMembers
|
||||||
import dev.inmo.tgbotapi.extensions.utils.*
|
import dev.inmo.tgbotapi.extensions.utils.*
|
||||||
import dev.inmo.tgbotapi.extensions.utils.formatting.buildEntities
|
import dev.inmo.tgbotapi.extensions.utils.extensions.parseCommandsWithParams
|
||||||
import dev.inmo.tgbotapi.extensions.utils.formatting.regular
|
import dev.inmo.tgbotapi.extensions.utils.extensions.sourceChat
|
||||||
import dev.inmo.tgbotapi.extensions.utils.shortcuts.executeUnsafe
|
|
||||||
import dev.inmo.tgbotapi.libraries.cache.admins.*
|
import dev.inmo.tgbotapi.libraries.cache.admins.*
|
||||||
import dev.inmo.tgbotapi.requests.DeleteMessage
|
|
||||||
import dev.inmo.tgbotapi.types.BotCommand
|
import dev.inmo.tgbotapi.types.BotCommand
|
||||||
import dev.inmo.tgbotapi.types.MessageEntity.textsources.mention
|
import dev.inmo.tgbotapi.types.chat.*
|
||||||
import dev.inmo.tgbotapi.types.User
|
|
||||||
import dev.inmo.tgbotapi.types.chat.ChatPermissions
|
|
||||||
import dev.inmo.tgbotapi.types.chat.LeftRestrictionsChatPermissions
|
|
||||||
import dev.inmo.tgbotapi.types.chat.abstracts.Chat
|
|
||||||
import dev.inmo.tgbotapi.types.chat.abstracts.PublicChat
|
|
||||||
import dev.inmo.tgbotapi.types.dice.SlotMachineDiceAnimationType
|
|
||||||
import dev.inmo.tgbotapi.types.message.abstracts.*
|
|
||||||
import dev.inmo.tgbotapi.types.message.content.abstracts.MessageContent
|
|
||||||
import kotlinx.coroutines.*
|
import kotlinx.coroutines.*
|
||||||
import kotlinx.coroutines.channels.Channel
|
|
||||||
import kotlinx.coroutines.channels.toList
|
|
||||||
import kotlinx.serialization.Serializable
|
import kotlinx.serialization.Serializable
|
||||||
|
import kotlinx.serialization.json.JsonObject
|
||||||
import org.jetbrains.exposed.sql.Database
|
import org.jetbrains.exposed.sql.Database
|
||||||
|
import org.koin.core.Koin
|
||||||
|
import org.koin.core.module.Module
|
||||||
|
|
||||||
private const val enableAutoDeleteCommands = "captcha_auto_delete_commands_on"
|
private const val enableAutoDeleteCommands = "captcha_auto_delete_commands_on"
|
||||||
private const val disableAutoDeleteCommands = "captcha_auto_delete_commands_off"
|
private const val disableAutoDeleteCommands = "captcha_auto_delete_commands_off"
|
||||||
|
private const val enableAutoDeleteServiceMessages = "captcha_auto_delete_events_on"
|
||||||
|
private const val disableAutoDeleteServiceMessages = "captcha_auto_delete_events_off"
|
||||||
|
|
||||||
|
private const val enableSlotMachineCaptcha = "captcha_use_slot_machine"
|
||||||
|
private const val enableSimpleCaptcha = "captcha_use_simple"
|
||||||
|
private const val enableExpressionCaptcha = "captcha_use_expression"
|
||||||
|
private const val disableCaptcha = "disable_captcha"
|
||||||
|
private const val enableCaptcha = "enable_captcha"
|
||||||
|
|
||||||
|
private val enableDisableKickOnUnsuccess = Regex("captcha_(enable|disable)_kick")
|
||||||
|
private const val enableKickOnUnsuccess = "captcha_enable_kick"
|
||||||
|
private const val disableKickOnUnsuccess = "captcha_disable_kick"
|
||||||
|
|
||||||
|
private val changeCaptchaMethodCommandRegex = Regex(
|
||||||
|
"captcha_use_((slot_machine)|(simple)|(expression))"
|
||||||
|
)
|
||||||
|
|
||||||
@Serializable
|
@Serializable
|
||||||
class CaptchaBotPlugin : Plugin {
|
class CaptchaBotPlugin : Plugin {
|
||||||
override suspend fun getCommands(): List<BotCommand> = listOf(
|
// override suspend fun getCommands(): List<BotCommand> = listOf(
|
||||||
BotCommand(
|
// BotCommand(
|
||||||
enableAutoDeleteCommands,
|
// enableAutoDeleteCommands,
|
||||||
"Enable auto removing of commands addressed to captcha plugin"
|
// "Enable auto removing of commands addressed to captcha plugin"
|
||||||
),
|
// ),
|
||||||
BotCommand(
|
// BotCommand(
|
||||||
disableAutoDeleteCommands,
|
// disableAutoDeleteCommands,
|
||||||
"Disable auto removing of commands addressed to captcha plugin"
|
// "Disable auto removing of commands addressed to captcha plugin"
|
||||||
)
|
// ),
|
||||||
)
|
// BotCommand(
|
||||||
|
// enableAutoDeleteServiceMessages,
|
||||||
|
// "Enable auto removing of users joined messages"
|
||||||
|
// ),
|
||||||
|
// BotCommand(
|
||||||
|
// disableAutoDeleteServiceMessages,
|
||||||
|
// "Disable auto removing of users joined messages"
|
||||||
|
// ),
|
||||||
|
// BotCommand(
|
||||||
|
// enableSlotMachineCaptcha,
|
||||||
|
// "Change captcha method to slot machine"
|
||||||
|
// ),
|
||||||
|
// BotCommand(
|
||||||
|
// enableSimpleCaptcha,
|
||||||
|
// "Change captcha method to simple button"
|
||||||
|
// ),
|
||||||
|
// BotCommand(
|
||||||
|
// disableCaptcha,
|
||||||
|
// "Disable captcha for chat"
|
||||||
|
// ),
|
||||||
|
// BotCommand(
|
||||||
|
// enableCaptcha,
|
||||||
|
// "Enable captcha for chat"
|
||||||
|
// ),
|
||||||
|
// BotCommand(
|
||||||
|
// enableExpressionCaptcha,
|
||||||
|
// "Change captcha method to expressions"
|
||||||
|
// ),
|
||||||
|
// BotCommand(
|
||||||
|
// enableKickOnUnsuccess,
|
||||||
|
// "Not solved captcha users will be kicked from the chat"
|
||||||
|
// ),
|
||||||
|
// BotCommand(
|
||||||
|
// disableKickOnUnsuccess,
|
||||||
|
// "Not solved captcha users will NOT be kicked from the chat"
|
||||||
|
// )
|
||||||
|
// )
|
||||||
|
|
||||||
|
override fun Module.setupDI(database: Database, params: JsonObject) {
|
||||||
|
single { CaptchaChatsSettingsRepo(database) }
|
||||||
|
}
|
||||||
|
|
||||||
|
override suspend fun BehaviourContext.setupBotPlugin(koin: Koin) {
|
||||||
|
val repo: CaptchaChatsSettingsRepo by koin.inject()
|
||||||
|
val adminsAPI = koin.adminsPlugin ?.adminsAPI(koin.get())
|
||||||
|
|
||||||
override suspend fun BehaviourContext.invoke(
|
|
||||||
database: Database,
|
|
||||||
params: Map<String, Any>
|
|
||||||
) {
|
|
||||||
val repo = CaptchaChatsSettingsRepo(database)
|
|
||||||
val adminsAPI = params.adminsPlugin ?.adminsAPI(database)
|
|
||||||
suspend fun Chat.settings() = repo.getById(id) ?: repo.create(ChatSettings(id)).first()
|
suspend fun Chat.settings() = repo.getById(id) ?: repo.create(ChatSettings(id)).first()
|
||||||
|
|
||||||
onNewChatMembers(
|
onNewChatMembers(
|
||||||
additionalFilter = {
|
initialFilter = {
|
||||||
it.chat.asPublicChat() != null
|
it.chat.asPublicChat() != null
|
||||||
},
|
},
|
||||||
includeFilterByChatInBehaviourSubContext = false
|
subcontextUpdatesFilter = { m, u -> u.sourceChat() == m.chat },
|
||||||
) {
|
) {
|
||||||
safelyWithoutExceptions { deleteMessage(it) }
|
launchSafelyWithoutExceptions {
|
||||||
val eventDateTime = it.date
|
val settings = it.chat.settings()
|
||||||
val chat = it.chat.requirePublicChat()
|
if (!settings.enabled) return@launchSafelyWithoutExceptions
|
||||||
val newUsers = it.chatEvent.members
|
|
||||||
newUsers.forEach { user ->
|
|
||||||
restrictChatMember(
|
|
||||||
chat,
|
|
||||||
user,
|
|
||||||
permissions = ChatPermissions()
|
|
||||||
)
|
|
||||||
}
|
|
||||||
val settings = it.chat.settings() ?: return@onNewChatMembers
|
|
||||||
val userBanDateTime = eventDateTime + settings.checkTimeSpan
|
|
||||||
val authorized = Channel<User>(newUsers.size)
|
|
||||||
val messagesToDelete = Channel<Message>(Channel.UNLIMITED)
|
|
||||||
val subContexts = newUsers.map {
|
|
||||||
doInSubContext(stopOnCompletion = false) {
|
|
||||||
val sentMessage = sendTextMessage(
|
|
||||||
chat,
|
|
||||||
buildEntities {
|
|
||||||
+it.mention(it.firstName)
|
|
||||||
regular(", ${settings.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 {
|
safelyWithoutExceptions {
|
||||||
val clicked = arrayOf<String?>(null, null, null)
|
if (settings.autoRemoveEvents) {
|
||||||
while (leftToClick.isNotEmpty()) {
|
deleteMessage(it)
|
||||||
val userClicked = waitDataCallbackQuery { if (user.id == it.id) this else null }.first()
|
|
||||||
if (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(it)
|
|
||||||
safelyWithoutExceptions { restrictChatMember(chat, it, permissions = LeftRestrictionsChatPermissions) }
|
|
||||||
stop()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
this to it
|
|
||||||
}
|
}
|
||||||
}
|
val chat = it.chat.requireGroupChat()
|
||||||
|
val newUsers = it.chatEvent.members
|
||||||
delay((userBanDateTime - eventDateTime).millisecondsLong)
|
newUsers.forEach { user ->
|
||||||
|
restrictChatMember(
|
||||||
authorized.close()
|
chat,
|
||||||
val authorizedUsers = authorized.toList()
|
user,
|
||||||
|
permissions = RestrictionsChatPermissions
|
||||||
subContexts.forEach { (context, user) ->
|
)
|
||||||
if (user !in authorizedUsers) {
|
}
|
||||||
context.stop()
|
val defaultChatPermissions = (getChat(it.chat) as ExtendedGroupChat).permissions
|
||||||
safelyWithoutExceptions { kickChatMember(chat, user) }
|
|
||||||
|
createSubContextAndDoWithUpdatesFilter(stopOnCompletion = false) {
|
||||||
|
launch {
|
||||||
|
settings.captchaProvider.apply { doAction(it.date, chat, newUsers, defaultChatPermissions) }
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
messagesToDelete.close()
|
|
||||||
for (message in messagesToDelete) {
|
|
||||||
executeUnsafe(DeleteMessage(message.chat.id, message.messageId), retries = 0)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (adminsAPI != null) {
|
if (adminsAPI != null) {
|
||||||
|
onCommand(changeCaptchaMethodCommandRegex) {
|
||||||
|
it.doAfterVerification(adminsAPI) {
|
||||||
|
val settings = it.chat.settings()
|
||||||
|
if (settings.autoRemoveCommands) {
|
||||||
|
safelyWithoutExceptions { deleteMessage(it) }
|
||||||
|
}
|
||||||
|
val commands = it.parseCommandsWithParams()
|
||||||
|
val changeCommand = commands.keys.first {
|
||||||
|
println(it)
|
||||||
|
changeCaptchaMethodCommandRegex.matches(it)
|
||||||
|
}
|
||||||
|
println(changeCommand)
|
||||||
|
val captcha = when {
|
||||||
|
changeCommand.startsWith(enableSimpleCaptcha) -> SimpleCaptchaProvider()
|
||||||
|
changeCommand.startsWith(enableExpressionCaptcha) -> ExpressionCaptchaProvider()
|
||||||
|
changeCommand.startsWith(enableSlotMachineCaptcha) -> SlotMachineCaptchaProvider()
|
||||||
|
else -> return@doAfterVerification
|
||||||
|
}
|
||||||
|
val newSettings = settings.copy(captchaProvider = captcha)
|
||||||
|
if (repo.contains(it.chat.id)) {
|
||||||
|
repo.update(it.chat.id, newSettings)
|
||||||
|
} else {
|
||||||
|
repo.create(newSettings)
|
||||||
|
}
|
||||||
|
sendMessage(it.chat, "Settings updated").also { sent ->
|
||||||
|
delay(5000L)
|
||||||
|
|
||||||
|
if (settings.autoRemoveCommands) {
|
||||||
|
deleteMessage(sent)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
onCommand(
|
onCommand(
|
||||||
enableAutoDeleteCommands,
|
enableAutoDeleteCommands,
|
||||||
requireOnlyCommandInMessage = false
|
requireOnlyCommandInMessage = false
|
||||||
@@ -181,6 +201,74 @@ class CaptchaBotPlugin : Plugin {
|
|||||||
)
|
)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
onCommand(disableCaptcha) { message ->
|
||||||
|
message.doAfterVerification(adminsAPI) {
|
||||||
|
val settings = message.chat.settings()
|
||||||
|
|
||||||
|
repo.update(
|
||||||
|
message.chat.id,
|
||||||
|
settings.copy(enabled = false)
|
||||||
|
)
|
||||||
|
|
||||||
|
reply(message, "Captcha has been disabled")
|
||||||
|
|
||||||
|
if (settings.autoRemoveCommands) {
|
||||||
|
deleteMessage(message)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
onCommand(enableCaptcha) { message ->
|
||||||
|
message.doAfterVerification(adminsAPI) {
|
||||||
|
val settings = message.chat.settings()
|
||||||
|
|
||||||
|
repo.update(
|
||||||
|
message.chat.id,
|
||||||
|
settings.copy(enabled = true)
|
||||||
|
)
|
||||||
|
|
||||||
|
reply(message, "Captcha has been enabled")
|
||||||
|
|
||||||
|
if (settings.autoRemoveCommands) {
|
||||||
|
deleteMessage(message)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
onCommand(enableAutoDeleteServiceMessages) { message ->
|
||||||
|
message.doAfterVerification(adminsAPI) {
|
||||||
|
val settings = message.chat.settings()
|
||||||
|
|
||||||
|
repo.update(
|
||||||
|
message.chat.id,
|
||||||
|
settings.copy(autoRemoveEvents = true)
|
||||||
|
)
|
||||||
|
|
||||||
|
reply(message, "Ok, user joined service messages will be deleted")
|
||||||
|
|
||||||
|
if (settings.autoRemoveCommands) {
|
||||||
|
deleteMessage(message)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
onCommand(disableAutoDeleteServiceMessages) { message ->
|
||||||
|
message.doAfterVerification(adminsAPI) {
|
||||||
|
val settings = message.chat.settings()
|
||||||
|
|
||||||
|
repo.update(
|
||||||
|
message.chat.id,
|
||||||
|
settings.copy(autoRemoveEvents = false)
|
||||||
|
)
|
||||||
|
|
||||||
|
reply(message, "Ok, user joined service messages will not be deleted")
|
||||||
|
|
||||||
|
if (settings.autoRemoveCommands) {
|
||||||
|
deleteMessage(message)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,13 +1,25 @@
|
|||||||
package dev.inmo.plagubot.plugins.captcha.db
|
package dev.inmo.plagubot.plugins.captcha.db
|
||||||
|
|
||||||
|
import dev.inmo.micro_utils.coroutines.launchSynchronously
|
||||||
import dev.inmo.micro_utils.repos.exposed.*
|
import dev.inmo.micro_utils.repos.exposed.*
|
||||||
import dev.inmo.micro_utils.repos.exposed.keyvalue.ExposedKeyValueRepo
|
import dev.inmo.micro_utils.repos.versions.VersionsRepo
|
||||||
|
import dev.inmo.plagubot.plugins.captcha.provider.CaptchaProvider
|
||||||
|
import dev.inmo.plagubot.plugins.captcha.provider.SimpleCaptchaProvider
|
||||||
import dev.inmo.plagubot.plugins.captcha.settings.*
|
import dev.inmo.plagubot.plugins.captcha.settings.*
|
||||||
import dev.inmo.tgbotapi.types.ChatId
|
import dev.inmo.tgbotapi.types.ChatId
|
||||||
import dev.inmo.tgbotapi.types.toChatId
|
import dev.inmo.tgbotapi.types.toChatId
|
||||||
|
import kotlinx.coroutines.CoroutineScope
|
||||||
|
import kotlinx.serialization.json.Json
|
||||||
import org.jetbrains.exposed.sql.*
|
import org.jetbrains.exposed.sql.*
|
||||||
import org.jetbrains.exposed.sql.statements.InsertStatement
|
import org.jetbrains.exposed.sql.statements.InsertStatement
|
||||||
import org.jetbrains.exposed.sql.statements.UpdateStatement
|
import org.jetbrains.exposed.sql.statements.UpdateStatement
|
||||||
|
import org.jetbrains.exposed.sql.transactions.transaction
|
||||||
|
|
||||||
|
private val captchaProviderSerialFormat = Json {
|
||||||
|
ignoreUnknownKeys = true
|
||||||
|
}
|
||||||
|
|
||||||
|
private val defaultCaptchaProviderValue = captchaProviderSerialFormat.encodeToString(CaptchaProvider.serializer(), SimpleCaptchaProvider())
|
||||||
|
|
||||||
class CaptchaChatsSettingsRepo(
|
class CaptchaChatsSettingsRepo(
|
||||||
override val database: Database
|
override val database: Database
|
||||||
@@ -15,47 +27,52 @@ class CaptchaChatsSettingsRepo(
|
|||||||
tableName = "CaptchaChatsSettingsRepo"
|
tableName = "CaptchaChatsSettingsRepo"
|
||||||
) {
|
) {
|
||||||
private val chatIdColumn = long("chatId")
|
private val chatIdColumn = long("chatId")
|
||||||
private val checkTimeSecondsColumn = integer("checkTime")
|
private val captchaProviderColumn = text("captchaProvider").apply {
|
||||||
private val solveCaptchaTextColumn = text("solveCaptchaText")
|
default(defaultCaptchaProviderValue)
|
||||||
|
}
|
||||||
private val autoRemoveCommandsColumn = bool("autoRemoveCommands")
|
private val autoRemoveCommandsColumn = bool("autoRemoveCommands")
|
||||||
|
private val autoRemoveEventsColumn = bool("autoRemoveEvents").apply { default(true) }
|
||||||
|
private val enabledColumn = bool("enabled").default(true)
|
||||||
|
|
||||||
override val primaryKey = PrimaryKey(chatIdColumn)
|
override val primaryKey = PrimaryKey(chatIdColumn)
|
||||||
|
|
||||||
override val selectByIds: SqlExpressionBuilder.(List<ChatId>) -> Op<Boolean> = {
|
override val selectByIds: SqlExpressionBuilder.(List<ChatId>) -> Op<Boolean> = {
|
||||||
chatIdColumn.inList(it.map { it.chatId })
|
chatIdColumn.inList(it.map { it.chatId })
|
||||||
}
|
}
|
||||||
override val InsertStatement<Number>.asObject: ChatSettings
|
|
||||||
get() = TODO("Not yet implemented")
|
|
||||||
|
|
||||||
override fun insert(value: ChatSettings, it: InsertStatement<Number>) {
|
override fun insert(value: ChatSettings, it: InsertStatement<Number>) {
|
||||||
it[chatIdColumn] = value.chatId.chatId
|
it[chatIdColumn] = value.chatId.chatId
|
||||||
it[checkTimeSecondsColumn] = value.checkTime
|
it[captchaProviderColumn] = captchaProviderSerialFormat.encodeToString(CaptchaProvider.serializer(), value.captchaProvider)
|
||||||
it[solveCaptchaTextColumn] = value.captchaText
|
|
||||||
it[autoRemoveCommandsColumn] = value.autoRemoveCommands
|
it[autoRemoveCommandsColumn] = value.autoRemoveCommands
|
||||||
|
it[autoRemoveEventsColumn] = value.autoRemoveEvents
|
||||||
|
it[enabledColumn] = value.enabled
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun update(id: ChatId, value: ChatSettings, it: UpdateStatement) {
|
override fun update(id: ChatId, value: ChatSettings, it: UpdateStatement) {
|
||||||
if (id.chatId == value.chatId.chatId) {
|
if (id.chatId == value.chatId.chatId) {
|
||||||
it[checkTimeSecondsColumn] = value.checkTime
|
it[captchaProviderColumn] = captchaProviderSerialFormat.encodeToString(CaptchaProvider.serializer(), value.captchaProvider)
|
||||||
it[solveCaptchaTextColumn] = value.captchaText
|
|
||||||
it[autoRemoveCommandsColumn] = value.autoRemoveCommands
|
it[autoRemoveCommandsColumn] = value.autoRemoveCommands
|
||||||
|
it[autoRemoveEventsColumn] = value.autoRemoveEvents
|
||||||
|
it[enabledColumn] = value.enabled
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun InsertStatement<Number>.asObject(value: ChatSettings): ChatSettings = ChatSettings(
|
override fun InsertStatement<Number>.asObject(value: ChatSettings): ChatSettings = ChatSettings(
|
||||||
get(chatIdColumn).toChatId(),
|
chatId = get(chatIdColumn).toChatId(),
|
||||||
get(checkTimeSecondsColumn),
|
captchaProvider = captchaProviderSerialFormat.decodeFromString(CaptchaProvider.serializer(), get(captchaProviderColumn)),
|
||||||
get(solveCaptchaTextColumn),
|
autoRemoveCommands = get(autoRemoveCommandsColumn),
|
||||||
get(autoRemoveCommandsColumn)
|
autoRemoveEvents = get(autoRemoveEventsColumn),
|
||||||
|
enabled = get(enabledColumn)
|
||||||
)
|
)
|
||||||
|
|
||||||
override val selectById: SqlExpressionBuilder.(ChatId) -> Op<Boolean> = { chatIdColumn.eq(it.chatId) }
|
override val selectById: SqlExpressionBuilder.(ChatId) -> Op<Boolean> = { chatIdColumn.eq(it.chatId) }
|
||||||
override val ResultRow.asObject: ChatSettings
|
override val ResultRow.asObject: ChatSettings
|
||||||
get() = ChatSettings(
|
get() = ChatSettings(
|
||||||
get(chatIdColumn).toChatId(),
|
chatId = get(chatIdColumn).toChatId(),
|
||||||
get(checkTimeSecondsColumn),
|
captchaProvider = captchaProviderSerialFormat.decodeFromString(CaptchaProvider.serializer(), get(captchaProviderColumn)),
|
||||||
get(solveCaptchaTextColumn),
|
autoRemoveCommands = get(autoRemoveCommandsColumn),
|
||||||
get(autoRemoveCommandsColumn)
|
autoRemoveEvents = get(autoRemoveEventsColumn),
|
||||||
|
enabled = get(enabledColumn)
|
||||||
)
|
)
|
||||||
|
|
||||||
init {
|
init {
|
||||||
|
|||||||
@@ -0,0 +1,381 @@
|
|||||||
|
package dev.inmo.plagubot.plugins.captcha.provider
|
||||||
|
|
||||||
|
import com.benasher44.uuid.uuid4
|
||||||
|
import com.soywiz.klock.DateTime
|
||||||
|
import com.soywiz.klock.seconds
|
||||||
|
import dev.inmo.micro_utils.coroutines.safelyWithResult
|
||||||
|
import dev.inmo.micro_utils.coroutines.safelyWithoutExceptions
|
||||||
|
import dev.inmo.plagubot.plugins.captcha.slotMachineReplyMarkup
|
||||||
|
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.ReplyMarkup.editMessageReplyMarkup
|
||||||
|
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.utils.asSlotMachineReelImage
|
||||||
|
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.types.buttons.InlineKeyboardMarkup
|
||||||
|
import dev.inmo.tgbotapi.requests.DeleteMessage
|
||||||
|
import dev.inmo.tgbotapi.types.*
|
||||||
|
import dev.inmo.tgbotapi.types.buttons.InlineKeyboardButtons.CallbackDataInlineKeyboardButton
|
||||||
|
import dev.inmo.tgbotapi.types.chat.ChatPermissions
|
||||||
|
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.sync.Mutex
|
||||||
|
import kotlinx.coroutines.sync.withLock
|
||||||
|
import kotlinx.serialization.Serializable
|
||||||
|
import kotlinx.serialization.Transient
|
||||||
|
import kotlin.random.Random
|
||||||
|
|
||||||
|
@Serializable
|
||||||
|
sealed class CaptchaProvider {
|
||||||
|
abstract suspend fun BehaviourContext.doAction(
|
||||||
|
eventDateTime: DateTime,
|
||||||
|
chat: GroupChat,
|
||||||
|
newUsers: List<User>,
|
||||||
|
leftRestrictionsPermissions: ChatPermissions
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
private suspend fun BehaviourContext.banUser(
|
||||||
|
chat: PublicChat,
|
||||||
|
user: User,
|
||||||
|
leftRestrictionsPermissions: ChatPermissions,
|
||||||
|
onFailure: suspend BehaviourContext.(Throwable) -> Unit = {
|
||||||
|
safelyWithResult {
|
||||||
|
sendTextMessage(
|
||||||
|
chat,
|
||||||
|
buildEntities(" ") {
|
||||||
|
user.mention(
|
||||||
|
listOfNotNull(
|
||||||
|
user.lastName.takeIf { it.isNotBlank() }, user.firstName.takeIf { it.isNotBlank() }
|
||||||
|
).takeIf {
|
||||||
|
it.isNotEmpty()
|
||||||
|
} ?.joinToString(" ") ?: "User"
|
||||||
|
)
|
||||||
|
+"failed captcha"
|
||||||
|
}
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
): Result<Boolean> = safelyWithResult {
|
||||||
|
restrictChatMember(chat, user, permissions = leftRestrictionsPermissions)
|
||||||
|
banChatMember(chat, user)
|
||||||
|
}.onFailure {
|
||||||
|
onFailure(it)
|
||||||
|
}
|
||||||
|
|
||||||
|
@Serializable
|
||||||
|
data class SlotMachineCaptchaProvider(
|
||||||
|
val checkTimeSeconds: Seconds = 300,
|
||||||
|
val captchaText: String = "solve this captcha: ",
|
||||||
|
val kick: Boolean = true
|
||||||
|
) : CaptchaProvider() {
|
||||||
|
@Transient
|
||||||
|
private val checkTimeSpan = checkTimeSeconds.seconds
|
||||||
|
|
||||||
|
override suspend fun BehaviourContext.doAction(
|
||||||
|
eventDateTime: DateTime,
|
||||||
|
chat: GroupChat,
|
||||||
|
newUsers: List<User>,
|
||||||
|
leftRestrictionsPermissions: ChatPermissions
|
||||||
|
) {
|
||||||
|
val userBanDateTime = eventDateTime + checkTimeSpan
|
||||||
|
val authorized = Channel<User>(newUsers.size)
|
||||||
|
val messagesToDelete = Channel<Message>(Channel.UNLIMITED)
|
||||||
|
val subContexts = newUsers.map {
|
||||||
|
createSubContextAndDoWithUpdatesFilter (stopOnCompletion = false) {
|
||||||
|
val sentMessage = sendTextMessage(
|
||||||
|
chat,
|
||||||
|
buildEntities {
|
||||||
|
+it.mention(it.firstName)
|
||||||
|
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 = waitDataCallbackQuery { if (user.id == it.id) this else null }.first()
|
||||||
|
if (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(it)
|
||||||
|
safelyWithoutExceptions { restrictChatMember(chat, it, permissions = leftRestrictionsPermissions) }
|
||||||
|
stop()
|
||||||
|
}
|
||||||
|
|
||||||
|
this to it
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
delay((userBanDateTime - eventDateTime).millisecondsLong)
|
||||||
|
|
||||||
|
authorized.close()
|
||||||
|
val authorizedUsers = authorized.toList()
|
||||||
|
|
||||||
|
subContexts.forEach { (context, user) ->
|
||||||
|
if (user !in authorizedUsers) {
|
||||||
|
context.stop()
|
||||||
|
if (kick) {
|
||||||
|
banUser(chat, user, leftRestrictionsPermissions)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
messagesToDelete.close()
|
||||||
|
for (message in messagesToDelete) {
|
||||||
|
executeUnsafe(DeleteMessage(message.chat.id, message.messageId), retries = 0)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Serializable
|
||||||
|
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
|
||||||
|
) : CaptchaProvider() {
|
||||||
|
@Transient
|
||||||
|
private val checkTimeSpan = checkTimeSeconds.seconds
|
||||||
|
|
||||||
|
override suspend fun BehaviourContext.doAction(
|
||||||
|
eventDateTime: DateTime,
|
||||||
|
chat: GroupChat,
|
||||||
|
newUsers: List<User>,
|
||||||
|
leftRestrictionsPermissions: ChatPermissions
|
||||||
|
) {
|
||||||
|
val userBanDateTime = eventDateTime + checkTimeSpan
|
||||||
|
newUsers.mapNotNull {
|
||||||
|
safelyWithoutExceptions {
|
||||||
|
launch {
|
||||||
|
createSubContextAndDoWithUpdatesFilter(stopOnCompletion = false) {
|
||||||
|
val callbackData = uuid4().toString()
|
||||||
|
val sentMessage = sendTextMessage(
|
||||||
|
chat,
|
||||||
|
buildEntities {
|
||||||
|
+it.mention(it.firstName)
|
||||||
|
regular(", $captchaText")
|
||||||
|
},
|
||||||
|
replyMarkup = InlineKeyboardMarkup(
|
||||||
|
CallbackDataInlineKeyboardButton(buttonText, callbackData)
|
||||||
|
)
|
||||||
|
)
|
||||||
|
|
||||||
|
suspend fun removeRedundantMessages() {
|
||||||
|
safelyWithoutExceptions {
|
||||||
|
deleteMessage(sentMessage)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
val job = parallel {
|
||||||
|
waitDataCallbackQuery {
|
||||||
|
if (it.id == user.id && this.data == callbackData) {
|
||||||
|
this
|
||||||
|
} else {
|
||||||
|
null
|
||||||
|
}
|
||||||
|
}.first()
|
||||||
|
|
||||||
|
removeRedundantMessages()
|
||||||
|
safelyWithoutExceptions { restrictChatMember(chat, it, permissions = leftRestrictionsPermissions) }
|
||||||
|
stop()
|
||||||
|
}
|
||||||
|
|
||||||
|
delay((userBanDateTime - eventDateTime).millisecondsLong)
|
||||||
|
|
||||||
|
if (job.isActive) {
|
||||||
|
job.cancel()
|
||||||
|
if (kick) {
|
||||||
|
banUser(chat, it, leftRestrictionsPermissions)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
stop()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}.joinAll()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private object ExpressionBuilder {
|
||||||
|
sealed class ExpressionOperation {
|
||||||
|
object PlusExpressionOperation : ExpressionOperation() {
|
||||||
|
override fun asString(): String = "+"
|
||||||
|
|
||||||
|
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 fun createNumber(max: Int) = Random.nextInt(max + 1)
|
||||||
|
fun generateResult(max: Int, operationsNumber: Int = 1): Int {
|
||||||
|
val operations = (0 until operationsNumber).map { experssions.random() }
|
||||||
|
var current = createNumber(max)
|
||||||
|
operations.forEach {
|
||||||
|
val rightOne = createNumber(max)
|
||||||
|
current = it.run { current.perform(rightOne) }
|
||||||
|
}
|
||||||
|
return current
|
||||||
|
}
|
||||||
|
fun createExpression(max: Int, operationsNumber: Int = 1): Pair<Int, String> {
|
||||||
|
val operations = (0 until operationsNumber).map { experssions.random() }
|
||||||
|
var current = createNumber(max)
|
||||||
|
var numbersString = "$current"
|
||||||
|
operations.forEach {
|
||||||
|
val rightOne = createNumber(max)
|
||||||
|
current = it.run { current.perform(rightOne) }
|
||||||
|
numbersString += " ${it.asString()} $rightOne"
|
||||||
|
}
|
||||||
|
return current to numbersString
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Serializable
|
||||||
|
data class ExpressionCaptchaProvider(
|
||||||
|
val checkTimeSeconds: Seconds = 60,
|
||||||
|
val captchaText: String = "Solve next captcha:",
|
||||||
|
val leftRetriesText: String = "Nope, left retries: ",
|
||||||
|
val maxPerNumber: Int = 10,
|
||||||
|
val operations: Int = 2,
|
||||||
|
val answers: Int = 6,
|
||||||
|
val attempts: Int = 3,
|
||||||
|
val kick: Boolean = true
|
||||||
|
) : CaptchaProvider() {
|
||||||
|
@Transient
|
||||||
|
private val checkTimeSpan = checkTimeSeconds.seconds
|
||||||
|
|
||||||
|
override suspend fun BehaviourContext.doAction(
|
||||||
|
eventDateTime: DateTime,
|
||||||
|
chat: GroupChat,
|
||||||
|
newUsers: List<User>,
|
||||||
|
leftRestrictionsPermissions: ChatPermissions
|
||||||
|
) {
|
||||||
|
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 {
|
||||||
|
+user.mention(user.firstName)
|
||||||
|
regular(", $captchaText ")
|
||||||
|
bold(callbackData.second)
|
||||||
|
},
|
||||||
|
replyMarkup = dev.inmo.tgbotapi.types.buttons.InlineKeyboardMarkup(
|
||||||
|
answers.map {
|
||||||
|
CallbackDataInlineKeyboardButton(it.toString(), it.toString())
|
||||||
|
}.chunked(3)
|
||||||
|
)
|
||||||
|
)
|
||||||
|
|
||||||
|
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
|
||||||
|
if (it) {
|
||||||
|
safelyWithoutExceptions { restrictChatMember(chat, user, permissions = leftRestrictionsPermissions) }
|
||||||
|
} else {
|
||||||
|
if (kick) {
|
||||||
|
banUser(chat, user, leftRestrictionsPermissions)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
val banJob = launch {
|
||||||
|
delay((userBanDateTime - eventDateTime).millisecondsLong)
|
||||||
|
|
||||||
|
if (passed == null) {
|
||||||
|
callback(false)
|
||||||
|
stop()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
var leftAttempts = attempts
|
||||||
|
waitDataCallbackQuery {
|
||||||
|
when {
|
||||||
|
this.user.id != user.id -> null
|
||||||
|
this.data != correctAnswer -> {
|
||||||
|
leftAttempts--
|
||||||
|
if (leftAttempts < 1) {
|
||||||
|
this
|
||||||
|
} else {
|
||||||
|
answerCallbackQuery(this@waitDataCallbackQuery, leftRetriesText + leftAttempts)
|
||||||
|
null
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else -> this
|
||||||
|
}
|
||||||
|
}.first()
|
||||||
|
|
||||||
|
banJob.cancel()
|
||||||
|
|
||||||
|
callback(leftAttempts > 0)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}.joinAll()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
@@ -1,18 +1,15 @@
|
|||||||
package dev.inmo.plagubot.plugins.captcha.settings
|
package dev.inmo.plagubot.plugins.captcha.settings
|
||||||
|
|
||||||
import com.soywiz.klock.TimeSpan
|
import dev.inmo.plagubot.plugins.captcha.provider.CaptchaProvider
|
||||||
|
import dev.inmo.plagubot.plugins.captcha.provider.SimpleCaptchaProvider
|
||||||
import dev.inmo.tgbotapi.types.ChatId
|
import dev.inmo.tgbotapi.types.ChatId
|
||||||
import dev.inmo.tgbotapi.types.Seconds
|
|
||||||
import kotlinx.serialization.Serializable
|
import kotlinx.serialization.Serializable
|
||||||
import kotlinx.serialization.Transient
|
|
||||||
|
|
||||||
@Serializable
|
@Serializable
|
||||||
data class ChatSettings(
|
data class ChatSettings(
|
||||||
val chatId: ChatId,
|
val chatId: ChatId,
|
||||||
val checkTime: Seconds = 60,
|
val captchaProvider: CaptchaProvider = SimpleCaptchaProvider(),
|
||||||
val captchaText: String = "solve next captcha:",
|
val autoRemoveCommands: Boolean = false,
|
||||||
val autoRemoveCommands: Boolean = false
|
val autoRemoveEvents: Boolean = true,
|
||||||
) {
|
val enabled: Boolean = true
|
||||||
@Transient
|
)
|
||||||
val checkTimeSpan = TimeSpan(checkTime * 1000.0)
|
|
||||||
}
|
|
||||||
|
|||||||
Reference in New Issue
Block a user