diff --git a/src/main/kotlin/com/github/djaler/evilbot/handlers/CasCheckHandler.kt b/src/main/kotlin/com/github/djaler/evilbot/handlers/CasCheckHandler.kt index 31d076fe..88669a09 100644 --- a/src/main/kotlin/com/github/djaler/evilbot/handlers/CasCheckHandler.kt +++ b/src/main/kotlin/com/github/djaler/evilbot/handlers/CasCheckHandler.kt @@ -1,18 +1,15 @@ package com.github.djaler.evilbot.handlers import com.github.djaler.evilbot.clients.CasClient -import com.github.djaler.evilbot.handlers.base.MessageHandler +import com.github.djaler.evilbot.handlers.base.NewMemberHandler import com.github.djaler.evilbot.utils.userId import com.github.djaler.evilbot.utils.usernameOrName import dev.inmo.tgbotapi.bot.RequestsExecutor import dev.inmo.tgbotapi.extensions.api.chat.members.banChatMember import dev.inmo.tgbotapi.extensions.api.send.sendMessage -import dev.inmo.tgbotapi.extensions.utils.asChatEventMessage -import dev.inmo.tgbotapi.extensions.utils.asPublicChat import dev.inmo.tgbotapi.extensions.utils.formatting.buildEntities import dev.inmo.tgbotapi.extensions.utils.formatting.link -import dev.inmo.tgbotapi.types.chat.Bot -import dev.inmo.tgbotapi.types.message.ChatEvents.NewChatMembers +import dev.inmo.tgbotapi.types.chat.User import dev.inmo.tgbotapi.types.message.abstracts.Message import org.springframework.stereotype.Component @@ -20,36 +17,23 @@ import org.springframework.stereotype.Component class CasCheckHandler( private val casClient: CasClient, private val requestsExecutor: RequestsExecutor -) : MessageHandler() { +) : NewMemberHandler(allowBots = false) { override val order = 0 - override suspend fun handleMessage(message: Message): Boolean { - val chat = message.chat.asPublicChat() ?: return false - val newMembersEvent = message.asChatEventMessage()?.chatEvent as? NewChatMembers ?: return false - - var anyBlocked = false - - for (member in newMembersEvent.members) { - if (member is Bot) { - continue - } - - val casInfo = casClient.getCasInfo(member.id.userId) - - if (casInfo.result !== null) { - requestsExecutor.sendMessage( - chat.id, - buildEntities(separator = " ") { - +"${member.usernameOrName}, пошёл нахер!" - +"Ты забанен в" + link("CAS", "https://cas.chat/query?u=${member.id.userId}") - } - ) - requestsExecutor.banChatMember(chat.id, member.id) - - anyBlocked = true - } + override suspend fun handleNewMember(newMember: User, message: Message): Boolean { + val casInfo = casClient.getCasInfo(newMember.id.userId) + if (casInfo.result === null) { + return false } - return anyBlocked + requestsExecutor.sendMessage( + message.chat.id, + buildEntities(separator = " ") { + +"${newMember.usernameOrName}, пошёл нахер!" + +"Ты забанен в" + link("CAS", "https://cas.chat/query?u=${newMember.id.userId}") + } + ) + requestsExecutor.banChatMember(message.chat.id, newMember.id) + return true } } diff --git a/src/main/kotlin/com/github/djaler/evilbot/handlers/DicePollCaptcha.kt b/src/main/kotlin/com/github/djaler/evilbot/handlers/DicePollCaptcha.kt index 3f392ff5..8e02a1c1 100644 --- a/src/main/kotlin/com/github/djaler/evilbot/handlers/DicePollCaptcha.kt +++ b/src/main/kotlin/com/github/djaler/evilbot/handlers/DicePollCaptcha.kt @@ -4,7 +4,7 @@ import com.github.djaler.evilbot.components.TelegramMediaSender import com.github.djaler.evilbot.config.BotProperties import com.github.djaler.evilbot.entity.DicePollCaptchaRestriction import com.github.djaler.evilbot.filters.message.CanRestrictMemberMessageFilter -import com.github.djaler.evilbot.handlers.base.MessageHandler +import com.github.djaler.evilbot.handlers.base.NewMemberHandler import com.github.djaler.evilbot.handlers.base.PollAnswerHandler import com.github.djaler.evilbot.service.DicePollCaptchaService import com.github.djaler.evilbot.utils.chatPermissions @@ -23,8 +23,6 @@ import dev.inmo.tgbotapi.extensions.utils.asRestrictedChatMember import dev.inmo.tgbotapi.types.chat.* import dev.inmo.tgbotapi.types.dartsCubeAndBowlingDiceResultLimit import dev.inmo.tgbotapi.types.dice.CubeDiceAnimationType -import dev.inmo.tgbotapi.types.message.ChatEvents.NewChatMembers -import dev.inmo.tgbotapi.types.message.abstracts.ChatEventMessage import dev.inmo.tgbotapi.types.message.abstracts.Message import dev.inmo.tgbotapi.types.polls.PollAnswer import dev.inmo.tgbotapi.types.toChatId @@ -37,30 +35,15 @@ class DicePollCaptchaSendHandler( private val captchaService: DicePollCaptchaService, private val botProperties: BotProperties, canRestrictMemberFilter: CanRestrictMemberMessageFilter -) : MessageHandler(filter = canRestrictMemberFilter) { - - override suspend fun handleMessage(message: Message): Boolean { - if (message !is ChatEventMessage<*>) { - return false - } +) : NewMemberHandler(allowBots = false, filter = canRestrictMemberFilter) { + override suspend fun handleNewMember(newMember: User, message: Message): Boolean { val chat = message.chat.asGroupChat() ?: return false - val newMembersEvent = message.chatEvent as? NewChatMembers ?: return false - - var anyUser = false - - val newMembers = newMembersEvent.members.filter { it !is Bot } - - if (newMembers.isEmpty()) { - return false - } - newMembers.forEach { member -> - val previousRestriction = captchaService.getRestriction(chat.id, member.id) - if (previousRestriction != null) { - forwardPreviousCaptcha(chat, previousRestriction) - } else { - createCaptcha(chat, member, message) - } + val previousRestriction = captchaService.getRestriction(chat.id, newMember.id) + if (previousRestriction != null) { + forwardPreviousCaptcha(chat, previousRestriction) + } else { + createCaptcha(chat, newMember, message) } return true @@ -74,7 +57,7 @@ class DicePollCaptchaSendHandler( captchaService.updateRestriction(previousRestriction, newDiceMessage, newPollMessage) } - private suspend fun createCaptcha(chat: GroupChat, member: User, message: ChatEventMessage<*>) { + private suspend fun createCaptcha(chat: GroupChat, member: User, message: Message) { val chatMember = requestsExecutor.getChatMember(chat.id, member.id) val originalPermissions = diff --git a/src/main/kotlin/com/github/djaler/evilbot/handlers/base/NewMemberHandler.kt b/src/main/kotlin/com/github/djaler/evilbot/handlers/base/NewMemberHandler.kt new file mode 100644 index 00000000..59e0631e --- /dev/null +++ b/src/main/kotlin/com/github/djaler/evilbot/handlers/base/NewMemberHandler.kt @@ -0,0 +1,36 @@ +package com.github.djaler.evilbot.handlers.base + +import com.github.djaler.evilbot.filters.message.MessageFilter +import dev.inmo.tgbotapi.extensions.utils.asChatEventMessage +import dev.inmo.tgbotapi.types.chat.Bot +import dev.inmo.tgbotapi.types.chat.User +import dev.inmo.tgbotapi.types.message.ChatEvents.NewChatMembers +import dev.inmo.tgbotapi.types.message.abstracts.Message + +abstract class NewMemberHandler( + private val allowBots: Boolean = true, + filter: MessageFilter? = null +) : MessageHandler(filter) { + override suspend fun handleMessage(message: Message): Boolean { + val newMembersEvent = message.asChatEventMessage()?.chatEvent as? NewChatMembers ?: return false + + val newMembers = newMembersEvent.members + + var anyHandled = false + + for (newMember in newMembers) { + if (newMember is Bot && !allowBots) { + continue + } + + val handled = handleNewMember(newMember, message) + if (handled) { + anyHandled = true + } + } + + return anyHandled + } + + abstract suspend fun handleNewMember(newMember: User, message: Message): Boolean +} diff --git a/src/main/kotlin/com/github/djaler/evilbot/service/DicePollCaptchaService.kt b/src/main/kotlin/com/github/djaler/evilbot/service/DicePollCaptchaService.kt index a6c0318a..7131eb00 100644 --- a/src/main/kotlin/com/github/djaler/evilbot/service/DicePollCaptchaService.kt +++ b/src/main/kotlin/com/github/djaler/evilbot/service/DicePollCaptchaService.kt @@ -10,7 +10,6 @@ import dev.inmo.tgbotapi.types.UserId import dev.inmo.tgbotapi.types.chat.ChatPermissions import dev.inmo.tgbotapi.types.chat.PublicChat import dev.inmo.tgbotapi.types.chat.User -import dev.inmo.tgbotapi.types.message.abstracts.ChatEventMessage import dev.inmo.tgbotapi.types.message.abstracts.ContentMessage import dev.inmo.tgbotapi.types.message.abstracts.Message import dev.inmo.tgbotapi.types.message.content.DiceContent @@ -28,7 +27,7 @@ class DicePollCaptchaService( fun fixRestriction( chat: PublicChat, member: User, - joinMessage: ChatEventMessage<*>, + joinMessage: Message, diceMessage: ContentMessage, pollMessage: ContentMessage, correctAnswerIndex: Int,