-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: выделен базовый хендлер для обработки новых участников чата
- Loading branch information
Showing
4 changed files
with
62 additions
and
60 deletions.
There are no files selected for viewing
48 changes: 16 additions & 32 deletions
48
src/main/kotlin/com/github/djaler/evilbot/handlers/CasCheckHandler.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
36 changes: 36 additions & 0 deletions
36
src/main/kotlin/com/github/djaler/evilbot/handlers/base/NewMemberHandler.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters