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

339 lines
13 KiB
Kotlin
Raw Normal View History

2021-02-17 13:24:28 +00:00
package dev.inmo.plagubot.plugins.captcha
2022-07-30 16:57:59 +00:00
import com.benasher44.uuid.uuid4
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-07-30 16:57:59 +00:00
import dev.inmo.plagubot.plugins.commands.BotCommandFullInfo
import dev.inmo.plagubot.plugins.commands.CommandsKeeperKey
2021-02-17 13:24:28 +00:00
import dev.inmo.tgbotapi.extensions.api.chat.members.*
import dev.inmo.tgbotapi.extensions.api.delete
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-02-23 07:29:17 +00:00
import dev.inmo.tgbotapi.libraries.cache.admins.*
2022-07-30 16:57:59 +00:00
import dev.inmo.tgbotapi.types.BotCommand
2022-05-18 08:27:29 +00:00
import dev.inmo.tgbotapi.types.chat.*
2022-07-30 16:57:59 +00:00
import dev.inmo.tgbotapi.types.commands.BotCommandScope
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
2022-07-30 16:57:59 +00:00
import org.koin.core.qualifier.named
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 fun Module.setupDI(database: Database, params: JsonObject) {
single { CaptchaChatsSettingsRepo(database) }
2022-07-30 16:57:59 +00:00
single(named(uuid4().toString())) {
BotCommandFullInfo(
CommandsKeeperKey(BotCommandScope.AllChatAdministrators),
BotCommand(enableAutoDeleteCommands, "Enable auto removing of commands addressed to captcha plugin")
)
}
single(named(uuid4().toString())) {
BotCommandFullInfo(
CommandsKeeperKey(BotCommandScope.AllChatAdministrators),
BotCommand(disableAutoDeleteCommands, "Disable auto removing of commands addressed to captcha plugin")
)
}
single(named(uuid4().toString())) {
BotCommandFullInfo(
CommandsKeeperKey(BotCommandScope.AllChatAdministrators),
BotCommand(enableAutoDeleteServiceMessages, "Enable auto removing of users joined messages")
)
}
single(named(uuid4().toString())) {
BotCommandFullInfo(
CommandsKeeperKey(BotCommandScope.AllChatAdministrators),
BotCommand(disableAutoDeleteServiceMessages, "Disable auto removing of users joined messages")
)
}
single(named(uuid4().toString())) {
BotCommandFullInfo(
CommandsKeeperKey(BotCommandScope.AllChatAdministrators),
BotCommand(enableSlotMachineCaptcha, "Change captcha method to slot machine")
)
}
single(named(uuid4().toString())) {
BotCommandFullInfo(
CommandsKeeperKey(BotCommandScope.AllChatAdministrators),
BotCommand(enableSimpleCaptcha, "Change captcha method to simple button")
)
}
single(named(uuid4().toString())) {
BotCommandFullInfo(
CommandsKeeperKey(BotCommandScope.AllChatAdministrators),
BotCommand(disableCaptcha, "Disable captcha for chat")
)
}
single(named(uuid4().toString())) {
BotCommandFullInfo(
CommandsKeeperKey(BotCommandScope.AllChatAdministrators),
BotCommand(enableCaptcha, "Enable captcha for chat")
)
}
single(named(uuid4().toString())) {
BotCommandFullInfo(
CommandsKeeperKey(BotCommandScope.AllChatAdministrators),
BotCommand(enableExpressionCaptcha, "Change captcha method to expressions")
)
}
single(named(uuid4().toString())) {
BotCommandFullInfo(
CommandsKeeperKey(BotCommandScope.AllChatAdministrators),
BotCommand(enableKickOnUnsuccess, "Not solved captcha users will be kicked from the chat")
)
}
single(named(uuid4().toString())) {
BotCommandFullInfo(
CommandsKeeperKey(BotCommandScope.AllChatAdministrators),
BotCommand(disableKickOnUnsuccess, "Not solved captcha users will NOT be kicked from the chat")
)
}
2022-05-18 08:22:03 +00:00
}
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 = LeftRestrictionsChatPermissions
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, adminsAPI, settings.kickOnUnsuccess)
}
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)
}
}
}
onCommand(enableKickOnUnsuccess) { message ->
message.doAfterVerification(adminsAPI) {
val settings = message.chat.settings()
repo.update(
message.chat.id,
settings.copy(kickOnUnsuccess = true)
)
reply(message, "Ok, new users didn't passed captcha will be kicked").apply {
launchSafelyWithoutExceptions {
delay(5000L)
delete(this@apply)
}
}
if (settings.autoRemoveCommands) {
deleteMessage(message)
}
}
}
onCommand(disableKickOnUnsuccess) { message ->
message.doAfterVerification(adminsAPI) {
val settings = message.chat.settings()
repo.update(
message.chat.id,
settings.copy(kickOnUnsuccess = false)
)
reply(message, "Ok, new users didn't passed captcha will NOT be kicked").apply {
launchSafelyWithoutExceptions {
delay(5000L)
delete(this@apply)
}
}
if (settings.autoRemoveCommands) {
deleteMessage(message)
}
}
}
2021-02-23 06:00:44 +00:00
}
2021-02-17 13:24:28 +00:00
}
}