From 9561ee1dbeaeee117af6a20fb188ae06fab265f2 Mon Sep 17 00:00:00 2001 From: InsanusMokrassar Date: Tue, 23 Feb 2021 12:00:44 +0600 Subject: [PATCH] including of admins --- build.gradle | 1 + gradle.properties | 7 +- .../inmo/plagubot/plugins/captcha/Plugin.kt | 75 ++++++++++++++++++- .../captcha/db/CaptchaChatsSettingsRepo.kt | 9 ++- .../plugins/captcha/settings/ChatSettings.kt | 3 +- 5 files changed, 87 insertions(+), 8 deletions(-) diff --git a/build.gradle b/build.gradle index 22c29b2..57995b6 100644 --- a/build.gradle +++ b/build.gradle @@ -32,4 +32,5 @@ dependencies { api "dev.inmo:plagubot.plugin:$plagubot_version" api "dev.inmo:micro_utils.repos.exposed:$micro_utils_version" + api "dev.inmo:tgbotapi.libraries.cache.admins.plagubot:$tgbotapi_libraries_version" } diff --git a/gradle.properties b/gradle.properties index 938d351..559eee8 100644 --- a/gradle.properties +++ b/gradle.properties @@ -6,10 +6,11 @@ kotlin.incremental=true kotlin_version=1.4.30 kotlin_coroutines_version=1.4.2 -kotlin_serialisation_runtime_version=1.1.0-RC -plagubot_version=0.1.3 +kotlin_serialisation_runtime_version=1.1.0 +plagubot_version=0.1.4 -micro_utils_version=0.4.25 +micro_utils_version=0.4.26 +tgbotapi_libraries_version=0.0.1 project_group=dev.inmo project_version=0.1.4 diff --git a/src/main/kotlin/dev/inmo/plagubot/plugins/captcha/Plugin.kt b/src/main/kotlin/dev/inmo/plagubot/plugins/captcha/Plugin.kt index 09db1bf..30e2004 100644 --- a/src/main/kotlin/dev/inmo/plagubot/plugins/captcha/Plugin.kt +++ b/src/main/kotlin/dev/inmo/plagubot/plugins/captcha/Plugin.kt @@ -16,32 +16,62 @@ import dev.inmo.tgbotapi.extensions.api.send.sendTextMessage 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.updateshandlers.FlowsUpdatesFilter import dev.inmo.tgbotapi.extensions.behaviour_builder.triggers_handling.onNewChatMembers import dev.inmo.tgbotapi.extensions.utils.* import dev.inmo.tgbotapi.extensions.utils.formatting.buildEntities import dev.inmo.tgbotapi.extensions.utils.formatting.regular import dev.inmo.tgbotapi.extensions.utils.shortcuts.executeUnsafe +import dev.inmo.tgbotapi.libraries.cache.admins.AdminsCacheAPI +import dev.inmo.tgbotapi.libraries.cache.admins.adminsPlugin import dev.inmo.tgbotapi.requests.DeleteMessage +import dev.inmo.tgbotapi.types.BotCommand import dev.inmo.tgbotapi.types.MessageEntity.textsources.mention 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.Message +import dev.inmo.tgbotapi.types.message.abstracts.* +import dev.inmo.tgbotapi.types.message.content.abstracts.MessageContent import kotlinx.coroutines.* import kotlinx.coroutines.channels.Channel import kotlinx.coroutines.channels.toList import kotlinx.serialization.Serializable import org.jetbrains.exposed.sql.Database +private const val enableAutoDeleteCommands = "captcha_auto_delete_commands_on" +private const val disableAutoDeleteCommands = "captcha_auto_delete_commands_off" + +private suspend fun AdminsCacheAPI.verifyMessageFromAdmin(message: CommonMessage<*>) = when (message) { + is CommonGroupContentMessage<*> -> getChatAdmins(message.chat.id) ?.any { it.user.id == message.user.id } == true + is AnonymousGroupContentMessage<*> -> true + else -> false +} + @Serializable class CaptchaBotPlugin : Plugin { + override suspend fun getCommands(): List = listOf( + BotCommand( + enableAutoDeleteCommands, + "Enable auto removing of commands addressed to captcha plugin" + ), + BotCommand( + disableAutoDeleteCommands, + "Disable auto removing of commands addressed to captcha plugin" + ) + ) + override suspend fun BehaviourContext.invoke( database: Database, params: Map ) { val repo = CaptchaChatsSettingsRepo(database) + val adminsAPI = params.adminsPlugin ?.adminsAPI(database) + suspend fun Chat.settings() = repo.getById(id) ?: repo.create(ChatSettings(id)).first() + onNewChatMembers( additionalFilter = { it.chat.asPublicChat() != null @@ -59,7 +89,7 @@ class CaptchaBotPlugin : Plugin { permissions = ChatPermissions() ) } - val settings = repo.getById(it.chat.id) ?: repo.create(ChatSettings(it.chat.id)).firstOrNull() ?: return@onNewChatMembers + val settings = it.chat.settings() ?: return@onNewChatMembers val userBanDateTime = eventDateTime + settings.checkTimeSpan val authorized = Channel(newUsers.size) val messagesToDelete = Channel(Channel.UNLIMITED) @@ -128,5 +158,46 @@ class CaptchaBotPlugin : Plugin { executeUnsafe(DeleteMessage(message.chat.id, message.messageId), retries = 0) } } + + if (adminsAPI != null) { + suspend fun CommonMessage.doAfterVerification(block: suspend () -> Unit) { + val chat = chat + + if (chat is PublicChat) { + val verified = adminsAPI.verifyMessageFromAdmin(this) + if (verified) { + block() + } + } + } + onCommand( + enableAutoDeleteCommands, + requireOnlyCommandInMessage = false + ) { message -> + message.doAfterVerification { + val settings = message.chat.settings() + + repo.update( + message.chat.id, + settings.copy(autoRemoveCommands = true) + ) + + deleteMessage(message) + } + } + onCommand( + disableAutoDeleteCommands, + requireOnlyCommandInMessage = false + ) { message -> + message.doAfterVerification { + val settings = message.chat.settings() + + repo.update( + message.chat.id, + settings.copy(autoRemoveCommands = false) + ) + } + } + } } } diff --git a/src/main/kotlin/dev/inmo/plagubot/plugins/captcha/db/CaptchaChatsSettingsRepo.kt b/src/main/kotlin/dev/inmo/plagubot/plugins/captcha/db/CaptchaChatsSettingsRepo.kt index 44c631d..6985139 100644 --- a/src/main/kotlin/dev/inmo/plagubot/plugins/captcha/db/CaptchaChatsSettingsRepo.kt +++ b/src/main/kotlin/dev/inmo/plagubot/plugins/captcha/db/CaptchaChatsSettingsRepo.kt @@ -17,6 +17,7 @@ class CaptchaChatsSettingsRepo( private val chatIdColumn = long("chatId") private val checkTimeSecondsColumn = integer("checkTime") private val solveCaptchaTextColumn = text("solveCaptchaText") + private val autoRemoveCommandsColumn = bool("autoRemoveCommands") override val primaryKey = PrimaryKey(chatIdColumn) @@ -30,19 +31,22 @@ class CaptchaChatsSettingsRepo( it[chatIdColumn] = value.chatId.chatId it[checkTimeSecondsColumn] = value.checkTime it[solveCaptchaTextColumn] = value.captchaText + it[autoRemoveCommandsColumn] = value.autoRemoveCommands } override fun update(id: ChatId, value: ChatSettings, it: UpdateStatement) { if (id.chatId == value.chatId.chatId) { it[checkTimeSecondsColumn] = value.checkTime it[solveCaptchaTextColumn] = value.captchaText + it[autoRemoveCommandsColumn] = value.autoRemoveCommands } } override fun InsertStatement.asObject(value: ChatSettings): ChatSettings = ChatSettings( get(chatIdColumn).toChatId(), get(checkTimeSecondsColumn), - get(solveCaptchaTextColumn) + get(solveCaptchaTextColumn), + get(autoRemoveCommandsColumn) ) override val selectById: SqlExpressionBuilder.(ChatId) -> Op = { chatIdColumn.eq(it.chatId) } @@ -50,7 +54,8 @@ class CaptchaChatsSettingsRepo( get() = ChatSettings( get(chatIdColumn).toChatId(), get(checkTimeSecondsColumn), - get(solveCaptchaTextColumn) + get(solveCaptchaTextColumn), + get(autoRemoveCommandsColumn) ) init { diff --git a/src/main/kotlin/dev/inmo/plagubot/plugins/captcha/settings/ChatSettings.kt b/src/main/kotlin/dev/inmo/plagubot/plugins/captcha/settings/ChatSettings.kt index 69d1334..a46ac09 100644 --- a/src/main/kotlin/dev/inmo/plagubot/plugins/captcha/settings/ChatSettings.kt +++ b/src/main/kotlin/dev/inmo/plagubot/plugins/captcha/settings/ChatSettings.kt @@ -10,7 +10,8 @@ import kotlinx.serialization.Transient data class ChatSettings( val chatId: ChatId, val checkTime: Seconds = 60, - val captchaText: String = "solve next captcha:" + val captchaText: String = "solve next captcha:", + val autoRemoveCommands: Boolean = false ) { @Transient val checkTimeSpan = TimeSpan(checkTime * 1000.0)