All Downloads are FREE. Search and download functionalities are using the official Maven repository.

utils.Settings.kt Maven / Gradle / Ivy

The newest version!
package dev.inmo.plagubot.plugins.inline.buttons.utils

import dev.inmo.plagubot.plugins.inline.buttons.InlineButtonsPlugin
import dev.inmo.tgbotapi.extensions.api.bot.getMe
import dev.inmo.tgbotapi.extensions.api.send.reply
import dev.inmo.tgbotapi.extensions.api.send.send
import dev.inmo.tgbotapi.extensions.behaviour_builder.BehaviourContext
import dev.inmo.tgbotapi.extensions.behaviour_builder.triggers_handling.onCommand
import dev.inmo.tgbotapi.extensions.utils.formatting.makeUsernameLink
import dev.inmo.tgbotapi.extensions.utils.groupChatOrThrow
import dev.inmo.tgbotapi.extensions.utils.ifFromUser
import dev.inmo.tgbotapi.libraries.cache.admins.AdminsCacheAPI
import dev.inmo.tgbotapi.libraries.cache.admins.doAfterVerification
import dev.inmo.tgbotapi.types.chat.ExtendedBot
import dev.inmo.tgbotapi.utils.code
import dev.inmo.tgbotapi.utils.link
import org.koin.core.Koin

suspend fun BehaviourContext.enableSettings(koin: Koin, plugin: InlineButtonsPlugin) {
    val adminsApi = koin.get()
    val me = koin.getOrNull() ?: getMe()
    onCommand("settings") { commandMessage ->
        val verified = commandMessage.doAfterVerification(adminsApi) {
            commandMessage.ifFromUser {
                runCatching {
                    send(
                        it.user.id,
                        replyMarkup = plugin.createProvidersInlineKeyboard(commandMessage.chat.id, InlineButtonsKeys.Settings)
                    ) {
                        +"Settings for chat "
                        code(commandMessage.chat.groupChatOrThrow().title)
                    }
                }.onFailure {
                    it.printStackTrace()
                    reply(
                        commandMessage
                    ) {
                        me.username ?.let {
                            +"Looks like you didn't started the bot. Please, "
                            link("start", makeUsernameLink(it.withoutAt))
                            +" bot and try again"
                        } ?: let {
                            +"Looks like you didn't started the bot. Please, start dialog with me and try again"
                        }
                    }
                }
            }
            true
        }
        if (verified == true) {
            return@onCommand
        }
        reply(commandMessage, "Only admins may trigger settings")
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy