CaptchaPlaguBotPlugin/src/main/kotlin/dev/inmo/plagubot/plugins/captcha/Plugin.kt

270 lines
9.9 KiB
Kotlin
Raw Normal View History

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
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
2022-01-04 18:48:35 +00:00
import dev.inmo.tgbotapi.extensions.api.chat.get.getChat
2021-02-17 13:24:28 +00:00
import dev.inmo.tgbotapi.extensions.api.chat.members.*
2021-02-17 18:27:18 +00:00
import dev.inmo.tgbotapi.extensions.api.deleteMessage
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.behaviour_builder.*
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.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-11-13 14:01:20 +00:00
import dev.inmo.tgbotapi.extensions.utils.extensions.sourceChat
2021-02-23 07:29:17 +00:00
import dev.inmo.tgbotapi.libraries.cache.admins.*
2021-02-23 06:00:44 +00:00
import dev.inmo.tgbotapi.types.BotCommand
2022-05-18 08:27:29 +00:00
import dev.inmo.tgbotapi.types.chat.*
2021-02-17 13:24:28 +00:00
import kotlinx.coroutines.*
import kotlinx.serialization.Serializable
2022-05-18 08:22:03 +00:00
import kotlinx.serialization.json.JsonObject
2021-02-17 13:24:28 +00:00
import org.jetbrains.exposed.sql.Database
2022-05-18 08:22:03 +00:00
import org.koin.core.Koin
import org.koin.core.module.Module
2021-02-17 13:24:28 +00:00
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"
2022-01-04 18:48:35 +00:00
private const val enableAutoDeleteServiceMessages = "captcha_auto_delete_events_on"
private const val disableAutoDeleteServiceMessages = "captcha_auto_delete_events_off"
2021-02-23 06:00:44 +00:00
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"
2022-01-04 18:48:35 +00:00
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"
2021-03-30 16:51:34 +00:00
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 {
2022-05-18 08:22:03 +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"
// ),
// 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())
2021-02-23 06:00:44 +00:00
suspend fun Chat.settings() = repo.getById(id) ?: repo.create(ChatSettings(id)).first()
2021-02-17 13:24:28 +00:00
onNewChatMembers(
2021-11-13 14:01:20 +00:00
initialFilter = {
2022-05-21 11:31:52 +00:00
it.chat is GroupChat
}
2021-02-17 13:24:28 +00:00
) {
2022-05-21 11:31:52 +00:00
val settings = it.chat.settings()
if (!settings.enabled) return@onNewChatMembers
2022-01-04 18:48:35 +00:00
2022-05-21 11:31:52 +00:00
safelyWithoutExceptions {
if (settings.autoRemoveEvents) {
deleteMessage(it)
2022-01-04 18:48:35 +00:00
}
2022-05-21 11:31:52 +00:00
}
val chat = it.chat.requireGroupChat()
val newUsers = it.chatEvent.members
newUsers.forEach { user ->
restrictChatMember(
chat,
user,
permissions = RestrictionsChatPermissions
)
}
val defaultChatPermissions = (getChat(it.chat) as ExtendedGroupChat).permissions
2022-01-04 18:48:35 +00:00
2022-05-21 11:31:52 +00:00
with (settings.captchaProvider) {
doAction(it.date, chat, newUsers, defaultChatPermissions)
}
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) {
2022-01-04 18:48:35 +00:00
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)
}
2022-01-04 18:48:35 +00:00
}
2021-03-30 16:51:34 +00:00
}
}
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)
)
}
}
2022-01-04 18:48:35 +00:00
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")
2022-01-04 18:48:35 +00:00
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")
2022-01-04 18:48:35 +00:00
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")
2022-01-04 18:48:35 +00:00
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")
2022-01-04 18:48:35 +00:00
if (settings.autoRemoveCommands) {
deleteMessage(message)
}
}
}
2021-02-23 06:00:44 +00:00
}
2021-02-17 13:24:28 +00:00
}
}