From 777604e5a03e69c7e95eb9589937e3b13a7dad30 Mon Sep 17 00:00:00 2001 From: InsanusMokrassar Date: Fri, 30 Aug 2024 19:06:38 +0600 Subject: [PATCH] update new samples --- .../src/main/kotlin/MemberUpdatedWatcher.kt | 45 +++++++++++++------ 1 file changed, 31 insertions(+), 14 deletions(-) diff --git a/MemberUpdatedWatcherBot/src/main/kotlin/MemberUpdatedWatcher.kt b/MemberUpdatedWatcherBot/src/main/kotlin/MemberUpdatedWatcher.kt index 0480d2d..37a28e4 100644 --- a/MemberUpdatedWatcherBot/src/main/kotlin/MemberUpdatedWatcher.kt +++ b/MemberUpdatedWatcherBot/src/main/kotlin/MemberUpdatedWatcher.kt @@ -1,3 +1,4 @@ +import dev.inmo.kslog.common.* import dev.inmo.tgbotapi.extensions.api.* import dev.inmo.tgbotapi.extensions.api.bot.* import dev.inmo.tgbotapi.extensions.api.send.* @@ -13,56 +14,72 @@ import dev.inmo.tgbotapi.utils.* suspend fun main(args: Array) { val token = args.first() + val isDebug = args.any { it == "debug" } + + if (isDebug) { + setDefaultKSLog( + KSLog { level: LogLevel, tag: String?, message: Any, throwable: Throwable? -> + println(defaultMessageFormatter(level, tag, message, throwable)) + } + ) + } + + val internalLogger = KSLog { level: LogLevel, tag: String?, message: Any, throwable: Throwable? -> + println(defaultMessageFormatter(level, tag ?: "ChatMemberUpdates", message, throwable)) + } + val bot = telegramBot(token) bot.buildBehaviourWithLongPolling { val me = getMe() val filterSelfUpdates = SimpleFilter { - it.newChatMemberState.user.id == me.id + it.member.id == me.id } + // This bot updates onChatMemberJoined(initialFilter = filterSelfUpdates) { - println("Bot was added to chat") + 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") } onChatMemberGotPromoted(initialFilter = filterSelfUpdates) { - println("Bot was granted admin permissions") + internalLogger.i("Bot was granted admin permissions") sendMessage(it.chat.id, "I was promoted to admin. I now can watch other users' events") } onChatMemberGotDemoted(initialFilter = filterSelfUpdates) { - println("Admin permissions were revoked") + 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") } + // All users updates onChatMemberJoined { - val member = it.newChatMemberState.user - println("${member.firstName} joined the chat: ${it.oldChatMemberState::class.simpleName} => ${it.newChatMemberState::class.simpleName}") + 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}") } onChatMemberLeft { - val member = it.newChatMemberState.user - println("${member.firstName} left the chat: ${it.oldChatMemberState::class.simpleName} => ${it.newChatMemberState::class.simpleName}") + 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}") } onChatMemberGotPromoted { - val newState = it.newChatMemberState.requireAdministratorChatMember() - println("${newState.user.firstName} got promoted to ${newState.customTitle ?: "Admin"}: ${it.oldChatMemberState::class.simpleName} => ${it.newChatMemberState::class.simpleName}") + 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"}") } onChatMemberGotDemoted { - val member = it.newChatMemberState.user - println("${member.firstName} got demoted: ${it.oldChatMemberState::class.simpleName} => ${it.newChatMemberState::class.simpleName}") + 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") } onChatMemberGotPromotionChanged { - val member = it.newChatMemberState.requireAdministratorChatMember() - println("${member.user.firstName} has the permissions changed: ${it.oldChatMemberState::class.simpleName} => ${it.newChatMemberState::class.simpleName}") + val member = it.newChatMemberState.administratorChatMemberOrThrow() + internalLogger.i("${member.user.firstName} has the permissions changed: ${it.oldChatMemberState::class.simpleName} => ${it.newChatMemberState::class.simpleName}") } }.join() } \ No newline at end of file