mirror of
https://github.com/InsanusMokrassar/TelegramBotAPI-examples.git
synced 2024-11-22 16:23:54 +00:00
fixes in rights bot
This commit is contained in:
parent
676ce0df80
commit
6922a6d667
@ -25,10 +25,8 @@ import dev.inmo.tgbotapi.types.chat.PublicChat
|
|||||||
import dev.inmo.tgbotapi.types.chat.member.*
|
import dev.inmo.tgbotapi.types.chat.member.*
|
||||||
import dev.inmo.tgbotapi.types.commands.BotCommandScope
|
import dev.inmo.tgbotapi.types.commands.BotCommandScope
|
||||||
import dev.inmo.tgbotapi.types.request.RequestId
|
import dev.inmo.tgbotapi.types.request.RequestId
|
||||||
import dev.inmo.tgbotapi.utils.botCommand
|
import dev.inmo.tgbotapi.utils.*
|
||||||
import dev.inmo.tgbotapi.utils.mention
|
import dev.inmo.tgbotapi.utils.mention
|
||||||
import dev.inmo.tgbotapi.utils.regular
|
|
||||||
import dev.inmo.tgbotapi.utils.row
|
|
||||||
import kotlinx.coroutines.flow.filter
|
import kotlinx.coroutines.flow.filter
|
||||||
import kotlinx.coroutines.flow.first
|
import kotlinx.coroutines.flow.first
|
||||||
import kotlinx.coroutines.flow.mapNotNull
|
import kotlinx.coroutines.flow.mapNotNull
|
||||||
@ -79,6 +77,7 @@ suspend fun main(args: Array<String>) {
|
|||||||
val webPagePreviewToggleCommonData = "$commonDataPrefix web page preview"
|
val webPagePreviewToggleCommonData = "$commonDataPrefix web page preview"
|
||||||
|
|
||||||
val adminRightsDataPrefix = "admin"
|
val adminRightsDataPrefix = "admin"
|
||||||
|
val refreshAdminRightsData = "${adminRightsDataPrefix}_refresh"
|
||||||
val postMessagesToggleAdminRightsData = "${adminRightsDataPrefix}_post_messages"
|
val postMessagesToggleAdminRightsData = "${adminRightsDataPrefix}_post_messages"
|
||||||
val editMessagesToggleAdminRightsData = "${adminRightsDataPrefix}_edit_messages"
|
val editMessagesToggleAdminRightsData = "${adminRightsDataPrefix}_edit_messages"
|
||||||
val deleteMessagesToggleAdminRightsData = "${adminRightsDataPrefix}_delete_messages"
|
val deleteMessagesToggleAdminRightsData = "${adminRightsDataPrefix}_delete_messages"
|
||||||
@ -128,11 +127,15 @@ suspend fun main(args: Array<String>) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
fun buildAdminRightsKeyboard(
|
fun buildAdminRightsKeyboard(
|
||||||
permissions: AdministratorChatMember,
|
permissions: AdministratorChatMember?,
|
||||||
channelId: ChatId,
|
channelId: ChatId,
|
||||||
userId: UserId
|
userId: UserId
|
||||||
): InlineKeyboardMarkup {
|
): InlineKeyboardMarkup {
|
||||||
return inlineKeyboard {
|
return inlineKeyboard {
|
||||||
|
permissions ?.also {
|
||||||
|
row {
|
||||||
|
dataButton("Refresh", "$refreshAdminRightsData ${channelId.chatId} ${userId.chatId}")
|
||||||
|
}
|
||||||
row {
|
row {
|
||||||
dataButton("Edit messages${permissions.canEditMessages.allowedSymbol()}", "$editMessagesToggleAdminRightsData ${channelId.chatId} ${userId.chatId}")
|
dataButton("Edit messages${permissions.canEditMessages.allowedSymbol()}", "$editMessagesToggleAdminRightsData ${channelId.chatId} ${userId.chatId}")
|
||||||
dataButton("Delete messages${permissions.canRemoveMessages.allowedSymbol()}", "$deleteMessagesToggleAdminRightsData ${channelId.chatId} ${userId.chatId}")
|
dataButton("Delete messages${permissions.canRemoveMessages.allowedSymbol()}", "$deleteMessagesToggleAdminRightsData ${channelId.chatId} ${userId.chatId}")
|
||||||
@ -147,6 +150,9 @@ suspend fun main(args: Array<String>) {
|
|||||||
row {
|
row {
|
||||||
dataButton("Post stories${permissions.canPostStories.allowedSymbol()}", "$postStoriesToggleAdminRightsData ${channelId.chatId} ${userId.chatId}")
|
dataButton("Post stories${permissions.canPostStories.allowedSymbol()}", "$postStoriesToggleAdminRightsData ${channelId.chatId} ${userId.chatId}")
|
||||||
}
|
}
|
||||||
|
} ?: row {
|
||||||
|
dataButton("Promote to admin", "$postMessagesToggleAdminRightsData ${channelId.chatId} ${userId.chatId}")
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -344,27 +350,30 @@ suspend fun main(args: Array<String>) {
|
|||||||
val (channelIdString, userIdString) = it.data.split(" ").drop(1)
|
val (channelIdString, userIdString) = it.data.split(" ").drop(1)
|
||||||
val channelId = ChatId(channelIdString.toLong())
|
val channelId = ChatId(channelIdString.toLong())
|
||||||
val userId = ChatId(userIdString.toLong())
|
val userId = ChatId(userIdString.toLong())
|
||||||
val chatMember = getChatMember(channelId, userId).administratorChatMemberOrNull() ?: return@onMessageDataCallbackQuery
|
val chatMember = getChatMember(channelId, userId)
|
||||||
|
val asAdmin = chatMember.administratorChatMemberOrNull()
|
||||||
|
val asMember = chatMember.memberChatMemberOrNull()
|
||||||
|
|
||||||
val realData = it.data.takeWhile { it != ' ' }
|
val realData = it.data.takeWhile { it != ' ' }
|
||||||
|
|
||||||
fun Boolean.toggleIfData(data: String) = if (realData == data) {
|
fun Boolean?.toggleIfData(data: String) = if (realData == data) {
|
||||||
!this
|
!(this ?: false)
|
||||||
} else {
|
} else {
|
||||||
null
|
null
|
||||||
}
|
}
|
||||||
val chat = getChat(userId)
|
|
||||||
|
|
||||||
|
if (realData != refreshAdminRightsData) {
|
||||||
promoteChannelAdministrator(
|
promoteChannelAdministrator(
|
||||||
channelId,
|
channelId,
|
||||||
userId,
|
userId,
|
||||||
canPostMessages = chatMember.canPostMessages.toggleIfData(postMessagesToggleAdminRightsData),
|
canPostMessages = asAdmin ?.canPostMessages.toggleIfData(postMessagesToggleAdminRightsData),
|
||||||
canEditMessages = chatMember.canEditMessages.toggleIfData(editMessagesToggleAdminRightsData),
|
canEditMessages = asAdmin ?.canEditMessages.toggleIfData(editMessagesToggleAdminRightsData),
|
||||||
canDeleteMessages = chatMember.canRemoveMessages.toggleIfData(deleteMessagesToggleAdminRightsData),
|
canDeleteMessages = asAdmin ?.canRemoveMessages.toggleIfData(deleteMessagesToggleAdminRightsData),
|
||||||
canEditStories = chatMember.canEditStories.toggleIfData(editStoriesToggleAdminRightsData),
|
canEditStories = asAdmin ?.canEditStories.toggleIfData(editStoriesToggleAdminRightsData),
|
||||||
canDeleteStories = chatMember.canDeleteStories.toggleIfData(deleteStoriesToggleAdminRightsData),
|
canDeleteStories = asAdmin ?.canDeleteStories.toggleIfData(deleteStoriesToggleAdminRightsData),
|
||||||
canPostStories = chatMember.canPostStories.toggleIfData(postStoriesToggleAdminRightsData),
|
canPostStories = asAdmin ?.canPostStories.toggleIfData(postStoriesToggleAdminRightsData),
|
||||||
)
|
)
|
||||||
|
}
|
||||||
|
|
||||||
edit(
|
edit(
|
||||||
it.message,
|
it.message,
|
||||||
@ -372,7 +381,7 @@ suspend fun main(args: Array<String>) {
|
|||||||
getChatMember(
|
getChatMember(
|
||||||
channelId,
|
channelId,
|
||||||
userId
|
userId
|
||||||
).administratorChatMemberOrNull() ?: return@onMessageDataCallbackQuery,
|
).administratorChatMemberOrNull(),
|
||||||
channelId,
|
channelId,
|
||||||
userId
|
userId
|
||||||
)
|
)
|
||||||
@ -478,7 +487,8 @@ suspend fun main(args: Array<String>) {
|
|||||||
)
|
)
|
||||||
) {
|
) {
|
||||||
regular("Rights of ")
|
regular("Rights of ")
|
||||||
mention(chatMember.user)
|
mentionln(chatMember.user)
|
||||||
|
regular("Please, remember, that to be able to change user rights bot must promote user by itself to admin")
|
||||||
}
|
}
|
||||||
null
|
null
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user