Merge pull request #1 from InsanusMokrassar/0.1.0

0.1.0
This commit is contained in:
InsanusMokrassar 2022-07-10 00:33:52 +06:00 committed by GitHub
commit 7a62373030
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 91 additions and 9 deletions

View File

@ -1,6 +1,76 @@
# PlaguBot Plugin Template # PlaguBotCommandsPlugin
1. Override project name in [settings.gradle](https://github.com/InsanusMokrassar/PlaguBotPluginTemplate/blob/master/settings.gradle) [![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)
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) This plugin has been created for centralized work with commands in your plugins. You may pass your commands (even in
4. Fill your [plugin logic](https://github.com/InsanusMokrassar/PlaguBotPluginTemplate/blob/master/src/main/kotlin/plagubot_plugin/Plugin.kt#L18) runtime) and they will automatically appear in bot commands for users
## 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<CommandsKeeper>()` 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
<dependency>
<groupId>dev.inmo</groupId>
<artifactId>plagubot.plugins.commands</artifactId>
<version>${commands_version}</version>
</dependency>
```

View File

@ -1,8 +1,9 @@
[versions] [versions]
kotlin = "1.6.21" kotlin = "1.6.21"
plagubot = "1.2.2" plagubot = "1.2.3"
kslog = "0.3.2" kslog = "0.3.2"
gh-release = "2.4.1"
[libraries] [libraries]

View File

@ -7,6 +7,7 @@ import dev.inmo.plagubot.Plugin
import dev.inmo.tgbotapi.extensions.api.bot.* import dev.inmo.tgbotapi.extensions.api.bot.*
import dev.inmo.tgbotapi.extensions.behaviour_builder.BehaviourContext import dev.inmo.tgbotapi.extensions.behaviour_builder.BehaviourContext
import dev.inmo.tgbotapi.types.BotCommand import dev.inmo.tgbotapi.types.BotCommand
import dev.inmo.tgbotapi.types.botCommandsLimit
import kotlinx.serialization.Serializable import kotlinx.serialization.Serializable
import kotlinx.serialization.json.JsonObject import kotlinx.serialization.json.JsonObject
import org.jetbrains.exposed.sql.Database import org.jetbrains.exposed.sql.Database
@ -19,7 +20,7 @@ import org.koin.core.module.Module
* flexible setup of commands in runtime. * flexible setup of commands in runtime.
*/ */
@Serializable @Serializable
class CommandsPlugin : Plugin { object CommandsPlugin : Plugin {
private val log = KSLog(logTag) private val log = KSLog(logTag)
/** /**
@ -34,7 +35,7 @@ class CommandsPlugin : Plugin {
runCatchingSafely { runCatchingSafely {
commands ?.let { commands ?.let {
setMyCommands( setMyCommands(
commands, commands.distinctBy { it.command }.take(botCommandsLimit.last + 1),
key.scope, key.scope,
key.languageCode key.languageCode
) )
@ -58,7 +59,7 @@ class CommandsPlugin : Plugin {
* take all the available keys in the [CommandsKeeper] and set commands for each key * take all the available keys in the [CommandsKeeper] and set commands for each key
*/ */
override suspend fun BehaviourContext.setupBotPlugin(koin: Koin) { override suspend fun BehaviourContext.setupBotPlugin(koin: Koin) {
val commandsKeeper = koin.get<CommandsKeeper>() val commandsKeeper = koin.commandsKeeper
log.d { "Subscribe to scopes changed flow" } log.d { "Subscribe to scopes changed flow" }
commandsKeeper.onScopeChanged.subscribeSafelyWithoutExceptions(scope) { commandsKeeper.onScopeChanged.subscribeSafelyWithoutExceptions(scope) {

View File

@ -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<CommandsKeeper>()
val Koin.commandsKeeper
get() = get<CommandsKeeper>()