From 0f774754347f443c0a22904b0caa1209d38c55a2 Mon Sep 17 00:00:00 2001 From: InsanusMokrassar Date: Thu, 22 Sep 2022 15:00:40 +0600 Subject: [PATCH] add admins cache api usage --- gradle/libs.versions.toml | 2 ++ welcome/build.gradle | 1 + welcome/src/main/kotlin/UserAdminChecker.kt | 11 ----------- welcome/src/main/kotlin/WelcomePlugin.kt | 14 +++++++++----- 4 files changed, 12 insertions(+), 16 deletions(-) delete mode 100644 welcome/src/main/kotlin/UserAdminChecker.kt diff --git a/gradle/libs.versions.toml b/gradle/libs.versions.toml index 9e1d8a6..4c986e5 100644 --- a/gradle/libs.versions.toml +++ b/gradle/libs.versions.toml @@ -4,6 +4,7 @@ kotlin = "1.7.10" plagubot = "2.3.3" kslog = "0.5.2" plagubot-commands = "0.3.4" +tgbotapi-libraries = "0.5.4" [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-plugin = { module = "dev.inmo:plagubot.plugin", version.ref = "plagubot" } 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" } # Libs for classpath diff --git a/welcome/build.gradle b/welcome/build.gradle index ddb46de..e0b0ad7 100644 --- a/welcome/build.gradle +++ b/welcome/build.gradle @@ -24,4 +24,5 @@ dependencies { api libs.plagubot.plugin api libs.kslog api libs.plagubot.commands + api libs.tgbotapi.libraries.admins } diff --git a/welcome/src/main/kotlin/UserAdminChecker.kt b/welcome/src/main/kotlin/UserAdminChecker.kt deleted file mode 100644 index 9217c19..0000000 --- a/welcome/src/main/kotlin/UserAdminChecker.kt +++ /dev/null @@ -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 -} diff --git a/welcome/src/main/kotlin/WelcomePlugin.kt b/welcome/src/main/kotlin/WelcomePlugin.kt index d452c18..2b9b171 100644 --- a/welcome/src/main/kotlin/WelcomePlugin.kt +++ b/welcome/src/main/kotlin/WelcomePlugin.kt @@ -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.sameMessage 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.flatInlineKeyboard 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.MilliSeconds import dev.inmo.tgbotapi.types.chat.GroupChat @@ -73,13 +75,14 @@ class WelcomePlugin : Plugin { } private suspend fun BehaviourContext.handleWelcomeCommand( + adminsCacheAPI: AdminsCacheAPI, welcomeTable: WelcomeTable, config: Config, groupMessage: CommonGroupContentMessage ) { val user = groupMessage.user - if (userIsAdmin(user, groupMessage.chat)) { + if (adminsCacheAPI.isAdmin(groupMessage.chat.id, user.id)) { val sentMessage = send( user, replyMarkup = flatInlineKeyboard { @@ -152,8 +155,8 @@ class WelcomePlugin : Plugin { while (isActive) { delay(config.recheckOfAdmin) - if (!userIsAdmin(user, groupMessage.chat)) { - edit(sentMessage, "Sorry, but you are not admin in chat ${groupMessage.chat.title} more") + if (adminsCacheAPI.isAdmin(groupMessage.chat.id, user.id)) { + edit(sentMessage, "Sorry, but you are not admin in chat ${groupMessage.chat.title} anymore") break } } @@ -169,14 +172,15 @@ class WelcomePlugin : Plugin { val config = koin.get() val welcomeTable = koin.get() + val adminsCacheAPI = koin.get() onCommand( "welcome", initialFilter = { it.chat is GroupChat } ) { - it.whenCommonGroupContentMessage { groupMessage -> + it.ifCommonGroupContentMessage { groupMessage -> launch { - handleWelcomeCommand(welcomeTable, config, groupMessage) + handleWelcomeCommand(adminsCacheAPI, welcomeTable, config, groupMessage) } } }