2021-02-17 13:24:28 +00:00
|
|
|
package dev.inmo.plagubot.plugins.captcha
|
|
|
|
|
2021-02-17 18:27:18 +00:00
|
|
|
import dev.inmo.micro_utils.coroutines.*
|
2021-02-17 13:24:28 +00:00
|
|
|
import dev.inmo.micro_utils.repos.create
|
2021-06-12 19:35:47 +00:00
|
|
|
import dev.inmo.micro_utils.repos.exposed.keyvalue.ExposedKeyValueRepo
|
|
|
|
import dev.inmo.micro_utils.repos.versions.KeyValueBasedVersionsRepoProxy
|
|
|
|
import dev.inmo.micro_utils.repos.versions.StandardVersionsRepo
|
2021-02-17 13:24:28 +00:00
|
|
|
import dev.inmo.plagubot.Plugin
|
|
|
|
import dev.inmo.plagubot.plugins.captcha.db.CaptchaChatsSettingsRepo
|
2021-03-30 16:51:34 +00:00
|
|
|
import dev.inmo.plagubot.plugins.captcha.provider.*
|
2021-02-17 13:24:28 +00:00
|
|
|
import dev.inmo.plagubot.plugins.captcha.settings.ChatSettings
|
|
|
|
import dev.inmo.tgbotapi.bot.TelegramBot
|
|
|
|
import dev.inmo.tgbotapi.extensions.api.answers.answerCallbackQuery
|
|
|
|
import dev.inmo.tgbotapi.extensions.api.chat.members.*
|
2021-02-17 18:27:18 +00:00
|
|
|
import dev.inmo.tgbotapi.extensions.api.deleteMessage
|
|
|
|
import dev.inmo.tgbotapi.extensions.api.edit.ReplyMarkup.editMessageReplyMarkup
|
2021-03-30 16:51:34 +00:00
|
|
|
import dev.inmo.tgbotapi.extensions.api.send.*
|
2021-02-17 13:24:28 +00:00
|
|
|
import dev.inmo.tgbotapi.extensions.api.send.media.reply
|
|
|
|
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
|
2021-02-23 06:00:44 +00:00
|
|
|
import dev.inmo.tgbotapi.extensions.behaviour_builder.triggers_handling.onCommand
|
2021-02-17 13:24:28 +00:00
|
|
|
import dev.inmo.tgbotapi.updateshandlers.FlowsUpdatesFilter
|
|
|
|
import dev.inmo.tgbotapi.extensions.behaviour_builder.triggers_handling.onNewChatMembers
|
|
|
|
import dev.inmo.tgbotapi.extensions.utils.*
|
2021-03-30 16:51:34 +00:00
|
|
|
import dev.inmo.tgbotapi.extensions.utils.extensions.parseCommandsWithParams
|
2021-02-17 13:24:28 +00:00
|
|
|
import dev.inmo.tgbotapi.extensions.utils.formatting.buildEntities
|
|
|
|
import dev.inmo.tgbotapi.extensions.utils.formatting.regular
|
2021-02-17 18:27:18 +00:00
|
|
|
import dev.inmo.tgbotapi.extensions.utils.shortcuts.executeUnsafe
|
2021-02-23 07:29:17 +00:00
|
|
|
import dev.inmo.tgbotapi.libraries.cache.admins.*
|
2021-02-17 18:27:18 +00:00
|
|
|
import dev.inmo.tgbotapi.requests.DeleteMessage
|
2021-02-23 06:00:44 +00:00
|
|
|
import dev.inmo.tgbotapi.types.BotCommand
|
2021-02-17 13:24:28 +00:00
|
|
|
import dev.inmo.tgbotapi.types.MessageEntity.textsources.mention
|
|
|
|
import dev.inmo.tgbotapi.types.User
|
|
|
|
import dev.inmo.tgbotapi.types.chat.ChatPermissions
|
2021-02-17 18:27:18 +00:00
|
|
|
import dev.inmo.tgbotapi.types.chat.LeftRestrictionsChatPermissions
|
2021-02-23 06:00:44 +00:00
|
|
|
import dev.inmo.tgbotapi.types.chat.abstracts.Chat
|
|
|
|
import dev.inmo.tgbotapi.types.chat.abstracts.PublicChat
|
2021-02-17 13:24:28 +00:00
|
|
|
import dev.inmo.tgbotapi.types.dice.SlotMachineDiceAnimationType
|
2021-02-23 06:00:44 +00:00
|
|
|
import dev.inmo.tgbotapi.types.message.abstracts.*
|
|
|
|
import dev.inmo.tgbotapi.types.message.content.abstracts.MessageContent
|
2021-02-17 13:24:28 +00:00
|
|
|
import kotlinx.coroutines.*
|
|
|
|
import kotlinx.coroutines.channels.Channel
|
|
|
|
import kotlinx.coroutines.channels.toList
|
|
|
|
import kotlinx.serialization.Serializable
|
|
|
|
import org.jetbrains.exposed.sql.Database
|
|
|
|
|
2021-02-23 06:00:44 +00:00
|
|
|
private const val enableAutoDeleteCommands = "captcha_auto_delete_commands_on"
|
|
|
|
private const val disableAutoDeleteCommands = "captcha_auto_delete_commands_off"
|
|
|
|
|
2021-03-30 16:51:34 +00:00
|
|
|
private const val enableSlotMachineCaptcha = "captcha_use_slot_machine"
|
|
|
|
private const val enableSimpleCaptcha = "captcha_use_simple"
|
|
|
|
private const val enableExpressionCaptcha = "captcha_use_expression"
|
|
|
|
|
|
|
|
private val changeCaptchaMethodCommandRegex = Regex(
|
2021-03-30 18:02:18 +00:00
|
|
|
"captcha_use_((slot_machine)|(simple)|(expression))"
|
2021-03-30 16:51:34 +00:00
|
|
|
)
|
|
|
|
|
2021-02-17 13:24:28 +00:00
|
|
|
@Serializable
|
|
|
|
class CaptchaBotPlugin : Plugin {
|
2021-02-23 06:00:44 +00:00
|
|
|
override suspend fun getCommands(): List<BotCommand> = listOf(
|
|
|
|
BotCommand(
|
|
|
|
enableAutoDeleteCommands,
|
|
|
|
"Enable auto removing of commands addressed to captcha plugin"
|
|
|
|
),
|
|
|
|
BotCommand(
|
|
|
|
disableAutoDeleteCommands,
|
|
|
|
"Disable auto removing of commands addressed to captcha plugin"
|
2021-03-30 16:51:34 +00:00
|
|
|
),
|
|
|
|
BotCommand(
|
|
|
|
enableSlotMachineCaptcha,
|
|
|
|
"Change captcha method to slot machine"
|
|
|
|
),
|
|
|
|
BotCommand(
|
|
|
|
enableSimpleCaptcha,
|
|
|
|
"Change captcha method to simple button"
|
|
|
|
),
|
|
|
|
BotCommand(
|
|
|
|
enableExpressionCaptcha,
|
|
|
|
"Change captcha method to expressions"
|
2021-02-23 06:00:44 +00:00
|
|
|
)
|
|
|
|
)
|
|
|
|
|
2021-02-17 13:24:28 +00:00
|
|
|
override suspend fun BehaviourContext.invoke(
|
|
|
|
database: Database,
|
|
|
|
params: Map<String, Any>
|
|
|
|
) {
|
|
|
|
val repo = CaptchaChatsSettingsRepo(database)
|
2021-02-23 06:00:44 +00:00
|
|
|
val adminsAPI = params.adminsPlugin ?.adminsAPI(database)
|
|
|
|
suspend fun Chat.settings() = repo.getById(id) ?: repo.create(ChatSettings(id)).first()
|
|
|
|
|
2021-02-17 13:24:28 +00:00
|
|
|
onNewChatMembers(
|
|
|
|
additionalFilter = {
|
|
|
|
it.chat.asPublicChat() != null
|
2021-02-17 18:27:18 +00:00
|
|
|
},
|
|
|
|
includeFilterByChatInBehaviourSubContext = false
|
2021-02-17 13:24:28 +00:00
|
|
|
) {
|
2021-02-17 18:27:18 +00:00
|
|
|
safelyWithoutExceptions { deleteMessage(it) }
|
2021-03-25 00:53:42 +00:00
|
|
|
val chat = it.chat.requireGroupChat()
|
2021-02-17 13:24:28 +00:00
|
|
|
val newUsers = it.chatEvent.members
|
|
|
|
newUsers.forEach { user ->
|
|
|
|
restrictChatMember(
|
|
|
|
chat,
|
|
|
|
user,
|
|
|
|
permissions = ChatPermissions()
|
|
|
|
)
|
|
|
|
}
|
2021-03-25 00:53:42 +00:00
|
|
|
val settings = it.chat.settings()
|
2021-04-03 08:22:01 +00:00
|
|
|
doInSubContext(stopOnCompletion = false) {
|
|
|
|
launch {
|
|
|
|
settings.captchaProvider.apply { doAction(it.date, chat, newUsers) }
|
|
|
|
}
|
|
|
|
}
|
2021-02-17 13:24:28 +00:00
|
|
|
}
|
2021-02-23 06:00:44 +00:00
|
|
|
|
|
|
|
if (adminsAPI != null) {
|
2021-03-30 16:51:34 +00:00
|
|
|
onCommand(changeCaptchaMethodCommandRegex) {
|
|
|
|
if (adminsAPI.sentByAdmin(it) != true) {
|
|
|
|
return@onCommand
|
|
|
|
}
|
2021-03-30 18:02:18 +00:00
|
|
|
|
|
|
|
val settings = it.chat.settings()
|
2021-03-30 16:51:34 +00:00
|
|
|
if (settings.autoRemoveCommands) {
|
|
|
|
safelyWithoutExceptions { deleteMessage(it) }
|
|
|
|
}
|
|
|
|
val commands = it.parseCommandsWithParams()
|
|
|
|
val changeCommand = commands.keys.first {
|
2021-03-30 18:02:18 +00:00
|
|
|
println(it)
|
2021-03-30 16:51:34 +00:00
|
|
|
changeCaptchaMethodCommandRegex.matches(it)
|
|
|
|
}
|
2021-03-30 18:02:18 +00:00
|
|
|
println(changeCommand)
|
|
|
|
val captcha = when {
|
|
|
|
changeCommand.startsWith(enableSimpleCaptcha) -> SimpleCaptchaProvider()
|
|
|
|
changeCommand.startsWith(enableExpressionCaptcha) -> ExpressionCaptchaProvider()
|
|
|
|
changeCommand.startsWith(enableSlotMachineCaptcha) -> SlotMachineCaptchaProvider()
|
2021-03-30 16:51:34 +00:00
|
|
|
else -> return@onCommand
|
|
|
|
}
|
|
|
|
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)
|
|
|
|
deleteMessage(sent)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-23 06:00:44 +00:00
|
|
|
onCommand(
|
|
|
|
enableAutoDeleteCommands,
|
|
|
|
requireOnlyCommandInMessage = false
|
|
|
|
) { message ->
|
2021-02-23 07:29:17 +00:00
|
|
|
message.doAfterVerification(adminsAPI) {
|
2021-02-23 06:00:44 +00:00
|
|
|
val settings = message.chat.settings()
|
|
|
|
|
|
|
|
repo.update(
|
|
|
|
message.chat.id,
|
|
|
|
settings.copy(autoRemoveCommands = true)
|
|
|
|
)
|
|
|
|
|
|
|
|
deleteMessage(message)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
onCommand(
|
|
|
|
disableAutoDeleteCommands,
|
|
|
|
requireOnlyCommandInMessage = false
|
|
|
|
) { message ->
|
2021-02-23 07:29:17 +00:00
|
|
|
message.doAfterVerification(adminsAPI) {
|
2021-02-23 06:00:44 +00:00
|
|
|
val settings = message.chat.settings()
|
|
|
|
|
|
|
|
repo.update(
|
|
|
|
message.chat.id,
|
|
|
|
settings.copy(autoRemoveCommands = false)
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-02-17 13:24:28 +00:00
|
|
|
}
|
|
|
|
}
|