mirror of
https://github.com/InsanusMokrassar/TelegramBotTutorial.git
synced 2024-11-16 13:13:49 +00:00
add admins cache api usage
This commit is contained in:
parent
6cefafbd09
commit
0f77475434
@ -4,6 +4,7 @@ kotlin = "1.7.10"
|
||||
plagubot = "2.3.3"
|
||||
kslog = "0.5.2"
|
||||
plagubot-commands = "0.3.4"
|
||||
tgbotapi-libraries = "0.5.4"
|
||||
|
||||
[libraries]
|
||||
|
||||
@ -11,6 +12,7 @@ kotlin = { module = "org.jetbrains.kotlin:kotlin-stdlib", version.ref = "kotlin"
|
||||
plagubot = { module = "dev.inmo:plagubot.bot", version.ref = "plagubot" }
|
||||
plagubot-plugin = { module = "dev.inmo:plagubot.plugin", version.ref = "plagubot" }
|
||||
plagubot-commands = { module = "dev.inmo:plagubot.plugins.commands", version.ref = "plagubot-commands" }
|
||||
tgbotapi-libraries-admins = { module = "dev.inmo:tgbotapi.libraries.cache.admins.plagubot", version.ref = "tgbotapi-libraries" }
|
||||
kslog = { module = "dev.inmo:kslog", version.ref = "kslog" }
|
||||
|
||||
# Libs for classpath
|
||||
|
@ -24,4 +24,5 @@ dependencies {
|
||||
api libs.plagubot.plugin
|
||||
api libs.kslog
|
||||
api libs.plagubot.commands
|
||||
api libs.tgbotapi.libraries.admins
|
||||
}
|
||||
|
@ -1,11 +0,0 @@
|
||||
import dev.inmo.tgbotapi.bot.TelegramBot
|
||||
import dev.inmo.tgbotapi.extensions.api.chat.get.getChatAdministrators
|
||||
import dev.inmo.tgbotapi.types.chat.GroupChat
|
||||
import dev.inmo.tgbotapi.types.chat.User
|
||||
|
||||
suspend fun TelegramBot.userIsAdmin(user: User, chat: GroupChat): Boolean {
|
||||
val chatAdmins = getChatAdministrators(chat)
|
||||
val chatAdminsIds = chatAdmins.map { adminMember -> adminMember.user.id }
|
||||
|
||||
return user.id in chatAdminsIds
|
||||
}
|
@ -15,9 +15,11 @@ import dev.inmo.tgbotapi.extensions.behaviour_builder.triggers_handling.onNewCha
|
||||
import dev.inmo.tgbotapi.extensions.utils.extensions.sameChat
|
||||
import dev.inmo.tgbotapi.extensions.utils.extensions.sameMessage
|
||||
import dev.inmo.tgbotapi.extensions.utils.formatting.bold
|
||||
import dev.inmo.tgbotapi.extensions.utils.ifCommonGroupContentMessage
|
||||
import dev.inmo.tgbotapi.extensions.utils.types.buttons.dataButton
|
||||
import dev.inmo.tgbotapi.extensions.utils.types.buttons.flatInlineKeyboard
|
||||
import dev.inmo.tgbotapi.extensions.utils.whenCommonGroupContentMessage
|
||||
import dev.inmo.tgbotapi.libraries.cache.admins.AdminsCacheAPI
|
||||
import dev.inmo.tgbotapi.types.BotCommand
|
||||
import dev.inmo.tgbotapi.types.MilliSeconds
|
||||
import dev.inmo.tgbotapi.types.chat.GroupChat
|
||||
@ -73,13 +75,14 @@ class WelcomePlugin : Plugin {
|
||||
}
|
||||
|
||||
private suspend fun BehaviourContext.handleWelcomeCommand(
|
||||
adminsCacheAPI: AdminsCacheAPI,
|
||||
welcomeTable: WelcomeTable,
|
||||
config: Config,
|
||||
groupMessage: CommonGroupContentMessage<MessageContent>
|
||||
) {
|
||||
val user = groupMessage.user
|
||||
|
||||
if (userIsAdmin(user, groupMessage.chat)) {
|
||||
if (adminsCacheAPI.isAdmin(groupMessage.chat.id, user.id)) {
|
||||
val sentMessage = send(
|
||||
user,
|
||||
replyMarkup = flatInlineKeyboard {
|
||||
@ -152,8 +155,8 @@ class WelcomePlugin : Plugin {
|
||||
while (isActive) {
|
||||
delay(config.recheckOfAdmin)
|
||||
|
||||
if (!userIsAdmin(user, groupMessage.chat)) {
|
||||
edit(sentMessage, "Sorry, but you are not admin in chat ${groupMessage.chat.title} more")
|
||||
if (adminsCacheAPI.isAdmin(groupMessage.chat.id, user.id)) {
|
||||
edit(sentMessage, "Sorry, but you are not admin in chat ${groupMessage.chat.title} anymore")
|
||||
break
|
||||
}
|
||||
}
|
||||
@ -169,14 +172,15 @@ class WelcomePlugin : Plugin {
|
||||
val config = koin.get<Config>()
|
||||
|
||||
val welcomeTable = koin.get<WelcomeTable>()
|
||||
val adminsCacheAPI = koin.get<AdminsCacheAPI>()
|
||||
|
||||
onCommand(
|
||||
"welcome",
|
||||
initialFilter = { it.chat is GroupChat }
|
||||
) {
|
||||
it.whenCommonGroupContentMessage { groupMessage ->
|
||||
it.ifCommonGroupContentMessage { groupMessage ->
|
||||
launch {
|
||||
handleWelcomeCommand(welcomeTable, config, groupMessage)
|
||||
handleWelcomeCommand(adminsCacheAPI, welcomeTable, config, groupMessage)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user