From 0ec543d5c5cdd75be5c1b857c9447eb8e00f8549 Mon Sep 17 00:00:00 2001 From: InsanusMokrassar Date: Fri, 30 Aug 2024 23:32:29 +0600 Subject: [PATCH] update sample of MemberUpdatedWatcher bot --- .../src/main/kotlin/MemberUpdatedWatcher.kt | 31 +++++++++++++------ 1 file changed, 22 insertions(+), 9 deletions(-) diff --git a/MemberUpdatedWatcherBot/src/main/kotlin/MemberUpdatedWatcher.kt b/MemberUpdatedWatcherBot/src/main/kotlin/MemberUpdatedWatcher.kt index 37a28e4..91362a2 100644 --- a/MemberUpdatedWatcherBot/src/main/kotlin/MemberUpdatedWatcher.kt +++ b/MemberUpdatedWatcherBot/src/main/kotlin/MemberUpdatedWatcher.kt @@ -3,6 +3,8 @@ import dev.inmo.tgbotapi.extensions.api.* import dev.inmo.tgbotapi.extensions.api.bot.* import dev.inmo.tgbotapi.extensions.api.send.* import dev.inmo.tgbotapi.extensions.behaviour_builder.* +import dev.inmo.tgbotapi.extensions.behaviour_builder.filters.chatMemberGotRestrictedFilter +import dev.inmo.tgbotapi.extensions.behaviour_builder.filters.chatMemberGotRestrictionsChangedFilter import dev.inmo.tgbotapi.extensions.behaviour_builder.triggers_handling.* import dev.inmo.tgbotapi.extensions.behaviour_builder.utils.* import dev.inmo.tgbotapi.extensions.utils.* @@ -39,47 +41,58 @@ suspend fun main(args: Array) { // This bot updates onChatMemberJoined(initialFilter = filterSelfUpdates) { internalLogger.i("Bot was added to chat") - sendMessage(it.chat.id, "I was added to chat. Please grant me admin permissions to make me able to watch other users' events") + send(it.chat.id, "I was added to chat. Please grant me admin permissions to make me able to watch other users' events") } onChatMemberGotPromoted(initialFilter = filterSelfUpdates) { internalLogger.i("Bot was granted admin permissions") - sendMessage(it.chat.id, "I was promoted to admin. I now can watch other users' events") + send(it.chat.id, "I was promoted to admin. I now can watch other users' events") } onChatMemberGotDemoted(initialFilter = filterSelfUpdates) { internalLogger.i("Admin permissions were revoked") - sendMessage(it.chat.id, "I'm no longer an admin. Admin permissions are required to watch other users' events") + send(it.chat.id, "I'm no longer an admin. Admin permissions are required to watch other users' events") } // All users updates onChatMemberJoined { val member = it.member internalLogger.i("${member.firstName} joined the chat: ${it.oldChatMemberState::class.simpleName} => ${it.newChatMemberState::class.simpleName}") - sendMessage(it.chat.id, "Welcome ${member.firstName}") + send(it.chat.id, "Welcome ${member.firstName}") } onChatMemberLeft { val member = it.member internalLogger.i("${member.firstName} left the chat: ${it.oldChatMemberState::class.simpleName} => ${it.newChatMemberState::class.simpleName}") - sendMessage(it.chat.id, "Goodbye ${member.firstName}") + send(it.chat.id, "Goodbye ${member.firstName}") } onChatMemberGotPromoted { val newState = it.newChatMemberState.administratorChatMemberOrThrow() internalLogger.i("${newState.user.firstName} got promoted to ${newState.customTitle ?: "Admin"}: ${it.oldChatMemberState::class.simpleName} => ${it.newChatMemberState::class.simpleName}") - sendMessage(it.chat.id, "${newState.user.firstName} is now an ${newState.customTitle ?: "Admin"}") + send(it.chat.id, "${newState.user.firstName} is now an ${newState.customTitle ?: "Admin"}") } onChatMemberGotDemoted { val member = it.member internalLogger.i("${member.firstName} got demoted: ${it.oldChatMemberState::class.simpleName} => ${it.newChatMemberState::class.simpleName}") - sendMessage(it.chat.id, "${member.firstName} is now got demoted back to member") + send(it.chat.id, "${member.firstName} is now got demoted back to member") } onChatMemberGotPromotionChanged { - val member = it.newChatMemberState.administratorChatMemberOrThrow() - internalLogger.i("${member.user.firstName} has the permissions changed: ${it.oldChatMemberState::class.simpleName} => ${it.newChatMemberState::class.simpleName}") + val member = it.member + val message = "${member.firstName} has the permissions changed: ${it.oldChatMemberState::class.simpleName} => ${it.newChatMemberState::class.simpleName}" + internalLogger.i(message) + send(it.chat.id, message) + } + + onChatMemberUpdated( + initialFilter = chatMemberGotRestrictedFilter + chatMemberGotRestrictionsChangedFilter, + ) { + val member = it.member + val message = "${member.firstName} has the permissions changed: ${it.oldChatMemberState::class.simpleName} => ${it.newChatMemberState::class.simpleName}" + internalLogger.i(message) + send(it.chat.id, message) } }.join() } \ No newline at end of file