From 38e1d271bae852a6ab30b95478b8cdf85d39d342 Mon Sep 17 00:00:00 2001 From: McModder Date: Wed, 28 Aug 2024 04:26:38 +0300 Subject: [PATCH] Add extension functions to ChatMember and ChatMemberUpdated to simplify working with updates. Closes #888 --- .../utils/extensions/ChatMember.kt | 24 +++++++ .../utils/extensions/ChatMemberUpdated.kt | 68 +++++++++++++++++++ 2 files changed, 92 insertions(+) create mode 100644 tgbotapi.behaviour_builder/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/behaviour_builder/utils/extensions/ChatMember.kt create mode 100644 tgbotapi.behaviour_builder/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/behaviour_builder/utils/extensions/ChatMemberUpdated.kt diff --git a/tgbotapi.behaviour_builder/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/behaviour_builder/utils/extensions/ChatMember.kt b/tgbotapi.behaviour_builder/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/behaviour_builder/utils/extensions/ChatMember.kt new file mode 100644 index 0000000000..8a13fcef5c --- /dev/null +++ b/tgbotapi.behaviour_builder/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/behaviour_builder/utils/extensions/ChatMember.kt @@ -0,0 +1,24 @@ +package dev.inmo.tgbotapi.extensions.behaviour_builder.utils.extensions + +import dev.inmo.tgbotapi.types.chat.member.* + +val ChatMember.isLeft: Boolean get() = this is LeftChatMember +val ChatMember.isKicked: Boolean get() = this is KickedChatMember +val ChatMember.isLeftOrKicked: Boolean get() = isLeft || isKicked +val ChatMember.isRestricted: Boolean get() = this is RestrictedChatMember +val ChatMember.isMember: Boolean get() = this is MemberChatMember +val ChatMember.isOwner: Boolean get() = this is OwnerChatMember +val ChatMember.isSubscriber: Boolean get() = this is SubscriptionMemberChatMember +val ChatMember.isAdministrator: Boolean get() = this is AdministratorChatMember + +/** + * Checks if member is strictly [MemberChatMember], not any derivatives + */ +val ChatMember.isMemberStrict: Boolean get() = this::class == MemberChatMemberImpl::class +/** + * Checks if member is strictly [AdministratorChatMember], not any derivatives + */ +val ChatMember.isAdministratorStrict: Boolean get() = this::class == AdministratorChatMemberImpl::class + +val ChatMember.hasSpecialRights: Boolean get() = this is SpecialRightsChatMember +val ChatMember.isKickedOrRestricted: Boolean get() = this is BannedChatMember diff --git a/tgbotapi.behaviour_builder/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/behaviour_builder/utils/extensions/ChatMemberUpdated.kt b/tgbotapi.behaviour_builder/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/behaviour_builder/utils/extensions/ChatMemberUpdated.kt new file mode 100644 index 0000000000..fec9d1974a --- /dev/null +++ b/tgbotapi.behaviour_builder/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/behaviour_builder/utils/extensions/ChatMemberUpdated.kt @@ -0,0 +1,68 @@ +package dev.inmo.tgbotapi.extensions.behaviour_builder.utils.extensions + +import dev.inmo.tgbotapi.types.chat.member.* + +/** + * User joined a chat + */ +val ChatMemberUpdated.joinedChat get() = oldChatMemberState.isLeftOrKicked && !newChatMemberState.isLeftOrKicked + +/** + * Member left a chat for any reason + */ +val ChatMemberUpdated.leftChat get() = !oldChatMemberState.isLeftOrKicked && newChatMemberState.isLeftOrKicked + +/** + * Member became a chat subscriber + */ +val ChatMemberUpdated.subscribed: Boolean get() = !oldChatMemberState.isSubscriber && newChatMemberState.isSubscriber + +/** + * Member became a chat subscriber or renewed their subscription + */ +val ChatMemberUpdated.subscriptionChanged: Boolean get() = newChatMemberState.isSubscriber + +/** + * Member subscription was expired + */ +val ChatMemberUpdated.unsubscribed: Boolean get() = oldChatMemberState.isSubscriber && !newChatMemberState.isSubscriber + +/** + * Member was promoted to chat administrator (or owner) + */ +val ChatMemberUpdated.gotPromoted: Boolean get() = !oldChatMemberState.isAdministrator && newChatMemberState.isAdministrator + +/** + * Member was promoted to chat administrator (or owner) or got it permissions/title changed + */ +val ChatMemberUpdated.administratorChanged: Boolean get() = newChatMemberState.isAdministrator + +/** + * Member was demoted from administrators (or ceased chat ownership) + */ +val ChatMemberUpdated.gotDemoted: Boolean get() = oldChatMemberState.isAdministrator && !newChatMemberState.isAdministrator + +/** + * Member became a chat owner + */ +val ChatMemberUpdated.becameOwner: Boolean get() = !oldChatMemberState.isOwner && newChatMemberState.isOwner + +/** + * Member ceased their chat ownership + */ +val ChatMemberUpdated.ceasedOwner: Boolean get() = oldChatMemberState.isOwner && !newChatMemberState.isOwner + +/** + * Member was restricted or some restrictions have changed + */ +val ChatMemberUpdated.gotRestricted: Boolean get() = !oldChatMemberState.isRestricted && newChatMemberState.isRestricted + +/** + * Member restrictions were changed (but not removed) + */ +val ChatMemberUpdated.gotRestrictionsChanged: Boolean get() = newChatMemberState.isRestricted + +/** + * All member restrictions were removed + */ +val ChatMemberUpdated.gotUnrestricted: Boolean get() = oldChatMemberState.isRestricted && !newChatMemberState.isRestricted