From 84f5b5da75389f9d6835632a04d89abac27e61a7 Mon Sep 17 00:00:00 2001 From: InsanusMokrassar Date: Sat, 9 Jul 2022 23:41:37 +0600 Subject: [PATCH 01/20] fill welcome plugin --- build.gradle | 1 + config.json | 7 +- gradle/libs.versions.toml | 4 +- settings.gradle | 1 + welcome/build.gradle | 1 + welcome/src/main/kotlin/UserAdminChecker.kt | 11 + welcome/src/main/kotlin/WelcomePlugin.kt | 198 +++++++++++++++++- welcome/src/main/kotlin/db/WelcomeTable.kt | 44 ++++ welcome/src/main/kotlin/model/ChatSettings.kt | 12 ++ 9 files changed, 266 insertions(+), 13 deletions(-) create mode 100644 welcome/src/main/kotlin/UserAdminChecker.kt create mode 100644 welcome/src/main/kotlin/db/WelcomeTable.kt create mode 100644 welcome/src/main/kotlin/model/ChatSettings.kt diff --git a/build.gradle b/build.gradle index 423b937..fd52fa2 100644 --- a/build.gradle +++ b/build.gradle @@ -25,6 +25,7 @@ dependencies { implementation libs.plagubot implementation project(":introduction") + implementation project(":welcome") } application { diff --git a/config.json b/config.json index 6878d1e..483d3a6 100644 --- a/config.json +++ b/config.json @@ -1,12 +1,15 @@ { "botToken": "1234567890:ABCDEFGHIJKLMNOP_qrstuvwxyz12345678", "plugins": [ - "IntroductionPlugin" + "dev.inmo.plagubot.plugins.commands.CommandsPlugin", + + "IntroductionPlugin", + "WelcomePlugin" ], "introduction": { "onStartCommandMessage": "Hello World" }, "database": { - "url": "jdbc:sqlite:file:local.db?mode=memory&cache=shared" + "url": "jdbc:sqlite:file:local.db" } } diff --git a/gradle/libs.versions.toml b/gradle/libs.versions.toml index 5857ec4..3b62b06 100644 --- a/gradle/libs.versions.toml +++ b/gradle/libs.versions.toml @@ -1,14 +1,16 @@ [versions] kotlin = "1.6.21" -plagubot = "1.2.2" +plagubot = "1.2.3" kslog = "0.3.2" +plagubot-commands = "0.1.0" [libraries] 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" } kslog = { module = "dev.inmo:kslog", version.ref = "kslog" } # Libs for classpath diff --git a/settings.gradle b/settings.gradle index 8fd4ae8..ecd58c9 100644 --- a/settings.gradle +++ b/settings.gradle @@ -1,3 +1,4 @@ rootProject.name = "tutorial" include ":introduction" +include ":welcome" diff --git a/welcome/build.gradle b/welcome/build.gradle index 3b4d2f0..ddb46de 100644 --- a/welcome/build.gradle +++ b/welcome/build.gradle @@ -23,4 +23,5 @@ dependencies { implementation libs.kotlin api libs.plagubot.plugin api libs.kslog + api libs.plagubot.commands } diff --git a/welcome/src/main/kotlin/UserAdminChecker.kt b/welcome/src/main/kotlin/UserAdminChecker.kt new file mode 100644 index 0000000..9217c19 --- /dev/null +++ b/welcome/src/main/kotlin/UserAdminChecker.kt @@ -0,0 +1,11 @@ +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 +} diff --git a/welcome/src/main/kotlin/WelcomePlugin.kt b/welcome/src/main/kotlin/WelcomePlugin.kt index 5998a2a..c795240 100644 --- a/welcome/src/main/kotlin/WelcomePlugin.kt +++ b/welcome/src/main/kotlin/WelcomePlugin.kt @@ -1,17 +1,40 @@ +import db.WelcomeTable import dev.inmo.kslog.common.logger import dev.inmo.kslog.common.w +import dev.inmo.micro_utils.coroutines.runCatchingSafely +import dev.inmo.micro_utils.coroutines.subscribeSafelyWithoutExceptions import dev.inmo.plagubot.Plugin -import dev.inmo.tgbotapi.extensions.api.send.reply -import dev.inmo.tgbotapi.extensions.api.send.sendMessage +import dev.inmo.plagubot.plugins.commands.full +import dev.inmo.tgbotapi.bot.exceptions.RequestException +import dev.inmo.tgbotapi.extensions.api.answers.answer +import dev.inmo.tgbotapi.extensions.api.chat.get.getChatAdministrators +import dev.inmo.tgbotapi.extensions.api.edit.edit +import dev.inmo.tgbotapi.extensions.api.send.* import dev.inmo.tgbotapi.extensions.behaviour_builder.BehaviourContext -import dev.inmo.tgbotapi.extensions.behaviour_builder.triggers_handling.onCommand -import dev.inmo.tgbotapi.extensions.behaviour_builder.triggers_handling.onMyChatMemberUpdated -import dev.inmo.tgbotapi.types.chat.PrivateChat +import dev.inmo.tgbotapi.extensions.behaviour_builder.expectations.waitContentMessage +import dev.inmo.tgbotapi.extensions.behaviour_builder.expectations.waitMessageDataCallbackQuery +import dev.inmo.tgbotapi.extensions.behaviour_builder.oneOf +import dev.inmo.tgbotapi.extensions.behaviour_builder.triggers_handling.* +import dev.inmo.tgbotapi.extensions.utils.* +import dev.inmo.tgbotapi.extensions.utils.formatting.* +import dev.inmo.tgbotapi.extensions.utils.types.buttons.* +import dev.inmo.tgbotapi.types.BotCommand +import dev.inmo.tgbotapi.types.MilliSeconds +import dev.inmo.tgbotapi.types.chat.GroupChat +import dev.inmo.tgbotapi.types.commands.BotCommandScope +import dev.inmo.tgbotapi.types.message.abstracts.CommonGroupContentMessage +import dev.inmo.tgbotapi.types.message.content.MessageContent +import dev.inmo.tgbotapi.types.message.content.TextContent +import kotlinx.coroutines.* +import kotlinx.coroutines.flow.filter +import kotlinx.coroutines.flow.first import kotlinx.serialization.Serializable import kotlinx.serialization.json.* +import model.ChatSettings import org.jetbrains.exposed.sql.Database import org.koin.core.Koin import org.koin.core.module.Module +import org.koin.core.qualifier.named /** * This is template of plugin with preset [log]ger, [Config] and template configurations of [setupDI] and [setupBotPlugin]. @@ -30,27 +53,182 @@ class WelcomePlugin : Plugin { * See realization of [setupDI] to get know how this class will be deserialized from global config * * See realization of [setupBotPlugin] to get know how to get access to this class + * + * @param recheckOfAdmin This parameter will be used before setup of */ @Serializable private class Config( + val recheckOfAdmin: MilliSeconds = 60L ) /** * DI configuration of current plugin. Here we are decoding [Config] and put it into [Module] receiver */ override fun Module.setupDI(database: Database, params: JsonObject) { - single { get().decodeFromJsonElement(Config.serializer(), params[pluginConfigSectionName] ?: return@single null) } + single { get().decodeFromJsonElement(Config.serializer(), params[pluginConfigSectionName] ?: return@single Config()) } + single { WelcomeTable(database) } + single(named("welcome")) { BotCommand("welcome", "Use to setup welcome message").full(BotCommandScope.AllChatAdministrators) } + } + + private suspend fun BehaviourContext.handleWelcomeCommand( + welcomeTable: WelcomeTable, + config: Config, + groupMessage: CommonGroupContentMessage + ) { + val user = groupMessage.user + + if (userIsAdmin(user, groupMessage.chat)) { + val cancelData = "cancel_${groupMessage.chat.id}" + val unsetData = "unset_${groupMessage.chat.id}" + + val sentMessage = sendMessage( + user, + buildEntities { + regular("Ok, send me the message which should be used as welcome message for chat ") + underline(groupMessage.chat.title) + }, + replyMarkup = inlineKeyboard { + row { + dataButton("Unset", unsetData) + dataButton("Cancel", cancelData) + } + } + ) + + oneOf( + async { + val query = waitMessageDataCallbackQuery().filter { + it.data == unsetData + && it.message.chat.id == sentMessage.chat.id + && it.message.messageId == sentMessage.messageId + }.first() + + if (welcomeTable.unset(groupMessage.chat.id)) { + edit( + sentMessage, + buildEntities { + regular("Welcome message has been removed for chat ") + underline(groupMessage.chat.title) + } + ) + } else { + edit( + sentMessage, + buildEntities { + regular("Something went wrong on welcome message unsetting for chat ") + underline(groupMessage.chat.title) + } + ) + } + + answer(query) + }, + async { + val query = waitMessageDataCallbackQuery().filter { + it.data == cancelData + && it.message.chat.id == sentMessage.chat.id + && it.message.messageId == sentMessage.messageId + }.first() + + edit( + sentMessage, + buildEntities { + regular("You have cancelled change of welcome message for chat ") + underline(groupMessage.chat.title) + } + ) + + answer(query) + }, + async { + val message = waitContentMessage().filter { + it.chat.id == sentMessage.chat.id + }.first() + + val success = welcomeTable.set( + ChatSettings( + groupMessage.chat.id, + message.chat.id, + message.messageId + ) + ) + + if (success) { + reply( + message, + buildEntities { + regular("Welcome message has been changed for chat ") + underline(groupMessage.chat.title) + regular(".\n\n") + bold("Please, do not delete this message if you want it to work and don't stop this bot to keep welcome message works right") + } + ) + } else { + reply( + message, + buildEntities { + regular("Something went wrong on welcome message changing for chat ") + underline(groupMessage.chat.title) + } + ) + } + }, + async { + while (isActive) { + delay(config.recheckOfAdmin) + + if (!userIsAdmin(user, groupMessage.chat)) { + edit(sentMessage, "Sorry, but you are not admin in chat ${groupMessage.chat.title} more") + break + } + } + } + ) + } } /** * Final configuration of bot. Here we are getting [Config] from [koin] */ override suspend fun BehaviourContext.setupBotPlugin(koin: Koin) { - val config = koin.getOrNull() + val config = koin.get() - if (config == null) { - log.w("Plugin has been disabled due to absence of \"$pluginConfigSectionName\" field in config or some error during configuration loading") - return + val welcomeTable = koin.get() + + onCommand( + "welcome", + initialFilter = { + it.chat is GroupChat + } + ) { + it.whenCommonGroupContentMessage { groupMessage -> + launch { + handleWelcomeCommand(welcomeTable, config, groupMessage) + } + } + } + + onNewChatMembers { + val chatSettings = welcomeTable.get(it.chat.id) + + if (chatSettings == null) { + return@onNewChatMembers + } + + try { + copyMessage( + it.chat.id, + chatSettings.sourceChatId, + chatSettings.sourceMessageId + ) + } catch (e: RequestException) { + welcomeTable.unset(it.chat.id) + } + } + + + allUpdatesFlow.subscribeSafelyWithoutExceptions(scope) { + println(it) } } diff --git a/welcome/src/main/kotlin/db/WelcomeTable.kt b/welcome/src/main/kotlin/db/WelcomeTable.kt new file mode 100644 index 0000000..62a3de1 --- /dev/null +++ b/welcome/src/main/kotlin/db/WelcomeTable.kt @@ -0,0 +1,44 @@ +package db + +import dev.inmo.micro_utils.repos.exposed.ExposedRepo +import dev.inmo.micro_utils.repos.exposed.initTable +import dev.inmo.tgbotapi.types.ChatId +import model.ChatSettings +import org.jetbrains.exposed.sql.* +import org.jetbrains.exposed.sql.transactions.transaction + +internal class WelcomeTable( + override val database: Database +) : Table("welcome"), ExposedRepo { + val targetChatIdColumn = long("targetChatId").uniqueIndex() + val sourceChatIdColumn = long("sourceChatId") + val sourceMessageIdColumn = long("sourceMessageId") + override val primaryKey: PrimaryKey = PrimaryKey(targetChatIdColumn) + + init { + initTable() + } + + fun get(chatId: ChatId): ChatSettings? = transaction(database) { + select { targetChatIdColumn.eq(chatId.chatId) }.limit(1).firstOrNull() ?.let { + ChatSettings( + ChatId(it[targetChatIdColumn]), + ChatId(it[sourceChatIdColumn]), + it[sourceMessageIdColumn] + ) + } + } + + fun set(chatSettings: ChatSettings): Boolean = transaction(database) { + deleteWhere { targetChatIdColumn.eq(chatSettings.targetChatId.chatId) } + insert { + it[targetChatIdColumn] = chatSettings.targetChatId.chatId + it[sourceChatIdColumn] = chatSettings.sourceChatId.chatId + it[sourceMessageIdColumn] = chatSettings.sourceMessageId + }.insertedCount > 0 + } + + fun unset(chatId: ChatId): Boolean = transaction(database) { + deleteWhere { targetChatIdColumn.eq(chatId.chatId) } > 0 + } +} diff --git a/welcome/src/main/kotlin/model/ChatSettings.kt b/welcome/src/main/kotlin/model/ChatSettings.kt new file mode 100644 index 0000000..17e668f --- /dev/null +++ b/welcome/src/main/kotlin/model/ChatSettings.kt @@ -0,0 +1,12 @@ +package model + +import dev.inmo.tgbotapi.types.ChatId +import dev.inmo.tgbotapi.types.MessageIdentifier +import kotlinx.serialization.Serializable + +@Serializable +internal data class ChatSettings( + val targetChatId: ChatId, + val sourceChatId: ChatId, + val sourceMessageId: MessageIdentifier +) From dc8b46dbd6007f7df1b8b3081d6185145052f8ec Mon Sep 17 00:00:00 2001 From: InsanusMokrassar Date: Sun, 10 Jul 2022 01:08:55 +0600 Subject: [PATCH 02/20] Update WelcomePlugin.kt --- welcome/src/main/kotlin/WelcomePlugin.kt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/welcome/src/main/kotlin/WelcomePlugin.kt b/welcome/src/main/kotlin/WelcomePlugin.kt index c795240..57f3697 100644 --- a/welcome/src/main/kotlin/WelcomePlugin.kt +++ b/welcome/src/main/kotlin/WelcomePlugin.kt @@ -58,7 +58,7 @@ class WelcomePlugin : Plugin { */ @Serializable private class Config( - val recheckOfAdmin: MilliSeconds = 60L + val recheckOfAdmin: MilliSeconds = 60000L ) /** From 47a762c8ba02876ddbb973d865c4051594bae398 Mon Sep 17 00:00:00 2001 From: InsanusMokrassar Date: Mon, 11 Jul 2022 00:48:45 +0600 Subject: [PATCH 03/20] fixes --- welcome/src/main/kotlin/WelcomePlugin.kt | 18 +++++------------- 1 file changed, 5 insertions(+), 13 deletions(-) diff --git a/welcome/src/main/kotlin/WelcomePlugin.kt b/welcome/src/main/kotlin/WelcomePlugin.kt index 57f3697..2bd617a 100644 --- a/welcome/src/main/kotlin/WelcomePlugin.kt +++ b/welcome/src/main/kotlin/WelcomePlugin.kt @@ -78,9 +78,6 @@ class WelcomePlugin : Plugin { val user = groupMessage.user if (userIsAdmin(user, groupMessage.chat)) { - val cancelData = "cancel_${groupMessage.chat.id}" - val unsetData = "unset_${groupMessage.chat.id}" - val sentMessage = sendMessage( user, buildEntities { @@ -197,9 +194,7 @@ class WelcomePlugin : Plugin { onCommand( "welcome", - initialFilter = { - it.chat is GroupChat - } + initialFilter = { it.chat is GroupChat } ) { it.whenCommonGroupContentMessage { groupMessage -> launch { @@ -216,8 +211,8 @@ class WelcomePlugin : Plugin { } try { - copyMessage( - it.chat.id, + reply( + it, chatSettings.sourceChatId, chatSettings.sourceMessageId ) @@ -225,14 +220,11 @@ class WelcomePlugin : Plugin { welcomeTable.unset(it.chat.id) } } - - - allUpdatesFlow.subscribeSafelyWithoutExceptions(scope) { - println(it) - } } companion object { private const val pluginConfigSectionName = "welcome" + private const val cancelData = "cancel" + private const val unsetData = "unset" } } From c6dad9105e89ed1eeeb7b7d33b6735f7deb954f5 Mon Sep 17 00:00:00 2001 From: InsanusMokrassar Date: Mon, 11 Jul 2022 01:33:53 +0600 Subject: [PATCH 04/20] small refactor of welcome plugin --- welcome/src/main/kotlin/WelcomePlugin.kt | 72 +++++++++++------------- 1 file changed, 32 insertions(+), 40 deletions(-) diff --git a/welcome/src/main/kotlin/WelcomePlugin.kt b/welcome/src/main/kotlin/WelcomePlugin.kt index 2bd617a..a3a36c3 100644 --- a/welcome/src/main/kotlin/WelcomePlugin.kt +++ b/welcome/src/main/kotlin/WelcomePlugin.kt @@ -10,10 +10,9 @@ import dev.inmo.tgbotapi.extensions.api.answers.answer import dev.inmo.tgbotapi.extensions.api.chat.get.getChatAdministrators import dev.inmo.tgbotapi.extensions.api.edit.edit import dev.inmo.tgbotapi.extensions.api.send.* -import dev.inmo.tgbotapi.extensions.behaviour_builder.BehaviourContext +import dev.inmo.tgbotapi.extensions.behaviour_builder.* import dev.inmo.tgbotapi.extensions.behaviour_builder.expectations.waitContentMessage import dev.inmo.tgbotapi.extensions.behaviour_builder.expectations.waitMessageDataCallbackQuery -import dev.inmo.tgbotapi.extensions.behaviour_builder.oneOf import dev.inmo.tgbotapi.extensions.behaviour_builder.triggers_handling.* import dev.inmo.tgbotapi.extensions.utils.* import dev.inmo.tgbotapi.extensions.utils.formatting.* @@ -93,34 +92,31 @@ class WelcomePlugin : Plugin { ) oneOf( - async { + parallel { val query = waitMessageDataCallbackQuery().filter { it.data == unsetData && it.message.chat.id == sentMessage.chat.id && it.message.messageId == sentMessage.messageId }.first() - if (welcomeTable.unset(groupMessage.chat.id)) { - edit( - sentMessage, - buildEntities { - regular("Welcome message has been removed for chat ") - underline(groupMessage.chat.title) - } - ) - } else { - edit( - sentMessage, - buildEntities { - regular("Something went wrong on welcome message unsetting for chat ") - underline(groupMessage.chat.title) - } - ) + val answerEntities = buildEntities { + if (welcomeTable.unset(groupMessage.chat.id)) { + regular("Welcome message has been removed for chat ") + underline(groupMessage.chat.title) + } else { + regular("Something went wrong on welcome message unsetting for chat ") + underline(groupMessage.chat.title) + } } + edit( + sentMessage, + answerEntities + ) + answer(query) }, - async { + parallel { val query = waitMessageDataCallbackQuery().filter { it.data == cancelData && it.message.chat.id == sentMessage.chat.id @@ -137,7 +133,7 @@ class WelcomePlugin : Plugin { answer(query) }, - async { + parallel { val message = waitContentMessage().filter { it.chat.id == sentMessage.chat.id }.first() @@ -150,27 +146,23 @@ class WelcomePlugin : Plugin { ) ) - if (success) { - reply( - message, - buildEntities { - regular("Welcome message has been changed for chat ") - underline(groupMessage.chat.title) - regular(".\n\n") - bold("Please, do not delete this message if you want it to work and don't stop this bot to keep welcome message works right") - } - ) - } else { - reply( - message, - buildEntities { - regular("Something went wrong on welcome message changing for chat ") - underline(groupMessage.chat.title) - } - ) + val entities = buildEntities { + if (success) { + regular("Welcome message has been changed for chat ") + underline(groupMessage.chat.title) + regular(".\n\n") + bold("Please, do not delete this message if you want it to work and don't stop this bot to keep welcome message works right") + } else { + regular("Something went wrong on welcome message changing for chat ") + underline(groupMessage.chat.title) + } } + reply( + message, + entities + ) }, - async { + parallel { while (isActive) { delay(config.recheckOfAdmin) From cf0b214ee8573337adfc95136e9bce225c826794 Mon Sep 17 00:00:00 2001 From: InsanusMokrassar Date: Mon, 11 Jul 2022 13:04:22 +0600 Subject: [PATCH 05/20] update dependencies --- gradle/libs.versions.toml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/gradle/libs.versions.toml b/gradle/libs.versions.toml index 3b62b06..9bb4732 100644 --- a/gradle/libs.versions.toml +++ b/gradle/libs.versions.toml @@ -1,9 +1,9 @@ [versions] kotlin = "1.6.21" -plagubot = "1.2.3" +plagubot = "1.3.0" kslog = "0.3.2" -plagubot-commands = "0.1.0" +plagubot-commands = "0.1.1" [libraries] From 33b2b4fbcb88cc88509c4318711587ed6a0aa291 Mon Sep 17 00:00:00 2001 From: InsanusMokrassar Date: Mon, 11 Jul 2022 13:07:42 +0600 Subject: [PATCH 06/20] refactor after dependencies update --- welcome/src/main/kotlin/WelcomePlugin.kt | 20 ++++++++------------ 1 file changed, 8 insertions(+), 12 deletions(-) diff --git a/welcome/src/main/kotlin/WelcomePlugin.kt b/welcome/src/main/kotlin/WelcomePlugin.kt index a3a36c3..40a45f6 100644 --- a/welcome/src/main/kotlin/WelcomePlugin.kt +++ b/welcome/src/main/kotlin/WelcomePlugin.kt @@ -15,6 +15,8 @@ import dev.inmo.tgbotapi.extensions.behaviour_builder.expectations.waitContentMe import dev.inmo.tgbotapi.extensions.behaviour_builder.expectations.waitMessageDataCallbackQuery import dev.inmo.tgbotapi.extensions.behaviour_builder.triggers_handling.* import dev.inmo.tgbotapi.extensions.utils.* +import dev.inmo.tgbotapi.extensions.utils.extensions.sameChat +import dev.inmo.tgbotapi.extensions.utils.extensions.sameMessage import dev.inmo.tgbotapi.extensions.utils.formatting.* import dev.inmo.tgbotapi.extensions.utils.types.buttons.* import dev.inmo.tgbotapi.types.BotCommand @@ -83,20 +85,16 @@ class WelcomePlugin : Plugin { regular("Ok, send me the message which should be used as welcome message for chat ") underline(groupMessage.chat.title) }, - replyMarkup = inlineKeyboard { - row { - dataButton("Unset", unsetData) - dataButton("Cancel", cancelData) - } + replyMarkup = flatInlineKeyboard { + dataButton("Unset", unsetData) + dataButton("Cancel", cancelData) } ) oneOf( parallel { val query = waitMessageDataCallbackQuery().filter { - it.data == unsetData - && it.message.chat.id == sentMessage.chat.id - && it.message.messageId == sentMessage.messageId + it.data == unsetData && it.message.sameMessage(sentMessage) }.first() val answerEntities = buildEntities { @@ -118,9 +116,7 @@ class WelcomePlugin : Plugin { }, parallel { val query = waitMessageDataCallbackQuery().filter { - it.data == cancelData - && it.message.chat.id == sentMessage.chat.id - && it.message.messageId == sentMessage.messageId + it.data == cancelData && it.message.sameMessage(sentMessage) }.first() edit( @@ -135,7 +131,7 @@ class WelcomePlugin : Plugin { }, parallel { val message = waitContentMessage().filter { - it.chat.id == sentMessage.chat.id + it.sameChat(sentMessage) }.first() val success = welcomeTable.set( From 4ee348190b10d90547eefbc6013d8c9f374bb97e Mon Sep 17 00:00:00 2001 From: InsanusMokrassar Date: Sat, 23 Jul 2022 00:07:11 +0600 Subject: [PATCH 07/20] Update libs.versions.toml --- gradle/libs.versions.toml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/gradle/libs.versions.toml b/gradle/libs.versions.toml index 9bb4732..2271c96 100644 --- a/gradle/libs.versions.toml +++ b/gradle/libs.versions.toml @@ -1,9 +1,9 @@ [versions] kotlin = "1.6.21" -plagubot = "1.3.0" +plagubot = "1.3.1" kslog = "0.3.2" -plagubot-commands = "0.1.1" +plagubot-commands = "0.1.2" [libraries] From 3e2e4e8cfd91765807a9d42ba0c3f9804f8e1357 Mon Sep 17 00:00:00 2001 From: InsanusMokrassar Date: Sun, 24 Jul 2022 12:08:19 +0600 Subject: [PATCH 08/20] Update libs.versions.toml --- gradle/libs.versions.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/gradle/libs.versions.toml b/gradle/libs.versions.toml index 2271c96..3602173 100644 --- a/gradle/libs.versions.toml +++ b/gradle/libs.versions.toml @@ -3,7 +3,7 @@ kotlin = "1.6.21" plagubot = "1.3.1" kslog = "0.3.2" -plagubot-commands = "0.1.2" +plagubot-commands = "0.1.3" [libraries] From d747d8a6be28b82e2a61f1df3b755421d3984ce5 Mon Sep 17 00:00:00 2001 From: InsanusMokrassar Date: Fri, 29 Jul 2022 22:54:23 +0600 Subject: [PATCH 09/20] update gradle version --- gradle/wrapper/gradle-wrapper.properties | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/gradle/wrapper/gradle-wrapper.properties b/gradle/wrapper/gradle-wrapper.properties index aa991fc..8049c68 100644 --- a/gradle/wrapper/gradle-wrapper.properties +++ b/gradle/wrapper/gradle-wrapper.properties @@ -1,5 +1,5 @@ distributionBase=GRADLE_USER_HOME distributionPath=wrapper/dists -distributionUrl=https\://services.gradle.org/distributions/gradle-7.4.2-bin.zip +distributionUrl=https\://services.gradle.org/distributions/gradle-7.5-bin.zip zipStoreBase=GRADLE_USER_HOME zipStorePath=wrapper/dists From c0a49ba869d5ff2eb1365f280c51e89dedd2b9f2 Mon Sep 17 00:00:00 2001 From: InsanusMokrassar Date: Wed, 3 Aug 2022 12:31:38 +0600 Subject: [PATCH 10/20] Update libs.versions.toml --- gradle/libs.versions.toml | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/gradle/libs.versions.toml b/gradle/libs.versions.toml index 3602173..944394c 100644 --- a/gradle/libs.versions.toml +++ b/gradle/libs.versions.toml @@ -1,9 +1,9 @@ [versions] kotlin = "1.6.21" -plagubot = "1.3.1" -kslog = "0.3.2" -plagubot-commands = "0.1.3" +plagubot = "1.4.1" +kslog = "0.4.2" +plagubot-commands = "0.2.1" [libraries] From e1ef47e676d2828ed4a6f780bea5bb0814c19e64 Mon Sep 17 00:00:00 2001 From: InsanusMokrassar Date: Sat, 6 Aug 2022 01:42:13 +0600 Subject: [PATCH 11/20] Update libs.versions.toml --- gradle/libs.versions.toml | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/gradle/libs.versions.toml b/gradle/libs.versions.toml index 944394c..0c4361d 100644 --- a/gradle/libs.versions.toml +++ b/gradle/libs.versions.toml @@ -1,9 +1,9 @@ [versions] -kotlin = "1.6.21" -plagubot = "1.4.1" -kslog = "0.4.2" -plagubot-commands = "0.2.1" +kotlin = "1.7.10" +plagubot = "2.0.0" +kslog = "0.5.0" +plagubot-commands = "0.3.0" [libraries] From 226ef5df24837a3489195cc43fbc19cbf8b5f38e Mon Sep 17 00:00:00 2001 From: InsanusMokrassar Date: Wed, 31 Aug 2022 11:13:40 +0600 Subject: [PATCH 12/20] Update libs.versions.toml --- gradle/libs.versions.toml | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/gradle/libs.versions.toml b/gradle/libs.versions.toml index 0c4361d..b310e12 100644 --- a/gradle/libs.versions.toml +++ b/gradle/libs.versions.toml @@ -1,9 +1,9 @@ [versions] kotlin = "1.7.10" -plagubot = "2.0.0" -kslog = "0.5.0" -plagubot-commands = "0.3.0" +plagubot = "2.2.0" +kslog = "0.5.1" +plagubot-commands = "0.3.2" [libraries] From c951f074aea5e344f83aeaf2c53835d861880471 Mon Sep 17 00:00:00 2001 From: InsanusMokrassar Date: Thu, 22 Sep 2022 14:28:49 +0600 Subject: [PATCH 13/20] updates --- gradle/libs.versions.toml | 6 ++-- gradle/wrapper/gradle-wrapper.properties | 2 +- welcome/src/main/kotlin/WelcomePlugin.kt | 37 +++++++++--------------- 3 files changed, 18 insertions(+), 27 deletions(-) diff --git a/gradle/libs.versions.toml b/gradle/libs.versions.toml index b310e12..9e1d8a6 100644 --- a/gradle/libs.versions.toml +++ b/gradle/libs.versions.toml @@ -1,9 +1,9 @@ [versions] kotlin = "1.7.10" -plagubot = "2.2.0" -kslog = "0.5.1" -plagubot-commands = "0.3.2" +plagubot = "2.3.3" +kslog = "0.5.2" +plagubot-commands = "0.3.4" [libraries] diff --git a/gradle/wrapper/gradle-wrapper.properties b/gradle/wrapper/gradle-wrapper.properties index 8049c68..ae04661 100644 --- a/gradle/wrapper/gradle-wrapper.properties +++ b/gradle/wrapper/gradle-wrapper.properties @@ -1,5 +1,5 @@ distributionBase=GRADLE_USER_HOME distributionPath=wrapper/dists -distributionUrl=https\://services.gradle.org/distributions/gradle-7.5-bin.zip +distributionUrl=https\://services.gradle.org/distributions/gradle-7.5.1-bin.zip zipStoreBase=GRADLE_USER_HOME zipStorePath=wrapper/dists diff --git a/welcome/src/main/kotlin/WelcomePlugin.kt b/welcome/src/main/kotlin/WelcomePlugin.kt index 40a45f6..c2386c9 100644 --- a/welcome/src/main/kotlin/WelcomePlugin.kt +++ b/welcome/src/main/kotlin/WelcomePlugin.kt @@ -26,6 +26,8 @@ import dev.inmo.tgbotapi.types.commands.BotCommandScope import dev.inmo.tgbotapi.types.message.abstracts.CommonGroupContentMessage import dev.inmo.tgbotapi.types.message.content.MessageContent import dev.inmo.tgbotapi.types.message.content.TextContent +import dev.inmo.tgbotapi.utils.regular +import dev.inmo.tgbotapi.utils.underline import kotlinx.coroutines.* import kotlinx.coroutines.flow.filter import kotlinx.coroutines.flow.first @@ -79,17 +81,16 @@ class WelcomePlugin : Plugin { val user = groupMessage.user if (userIsAdmin(user, groupMessage.chat)) { - val sentMessage = sendMessage( + val sentMessage = send( user, - buildEntities { - regular("Ok, send me the message which should be used as welcome message for chat ") - underline(groupMessage.chat.title) - }, replyMarkup = flatInlineKeyboard { dataButton("Unset", unsetData) dataButton("Cancel", cancelData) } - ) + ) { + regular("Ok, send me the message which should be used as welcome message for chat ") + underline(groupMessage.chat.title) + } oneOf( parallel { @@ -97,7 +98,7 @@ class WelcomePlugin : Plugin { it.data == unsetData && it.message.sameMessage(sentMessage) }.first() - val answerEntities = buildEntities { + edit(sentMessage) { if (welcomeTable.unset(groupMessage.chat.id)) { regular("Welcome message has been removed for chat ") underline(groupMessage.chat.title) @@ -107,11 +108,6 @@ class WelcomePlugin : Plugin { } } - edit( - sentMessage, - answerEntities - ) - answer(query) }, parallel { @@ -120,12 +116,11 @@ class WelcomePlugin : Plugin { }.first() edit( - sentMessage, - buildEntities { - regular("You have cancelled change of welcome message for chat ") - underline(groupMessage.chat.title) - } - ) + sentMessage + ) { + regular("You have cancelled change of welcome message for chat ") + underline(groupMessage.chat.title) + } answer(query) }, @@ -142,7 +137,7 @@ class WelcomePlugin : Plugin { ) ) - val entities = buildEntities { + reply(message) { if (success) { regular("Welcome message has been changed for chat ") underline(groupMessage.chat.title) @@ -153,10 +148,6 @@ class WelcomePlugin : Plugin { underline(groupMessage.chat.title) } } - reply( - message, - entities - ) }, parallel { while (isActive) { From 05ddfadb037f6b0944488e68ae52038a90e83ad0 Mon Sep 17 00:00:00 2001 From: InsanusMokrassar Date: Thu, 22 Sep 2022 14:29:09 +0600 Subject: [PATCH 14/20] update dependencies --- welcome/src/main/kotlin/WelcomePlugin.kt | 23 ++++++++++++----------- 1 file changed, 12 insertions(+), 11 deletions(-) diff --git a/welcome/src/main/kotlin/WelcomePlugin.kt b/welcome/src/main/kotlin/WelcomePlugin.kt index c2386c9..f95c252 100644 --- a/welcome/src/main/kotlin/WelcomePlugin.kt +++ b/welcome/src/main/kotlin/WelcomePlugin.kt @@ -1,38 +1,39 @@ +import WelcomePlugin.Companion.pluginConfigSectionName +import WelcomePlugin.Config import db.WelcomeTable import dev.inmo.kslog.common.logger -import dev.inmo.kslog.common.w -import dev.inmo.micro_utils.coroutines.runCatchingSafely -import dev.inmo.micro_utils.coroutines.subscribeSafelyWithoutExceptions import dev.inmo.plagubot.Plugin import dev.inmo.plagubot.plugins.commands.full import dev.inmo.tgbotapi.bot.exceptions.RequestException import dev.inmo.tgbotapi.extensions.api.answers.answer -import dev.inmo.tgbotapi.extensions.api.chat.get.getChatAdministrators import dev.inmo.tgbotapi.extensions.api.edit.edit -import dev.inmo.tgbotapi.extensions.api.send.* +import dev.inmo.tgbotapi.extensions.api.send.reply +import dev.inmo.tgbotapi.extensions.api.send.send import dev.inmo.tgbotapi.extensions.behaviour_builder.* import dev.inmo.tgbotapi.extensions.behaviour_builder.expectations.waitContentMessage import dev.inmo.tgbotapi.extensions.behaviour_builder.expectations.waitMessageDataCallbackQuery -import dev.inmo.tgbotapi.extensions.behaviour_builder.triggers_handling.* -import dev.inmo.tgbotapi.extensions.utils.* +import dev.inmo.tgbotapi.extensions.behaviour_builder.triggers_handling.onCommand +import dev.inmo.tgbotapi.extensions.behaviour_builder.triggers_handling.onNewChatMembers import dev.inmo.tgbotapi.extensions.utils.extensions.sameChat import dev.inmo.tgbotapi.extensions.utils.extensions.sameMessage -import dev.inmo.tgbotapi.extensions.utils.formatting.* -import dev.inmo.tgbotapi.extensions.utils.types.buttons.* +import dev.inmo.tgbotapi.extensions.utils.formatting.bold +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.types.BotCommand import dev.inmo.tgbotapi.types.MilliSeconds import dev.inmo.tgbotapi.types.chat.GroupChat import dev.inmo.tgbotapi.types.commands.BotCommandScope import dev.inmo.tgbotapi.types.message.abstracts.CommonGroupContentMessage import dev.inmo.tgbotapi.types.message.content.MessageContent -import dev.inmo.tgbotapi.types.message.content.TextContent import dev.inmo.tgbotapi.utils.regular import dev.inmo.tgbotapi.utils.underline import kotlinx.coroutines.* import kotlinx.coroutines.flow.filter import kotlinx.coroutines.flow.first import kotlinx.serialization.Serializable -import kotlinx.serialization.json.* +import kotlinx.serialization.json.Json +import kotlinx.serialization.json.JsonObject import model.ChatSettings import org.jetbrains.exposed.sql.Database import org.koin.core.Koin From 6cefafbd095bec80b294777738a0736ba7d1423b Mon Sep 17 00:00:00 2001 From: InsanusMokrassar Date: Thu, 22 Sep 2022 14:30:01 +0600 Subject: [PATCH 15/20] remove redundant imports in WelcomePlugin --- welcome/src/main/kotlin/WelcomePlugin.kt | 2 -- 1 file changed, 2 deletions(-) diff --git a/welcome/src/main/kotlin/WelcomePlugin.kt b/welcome/src/main/kotlin/WelcomePlugin.kt index f95c252..d452c18 100644 --- a/welcome/src/main/kotlin/WelcomePlugin.kt +++ b/welcome/src/main/kotlin/WelcomePlugin.kt @@ -1,5 +1,3 @@ -import WelcomePlugin.Companion.pluginConfigSectionName -import WelcomePlugin.Config import db.WelcomeTable import dev.inmo.kslog.common.logger import dev.inmo.plagubot.Plugin From 0f774754347f443c0a22904b0caa1209d38c55a2 Mon Sep 17 00:00:00 2001 From: InsanusMokrassar Date: Thu, 22 Sep 2022 15:00:40 +0600 Subject: [PATCH 16/20] add admins cache api usage --- gradle/libs.versions.toml | 2 ++ welcome/build.gradle | 1 + welcome/src/main/kotlin/UserAdminChecker.kt | 11 ----------- welcome/src/main/kotlin/WelcomePlugin.kt | 14 +++++++++----- 4 files changed, 12 insertions(+), 16 deletions(-) delete mode 100644 welcome/src/main/kotlin/UserAdminChecker.kt diff --git a/gradle/libs.versions.toml b/gradle/libs.versions.toml index 9e1d8a6..4c986e5 100644 --- a/gradle/libs.versions.toml +++ b/gradle/libs.versions.toml @@ -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 diff --git a/welcome/build.gradle b/welcome/build.gradle index ddb46de..e0b0ad7 100644 --- a/welcome/build.gradle +++ b/welcome/build.gradle @@ -24,4 +24,5 @@ dependencies { api libs.plagubot.plugin api libs.kslog api libs.plagubot.commands + api libs.tgbotapi.libraries.admins } diff --git a/welcome/src/main/kotlin/UserAdminChecker.kt b/welcome/src/main/kotlin/UserAdminChecker.kt deleted file mode 100644 index 9217c19..0000000 --- a/welcome/src/main/kotlin/UserAdminChecker.kt +++ /dev/null @@ -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 -} diff --git a/welcome/src/main/kotlin/WelcomePlugin.kt b/welcome/src/main/kotlin/WelcomePlugin.kt index d452c18..2b9b171 100644 --- a/welcome/src/main/kotlin/WelcomePlugin.kt +++ b/welcome/src/main/kotlin/WelcomePlugin.kt @@ -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 ) { 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() val welcomeTable = koin.get() + val adminsCacheAPI = koin.get() onCommand( "welcome", initialFilter = { it.chat is GroupChat } ) { - it.whenCommonGroupContentMessage { groupMessage -> + it.ifCommonGroupContentMessage { groupMessage -> launch { - handleWelcomeCommand(welcomeTable, config, groupMessage) + handleWelcomeCommand(adminsCacheAPI, welcomeTable, config, groupMessage) } } } From 8634007e06781e6b63d3339c9f0db3f4d2ecd3c3 Mon Sep 17 00:00:00 2001 From: InsanusMokrassar Date: Thu, 22 Sep 2022 15:13:27 +0600 Subject: [PATCH 17/20] small import fixes --- welcome/src/main/kotlin/WelcomePlugin.kt | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/welcome/src/main/kotlin/WelcomePlugin.kt b/welcome/src/main/kotlin/WelcomePlugin.kt index 2b9b171..7f16d4b 100644 --- a/welcome/src/main/kotlin/WelcomePlugin.kt +++ b/welcome/src/main/kotlin/WelcomePlugin.kt @@ -14,11 +14,9 @@ import dev.inmo.tgbotapi.extensions.behaviour_builder.triggers_handling.onComman import dev.inmo.tgbotapi.extensions.behaviour_builder.triggers_handling.onNewChatMembers 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 @@ -26,8 +24,7 @@ import dev.inmo.tgbotapi.types.chat.GroupChat import dev.inmo.tgbotapi.types.commands.BotCommandScope import dev.inmo.tgbotapi.types.message.abstracts.CommonGroupContentMessage import dev.inmo.tgbotapi.types.message.content.MessageContent -import dev.inmo.tgbotapi.utils.regular -import dev.inmo.tgbotapi.utils.underline +import dev.inmo.tgbotapi.utils.* import kotlinx.coroutines.* import kotlinx.coroutines.flow.filter import kotlinx.coroutines.flow.first From 2ba35e7b9b6dae7b84869d77893ced077779d89e Mon Sep 17 00:00:00 2001 From: InsanusMokrassar Date: Thu, 22 Sep 2022 15:24:07 +0600 Subject: [PATCH 18/20] replace unset of welcome message by logging of errors on welcome sending --- welcome/src/main/kotlin/WelcomePlugin.kt | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/welcome/src/main/kotlin/WelcomePlugin.kt b/welcome/src/main/kotlin/WelcomePlugin.kt index 7f16d4b..c7473b0 100644 --- a/welcome/src/main/kotlin/WelcomePlugin.kt +++ b/welcome/src/main/kotlin/WelcomePlugin.kt @@ -1,4 +1,5 @@ import db.WelcomeTable +import dev.inmo.kslog.common.e import dev.inmo.kslog.common.logger import dev.inmo.plagubot.Plugin import dev.inmo.plagubot.plugins.commands.full @@ -196,7 +197,7 @@ class WelcomePlugin : Plugin { chatSettings.sourceMessageId ) } catch (e: RequestException) { - welcomeTable.unset(it.chat.id) + log.e("Unable to send welcome message", e) } } } From 7483988faa88da85813b1b526314ed642ac93bd1 Mon Sep 17 00:00:00 2001 From: InsanusMokrassar Date: Thu, 22 Sep 2022 15:24:53 +0600 Subject: [PATCH 19/20] remove handling of welcome message sending errors --- welcome/src/main/kotlin/WelcomePlugin.kt | 14 +++++--------- 1 file changed, 5 insertions(+), 9 deletions(-) diff --git a/welcome/src/main/kotlin/WelcomePlugin.kt b/welcome/src/main/kotlin/WelcomePlugin.kt index c7473b0..091add3 100644 --- a/welcome/src/main/kotlin/WelcomePlugin.kt +++ b/welcome/src/main/kotlin/WelcomePlugin.kt @@ -190,15 +190,11 @@ class WelcomePlugin : Plugin { return@onNewChatMembers } - try { - reply( - it, - chatSettings.sourceChatId, - chatSettings.sourceMessageId - ) - } catch (e: RequestException) { - log.e("Unable to send welcome message", e) - } + reply( + it, + chatSettings.sourceChatId, + chatSettings.sourceMessageId + ) } } From eea5fcc360cbf2388e88277ca367da2524939923 Mon Sep 17 00:00:00 2001 From: InsanusMokrassar Date: Thu, 22 Sep 2022 16:06:21 +0600 Subject: [PATCH 20/20] fixes and improvements --- welcome/src/main/kotlin/WelcomePlugin.kt | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/welcome/src/main/kotlin/WelcomePlugin.kt b/welcome/src/main/kotlin/WelcomePlugin.kt index 091add3..b1dc48a 100644 --- a/welcome/src/main/kotlin/WelcomePlugin.kt +++ b/welcome/src/main/kotlin/WelcomePlugin.kt @@ -5,6 +5,7 @@ import dev.inmo.plagubot.Plugin import dev.inmo.plagubot.plugins.commands.full import dev.inmo.tgbotapi.bot.exceptions.RequestException import dev.inmo.tgbotapi.extensions.api.answers.answer +import dev.inmo.tgbotapi.extensions.api.delete import dev.inmo.tgbotapi.extensions.api.edit.edit import dev.inmo.tgbotapi.extensions.api.send.reply import dev.inmo.tgbotapi.extensions.api.send.send @@ -81,10 +82,13 @@ class WelcomePlugin : Plugin { val user = groupMessage.user if (adminsCacheAPI.isAdmin(groupMessage.chat.id, user.id)) { + val previousMessage = welcomeTable.get(groupMessage.chat.id) val sentMessage = send( user, replyMarkup = flatInlineKeyboard { - dataButton("Unset", unsetData) + if (previousMessage != null) { + dataButton("Unset", unsetData) + } dataButton("Cancel", cancelData) } ) { @@ -115,9 +119,7 @@ class WelcomePlugin : Plugin { it.data == cancelData && it.message.sameMessage(sentMessage) }.first() - edit( - sentMessage - ) { + edit(sentMessage) { regular("You have cancelled change of welcome message for chat ") underline(groupMessage.chat.title) } @@ -148,6 +150,7 @@ class WelcomePlugin : Plugin { underline(groupMessage.chat.title) } } + delete(sentMessage) }, parallel { while (isActive) {