Skip to content

Commit

Permalink
refactor: выделен базовый хендлер для обработки новых участников чата
Browse files Browse the repository at this point in the history
  • Loading branch information
Djaler committed Jul 2, 2022
1 parent 5590088 commit 6fa04b4
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 60 deletions.
Original file line number Diff line number Diff line change
@@ -1,55 +1,39 @@
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

@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
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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 =
Expand Down
Original file line number Diff line number Diff line change
@@ -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
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -28,7 +27,7 @@ class DicePollCaptchaService(
fun fixRestriction(
chat: PublicChat,
member: User,
joinMessage: ChatEventMessage<*>,
joinMessage: Message,
diceMessage: ContentMessage<DiceContent>,
pollMessage: ContentMessage<PollContent>,
correctAnswerIndex: Int,
Expand Down

0 comments on commit 6fa04b4

Please sign in to comment.