From 0dcf9086c44aae50295b1111e678a90b1e26333e Mon Sep 17 00:00:00 2001 From: InsanusMokrassar Date: Sat, 9 Jul 2022 20:52:47 +0600 Subject: [PATCH 1/6] update versions toml --- gradle/libs.versions.toml | 1 + 1 file changed, 1 insertion(+) diff --git a/gradle/libs.versions.toml b/gradle/libs.versions.toml index fc364c5..61884d4 100644 --- a/gradle/libs.versions.toml +++ b/gradle/libs.versions.toml @@ -3,6 +3,7 @@ kotlin = "1.6.21" plagubot = "1.2.2" kslog = "0.3.2" +gh-release = "2.4.1" [libraries] From a2345fe496cb4127d3ebfe566220b25975cc4397 Mon Sep 17 00:00:00 2001 From: InsanusMokrassar Date: Sat, 9 Jul 2022 21:09:13 +0600 Subject: [PATCH 2/6] fill README --- README.md | 79 +++++++++++++++++++-- src/main/kotlin/CommandsPlugin.kt | 4 +- src/main/kotlin/KoinCommandKeeperGetters.kt | 10 +++ 3 files changed, 86 insertions(+), 7 deletions(-) create mode 100644 src/main/kotlin/KoinCommandKeeperGetters.kt diff --git a/README.md b/README.md index e1a801b..619177f 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,75 @@ -# PlaguBot Plugin Template +# PlaguBotCommandsPlugin -1. Override project name in [settings.gradle](https://github.com/InsanusMokrassar/PlaguBotPluginTemplate/blob/master/settings.gradle) -2. Change your [package](https://github.com/InsanusMokrassar/PlaguBotPluginTemplate/blob/master/src/main/kotlin) -3. Rename your [plugin](https://github.com/InsanusMokrassar/PlaguBotPluginTemplate/blob/master/src/main/kotlin/plagubot_plugin/Plugin.kt) -4. Fill your [plugin logic](https://github.com/InsanusMokrassar/PlaguBotPluginTemplate/blob/master/src/main/kotlin/plagubot_plugin/Plugin.kt#L18) +[![Maven Central](https://maven-badges.herokuapp.com/maven-central/dev.inmo/plagubot.plugins.commands/badge.svg)](https://maven-badges.herokuapp.com/maven-central/dev.inmo/plagubot.plugins.commands) + +This plugin has been created for centralized work with commands in your plugins. + +## How to use + +End user should include in his plugins section next line: + +```json +... +"plugins": [ + ..., + "dev.inmo.plagubot.plugins.commands.CommandsPlugin" +], +... +``` + +Then, in your plugin you should register your commands. Just pass them inside `setupDI` as `BotCommandFullInfo`: + +```kotlin +// ... Your plugin code + override fun Module.setupDI(database: Database, params: JsonObject) { + // ... + single { BotCommand("commandExample", "Command description").full() } + // ... + } +// ... +``` + +You may pass info `full` extension: + +* Any [BotCommandScope](https://tgbotapi.inmo.dev/docs/dev.inmo.tgbotapi.types.commands/-bot-command-scope/index.html) +* Some language code as `String` OR: +* Any [IetfLanguageCode](https://microutils.inmo.dev/micro_utils.dokka/dev.inmo.micro_utils.language_codes/%5Bcommon%5D-ietf-language-code/index.html) + +### Runtime commands changing + +In runtime you may change the commands of bot using `CommandsKeeper`. The instance of `CommandsKeeper` can be +retrieved from `koin` via simple `koin.get()` or `koin.commandsKeeper`: + +```kotlin +// ... + override suspend fun BehaviourContext.setupBotPlugin(koin: Koin) { + val commandsKeeper = koin.commandsKeeper + // ... Some your code + commandsKeeper.addCommand(BotCommand("commandExample", "Command description")) + // ... Some your code + } +// ... +``` + +Just as in the code above (in `setupDI`) you may pass all the command environment and it will be automatically updated +for bot. + +## How to include + +Add dependency: + +Gradle: + +```groovy +api "dev.inmo:plagubot.plugins.commands:$commands_version" +``` + +Maven: + +```xml + + dev.inmo + plagubot.plugins.commands + ${$commands_version} + +``` diff --git a/src/main/kotlin/CommandsPlugin.kt b/src/main/kotlin/CommandsPlugin.kt index 210e733..e0aed0a 100644 --- a/src/main/kotlin/CommandsPlugin.kt +++ b/src/main/kotlin/CommandsPlugin.kt @@ -34,7 +34,7 @@ class CommandsPlugin : Plugin { runCatchingSafely { commands ?.let { setMyCommands( - commands, + commands.distinctBy { it.command }, key.scope, key.languageCode ) @@ -58,7 +58,7 @@ class CommandsPlugin : Plugin { * take all the available keys in the [CommandsKeeper] and set commands for each key */ override suspend fun BehaviourContext.setupBotPlugin(koin: Koin) { - val commandsKeeper = koin.get() + val commandsKeeper = koin.commandsKeeper log.d { "Subscribe to scopes changed flow" } commandsKeeper.onScopeChanged.subscribeSafelyWithoutExceptions(scope) { diff --git a/src/main/kotlin/KoinCommandKeeperGetters.kt b/src/main/kotlin/KoinCommandKeeperGetters.kt new file mode 100644 index 0000000..0e29d9f --- /dev/null +++ b/src/main/kotlin/KoinCommandKeeperGetters.kt @@ -0,0 +1,10 @@ +package dev.inmo.plagubot.plugins.commands + +import org.koin.core.Koin +import org.koin.core.scope.Scope + +val Scope.commandsKeeper + get() = get() + +val Koin.commandsKeeper + get() = get() From 42ff5262f508d0f98d70f6bd5e64b59c87a20370 Mon Sep 17 00:00:00 2001 From: InsanusMokrassar Date: Sat, 9 Jul 2022 21:11:55 +0600 Subject: [PATCH 3/6] update readme --- README.md | 3 ++- src/main/kotlin/CommandsPlugin.kt | 3 ++- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 619177f..390d42c 100644 --- a/README.md +++ b/README.md @@ -2,7 +2,8 @@ [![Maven Central](https://maven-badges.herokuapp.com/maven-central/dev.inmo/plagubot.plugins.commands/badge.svg)](https://maven-badges.herokuapp.com/maven-central/dev.inmo/plagubot.plugins.commands) -This plugin has been created for centralized work with commands in your plugins. +This plugin has been created for centralized work with commands in your plugins. You may pass your commands (even in +runtime) and they will automatically appear in bot commands for users ## How to use diff --git a/src/main/kotlin/CommandsPlugin.kt b/src/main/kotlin/CommandsPlugin.kt index e0aed0a..e7f023c 100644 --- a/src/main/kotlin/CommandsPlugin.kt +++ b/src/main/kotlin/CommandsPlugin.kt @@ -7,6 +7,7 @@ import dev.inmo.plagubot.Plugin import dev.inmo.tgbotapi.extensions.api.bot.* import dev.inmo.tgbotapi.extensions.behaviour_builder.BehaviourContext import dev.inmo.tgbotapi.types.BotCommand +import dev.inmo.tgbotapi.types.botCommandsLimit import kotlinx.serialization.Serializable import kotlinx.serialization.json.JsonObject import org.jetbrains.exposed.sql.Database @@ -34,7 +35,7 @@ class CommandsPlugin : Plugin { runCatchingSafely { commands ?.let { setMyCommands( - commands.distinctBy { it.command }, + commands.distinctBy { it.command }.take(botCommandsLimit.last + 1), key.scope, key.languageCode ) From d9c86039a17ab1c90f336a8608baa323e7ced950 Mon Sep 17 00:00:00 2001 From: InsanusMokrassar Date: Sat, 9 Jul 2022 21:57:31 +0600 Subject: [PATCH 4/6] Update CommandsPlugin.kt --- src/main/kotlin/CommandsPlugin.kt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/kotlin/CommandsPlugin.kt b/src/main/kotlin/CommandsPlugin.kt index e7f023c..2e6f496 100644 --- a/src/main/kotlin/CommandsPlugin.kt +++ b/src/main/kotlin/CommandsPlugin.kt @@ -20,7 +20,7 @@ import org.koin.core.module.Module * flexible setup of commands in runtime. */ @Serializable -class CommandsPlugin : Plugin { +object CommandsPlugin : Plugin { private val log = KSLog(logTag) /** From 375def33fcf8836c0f944e5b10fa9ca4dd2b0c22 Mon Sep 17 00:00:00 2001 From: InsanusMokrassar Date: Sat, 9 Jul 2022 21:57:47 +0600 Subject: [PATCH 5/6] Update README.md --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 390d42c..a3ed23c 100644 --- a/README.md +++ b/README.md @@ -71,6 +71,6 @@ Maven: dev.inmo plagubot.plugins.commands - ${$commands_version} + ${commands_version} ``` From d41293a0a3af132889074adeed947d4f7af82f66 Mon Sep 17 00:00:00 2001 From: InsanusMokrassar Date: Sun, 10 Jul 2022 00:06:45 +0600 Subject: [PATCH 6/6] 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 61884d4..52d5caf 100644 --- a/gradle/libs.versions.toml +++ b/gradle/libs.versions.toml @@ -1,7 +1,7 @@ [versions] kotlin = "1.6.21" -plagubot = "1.2.2" +plagubot = "1.2.3" kslog = "0.3.2" gh-release = "2.4.1"