add admins cache api usage

This commit is contained in:
InsanusMokrassar 2022-09-22 15:00:40 +06:00
parent 6cefafbd09
commit 0f77475434
4 changed files with 12 additions and 16 deletions

View File

@ -4,6 +4,7 @@ kotlin = "1.7.10"
plagubot = "2.3.3" plagubot = "2.3.3"
kslog = "0.5.2" kslog = "0.5.2"
plagubot-commands = "0.3.4" plagubot-commands = "0.3.4"
tgbotapi-libraries = "0.5.4"
[libraries] [libraries]
@ -11,6 +12,7 @@ kotlin = { module = "org.jetbrains.kotlin:kotlin-stdlib", version.ref = "kotlin"
plagubot = { module = "dev.inmo:plagubot.bot", version.ref = "plagubot" } plagubot = { module = "dev.inmo:plagubot.bot", version.ref = "plagubot" }
plagubot-plugin = { module = "dev.inmo:plagubot.plugin", version.ref = "plagubot" } plagubot-plugin = { module = "dev.inmo:plagubot.plugin", version.ref = "plagubot" }
plagubot-commands = { module = "dev.inmo:plagubot.plugins.commands", version.ref = "plagubot-commands" } plagubot-commands = { module = "dev.inmo:plagubot.plugins.commands", version.ref = "plagubot-commands" }
tgbotapi-libraries-admins = { module = "dev.inmo:tgbotapi.libraries.cache.admins.plagubot", version.ref = "tgbotapi-libraries" }
kslog = { module = "dev.inmo:kslog", version.ref = "kslog" } kslog = { module = "dev.inmo:kslog", version.ref = "kslog" }
# Libs for classpath # Libs for classpath

View File

@ -24,4 +24,5 @@ dependencies {
api libs.plagubot.plugin api libs.plagubot.plugin
api libs.kslog api libs.kslog
api libs.plagubot.commands api libs.plagubot.commands
api libs.tgbotapi.libraries.admins
} }

View File

@ -1,11 +0,0 @@
import dev.inmo.tgbotapi.bot.TelegramBot
import dev.inmo.tgbotapi.extensions.api.chat.get.getChatAdministrators
import dev.inmo.tgbotapi.types.chat.GroupChat
import dev.inmo.tgbotapi.types.chat.User
suspend fun TelegramBot.userIsAdmin(user: User, chat: GroupChat): Boolean {
val chatAdmins = getChatAdministrators(chat)
val chatAdminsIds = chatAdmins.map { adminMember -> adminMember.user.id }
return user.id in chatAdminsIds
}

View File

@ -15,9 +15,11 @@ import dev.inmo.tgbotapi.extensions.behaviour_builder.triggers_handling.onNewCha
import dev.inmo.tgbotapi.extensions.utils.extensions.sameChat import dev.inmo.tgbotapi.extensions.utils.extensions.sameChat
import dev.inmo.tgbotapi.extensions.utils.extensions.sameMessage import dev.inmo.tgbotapi.extensions.utils.extensions.sameMessage
import dev.inmo.tgbotapi.extensions.utils.formatting.bold import dev.inmo.tgbotapi.extensions.utils.formatting.bold
import dev.inmo.tgbotapi.extensions.utils.ifCommonGroupContentMessage
import dev.inmo.tgbotapi.extensions.utils.types.buttons.dataButton import dev.inmo.tgbotapi.extensions.utils.types.buttons.dataButton
import dev.inmo.tgbotapi.extensions.utils.types.buttons.flatInlineKeyboard import dev.inmo.tgbotapi.extensions.utils.types.buttons.flatInlineKeyboard
import dev.inmo.tgbotapi.extensions.utils.whenCommonGroupContentMessage import dev.inmo.tgbotapi.extensions.utils.whenCommonGroupContentMessage
import dev.inmo.tgbotapi.libraries.cache.admins.AdminsCacheAPI
import dev.inmo.tgbotapi.types.BotCommand import dev.inmo.tgbotapi.types.BotCommand
import dev.inmo.tgbotapi.types.MilliSeconds import dev.inmo.tgbotapi.types.MilliSeconds
import dev.inmo.tgbotapi.types.chat.GroupChat import dev.inmo.tgbotapi.types.chat.GroupChat
@ -73,13 +75,14 @@ class WelcomePlugin : Plugin {
} }
private suspend fun BehaviourContext.handleWelcomeCommand( private suspend fun BehaviourContext.handleWelcomeCommand(
adminsCacheAPI: AdminsCacheAPI,
welcomeTable: WelcomeTable, welcomeTable: WelcomeTable,
config: Config, config: Config,
groupMessage: CommonGroupContentMessage<MessageContent> groupMessage: CommonGroupContentMessage<MessageContent>
) { ) {
val user = groupMessage.user val user = groupMessage.user
if (userIsAdmin(user, groupMessage.chat)) { if (adminsCacheAPI.isAdmin(groupMessage.chat.id, user.id)) {
val sentMessage = send( val sentMessage = send(
user, user,
replyMarkup = flatInlineKeyboard { replyMarkup = flatInlineKeyboard {
@ -152,8 +155,8 @@ class WelcomePlugin : Plugin {
while (isActive) { while (isActive) {
delay(config.recheckOfAdmin) delay(config.recheckOfAdmin)
if (!userIsAdmin(user, groupMessage.chat)) { if (adminsCacheAPI.isAdmin(groupMessage.chat.id, user.id)) {
edit(sentMessage, "Sorry, but you are not admin in chat ${groupMessage.chat.title} more") edit(sentMessage, "Sorry, but you are not admin in chat ${groupMessage.chat.title} anymore")
break break
} }
} }
@ -169,14 +172,15 @@ class WelcomePlugin : Plugin {
val config = koin.get<Config>() val config = koin.get<Config>()
val welcomeTable = koin.get<WelcomeTable>() val welcomeTable = koin.get<WelcomeTable>()
val adminsCacheAPI = koin.get<AdminsCacheAPI>()
onCommand( onCommand(
"welcome", "welcome",
initialFilter = { it.chat is GroupChat } initialFilter = { it.chat is GroupChat }
) { ) {
it.whenCommonGroupContentMessage { groupMessage -> it.ifCommonGroupContentMessage { groupMessage ->
launch { launch {
handleWelcomeCommand(welcomeTable, config, groupMessage) handleWelcomeCommand(adminsCacheAPI, welcomeTable, config, groupMessage)
} }
} }
} }