mirror of
https://github.com/InsanusMokrassar/TelegramBotApiLibraries.git
synced 2025-09-07 09:09:21 +00:00
update dependencies and add opportunity to listen for admins in chats
This commit is contained in:
@@ -0,0 +1,24 @@
|
||||
package dev.inmo.tgbotapi.libraries.cache.admins
|
||||
|
||||
import dev.inmo.tgbotapi.extensions.api.bot.getMe
|
||||
import dev.inmo.tgbotapi.extensions.behaviour_builder.BehaviourContext
|
||||
import dev.inmo.tgbotapi.extensions.behaviour_builder.triggers_handling.onChatMemberUpdated
|
||||
import dev.inmo.tgbotapi.types.chat.member.AdministratorChatMember
|
||||
|
||||
suspend fun BehaviourContext.activateAdminsChangesListening(
|
||||
repo: DefaultAdminsCacheAPIRepo
|
||||
) {
|
||||
val me = getMe()
|
||||
onChatMemberUpdated {
|
||||
when {
|
||||
it.oldChatMemberState is AdministratorChatMember && it.newChatMemberState !is AdministratorChatMember ||
|
||||
it.newChatMemberState is AdministratorChatMember && it.oldChatMemberState !is AdministratorChatMember -> {
|
||||
updateAdmins(
|
||||
it.chat.id,
|
||||
repo,
|
||||
me
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@@ -29,15 +29,6 @@ class DefaultAdminsCacheAPI(
|
||||
bot.getMe().also { botInfo = it }
|
||||
}
|
||||
|
||||
private suspend fun triggerUpdate(chatId: ChatId): List<AdministratorChatMember> {
|
||||
val botInfo = getBotInfo()
|
||||
val admins = bot.getChatAdministrators(chatId).filter {
|
||||
botInfo.id != it.user.id
|
||||
}
|
||||
repo.setChatAdmins(chatId, admins)
|
||||
return admins
|
||||
}
|
||||
|
||||
override suspend fun getChatAdmins(chatId: ChatId): List<AdministratorChatMember>? {
|
||||
val settings = settingsAPI.getChatSettings(chatId)
|
||||
val lastUpdate = repo.lastUpdate(chatId)
|
||||
@@ -63,5 +54,4 @@ class DefaultAdminsCacheAPI(
|
||||
}
|
||||
|
||||
override suspend fun settings(): AdminsCacheSettingsAPI = settingsAPI
|
||||
|
||||
}
|
||||
|
@@ -0,0 +1,21 @@
|
||||
package dev.inmo.tgbotapi.libraries.cache.admins
|
||||
|
||||
import dev.inmo.tgbotapi.bot.TelegramBot
|
||||
import dev.inmo.tgbotapi.extensions.api.bot.getMe
|
||||
import dev.inmo.tgbotapi.extensions.api.chat.get.getChatAdministrators
|
||||
import dev.inmo.tgbotapi.types.ChatId
|
||||
import dev.inmo.tgbotapi.types.chat.ExtendedBot
|
||||
import dev.inmo.tgbotapi.types.chat.member.AdministratorChatMember
|
||||
|
||||
internal suspend fun TelegramBot.updateAdmins(
|
||||
chatId: ChatId,
|
||||
repo: DefaultAdminsCacheAPIRepo,
|
||||
botInfo: ExtendedBot? = null
|
||||
): List<AdministratorChatMember> {
|
||||
val botInfo = botInfo ?: getMe()
|
||||
val admins = getChatAdministrators(chatId).filter {
|
||||
botInfo.id != it.user.id
|
||||
}
|
||||
repo.setChatAdmins(chatId, admins)
|
||||
return admins
|
||||
}
|
Reference in New Issue
Block a user