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

272 lines
10 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-01-04 18:48:35 +00:00
import dev.inmo.tgbotapi.types.chat.RestrictionsChatPermissions
2021-02-23 06:00:44 +00:00
import dev.inmo.tgbotapi.types.chat.abstracts.Chat
2022-01-04 18:48:35 +00:00
import dev.inmo.tgbotapi.types.chat.abstracts.extended.ExtendedGroupChat
2021-02-17 13:24:28 +00:00
import kotlinx.coroutines.*
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"
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 {
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
),
2022-01-04 18:48:35 +00:00
BotCommand(
enableAutoDeleteServiceMessages,
"Enable auto removing of users joined messages"
),
BotCommand(
disableAutoDeleteServiceMessages,
"Disable auto removing of users joined messages"
),
2021-03-30 16:51:34 +00:00
BotCommand(
enableSlotMachineCaptcha,
"Change captcha method to slot machine"
),
BotCommand(
enableSimpleCaptcha,
"Change captcha method to simple button"
),
2022-01-04 18:48:35 +00:00
BotCommand(
disableCaptcha,
"Disable captcha for chat"
),
BotCommand(
enableCaptcha,
"Enable captcha for chat"
),
2021-03-30 16:51:34 +00:00
BotCommand(
enableExpressionCaptcha,
"Change captcha method to expressions"
2022-01-04 18:48:35 +00:00
),
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"
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(
2021-11-13 14:01:20 +00:00
initialFilter = {
2021-02-17 13:24:28 +00:00
it.chat.asPublicChat() != null
2021-02-17 18:27:18 +00:00
},
2021-11-13 14:01:20 +00:00
subcontextUpdatesFilter = { m, u -> u.sourceChat() == m.chat },
2021-02-17 13:24:28 +00:00
) {
2021-06-12 20:00:03 +00:00
launchSafelyWithoutExceptions {
2022-01-04 18:48:35 +00:00
val settings = it.chat.settings()
if (!settings.enabled) return@launchSafelyWithoutExceptions
safelyWithoutExceptions {
if (settings.autoRemoveEvents) {
deleteMessage(it)
}
}
2021-06-12 20:00:03 +00:00
val chat = it.chat.requireGroupChat()
val newUsers = it.chatEvent.members
newUsers.forEach { user ->
restrictChatMember(
chat,
user,
2022-01-04 18:48:35 +00:00
permissions = RestrictionsChatPermissions
2021-06-12 20:00:03 +00:00
)
}
2022-01-04 18:48:35 +00:00
val defaultChatPermissions = (getChat(it.chat) as ExtendedGroupChat).permissions
2021-06-12 20:00:03 +00:00
doInSubContext(stopOnCompletion = false) {
launch {
2022-01-04 18:48:35 +00:00
settings.captchaProvider.apply { doAction(it.date, chat, newUsers, defaultChatPermissions) }
2021-06-12 20:00:03 +00:00
}
}
}
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
}
}