1
0
mirror of https://github.com/InsanusMokrassar/TelegramBotAPI.git synced 2024-11-22 08:13:47 +00:00

Add extension functions to ChatMember and ChatMemberUpdated to simplify working with updates. Closes #888

This commit is contained in:
McModder 2024-08-28 04:26:38 +03:00
parent e422c3bc33
commit 38e1d271ba
No known key found for this signature in database
GPG Key ID: A47A5642B5DA64BE
2 changed files with 92 additions and 0 deletions

View File

@ -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

View File

@ -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