add request command

This commit is contained in:
InsanusMokrassar 2022-09-09 16:19:14 +06:00
parent e764358929
commit 296be3a4e3
2 changed files with 36 additions and 15 deletions

View File

@ -1,14 +1,13 @@
import dev.inmo.krontab.utils.asFlow
import dev.inmo.micro_utils.coroutines.*
import dev.inmo.micro_utils.pagination.utils.doForAllWithNextPaging
import dev.inmo.micro_utils.repos.add
import dev.inmo.micro_utils.repos.*
import dev.inmo.micro_utils.repos.cache.cache.FullKVCache
import dev.inmo.micro_utils.repos.cache.cache.KVCache
import dev.inmo.micro_utils.repos.cache.cached
import dev.inmo.micro_utils.repos.exposed.keyvalue.ExposedKeyValueRepo
import dev.inmo.micro_utils.repos.exposed.onetomany.ExposedKeyValuesRepo
import dev.inmo.micro_utils.repos.mappers.withMapper
import dev.inmo.micro_utils.repos.unset
import dev.inmo.tgbotapi.bot.ktor.telegramBot
import dev.inmo.tgbotapi.extensions.api.bot.getMe
import dev.inmo.tgbotapi.extensions.api.bot.setMyCommands
@ -22,7 +21,6 @@ import dev.inmo.tgbotapi.types.*
import dev.inmo.tgbotapi.types.chat.ChannelChat
import dev.inmo.tgbotapi.types.chat.PrivateChat
import dev.inmo.tgbotapi.types.media.TelegramMediaPhoto
import dev.inmo.tgbotapi.types.message.content.PhotoContent
import java.io.File
import kotlinx.coroutines.*
import kotlinx.coroutines.sync.Mutex
@ -97,7 +95,7 @@ suspend fun main(args: Array<String>) {
result.dropLast(toDrop)
}.takeIf { it.isNotEmpty() } ?: return
runCatchingSafely {
val urls = result.map { it.url.also(::println) }
val urls = result.map { it.url }
chatsUrlsSeen.add(chatId, urls)
when {
urls.isEmpty() -> return@runCatchingSafely
@ -155,13 +153,14 @@ suspend fun main(args: Array<String>) {
}
onCommand(Regex("(help|start)"), requireOnlyCommandInMessage = true) {
reply(it, EnableArgsParser(it.chat.id, repo, scope).getFormattedHelp().takeIf { it.isNotBlank() } ?: return@onCommand)
reply(it, EnableArgsParser().getFormattedHelp().takeIf { it.isNotBlank() } ?: return@onCommand)
}
onCommand("enable", requireOnlyCommandInMessage = false) {
val args = it.content.textSources.drop(1).joinToString("") { it.source }.split(" ")
val parser = EnableArgsParser(it.chat.id, repo, this)
val parser = EnableArgsParser()
runCatchingSafely {
parser.parse(args)
repo.set(it.chat.id, parser.resultSettings ?: return@runCatchingSafely)
}.onFailure { e ->
e.printStackTrace()
if (it.chat is PrivateChat) {
@ -174,6 +173,31 @@ suspend fun main(args: Array<String>) {
}
}
}
onCommand("request", requireOnlyCommandInMessage = false) {
val args = it.content.textSources.drop(1).joinToString("") { it.source }.split(" ")
val chatSettings = if (args.isEmpty()) {
repo.get(it.chat.id) ?: run {
if (it.chat is PrivateChat) {
reply(it, "Unable to find default config")
}
return@onCommand
}
} else {
val parser = EnableArgsParser()
runCatchingSafely {
parser.parse(args)
parser.resultSettings
}.onFailure { e ->
e.printStackTrace()
if (it.chat is PrivateChat) {
reply(it, parser.getFormattedHelp())
}
}.getOrNull()
}
triggerSendForChat(it.chat.id, chatSettings ?: return@onCommand)
}
onCommand("disable", requireOnlyCommandInMessage = true) {
runCatchingSafely {
repo.unset(it.chat.id)
@ -187,6 +211,7 @@ suspend fun main(args: Array<String>) {
listOf(
BotCommand("start", "Will return the help for the enable command"),
BotCommand("help", "Will return the help for the enable command"),
BotCommand("request", "Will trigger image immediately with custom settings from arguments or default settings of chat if any"),
BotCommand("enable", "Will enable images grabbing for current chat or update exists settings"),
BotCommand("disable", "Will disable bot for current chat"),
)

View File

@ -8,11 +8,7 @@ import dev.inmo.micro_utils.repos.set
import dev.inmo.tgbotapi.types.ChatId
import kotlinx.coroutines.CoroutineScope
class EnableArgsParser(
private val chatId: ChatId,
private val repo: KeyValueRepo<ChatId, ChatSettings>,
private val scope: CoroutineScope
) : CliktCommand(name = "enable") {
class EnableArgsParser: CliktCommand(name = "enable") {
val count by option("-n").int().help("Amount of pictures to grab each trigger time").default(1).check("Count should be in range 1-10") {
it in 1 .. 10
}
@ -25,16 +21,16 @@ class EnableArgsParser(
}.required().help("Board type. Possible values: ${ChatSettings.BoardSerializer.types.keys.joinToString { it }}")
val gallery by option("-g", "--gallery").flag(default = false).help("Effective only when count passed > 1. Will send chosen images as gallery instead of separated images")
var resultSettings: ChatSettings? = null
private set
override fun run() {
val chatSettings = ChatSettings(
resultSettings = ChatSettings(
query.filterNot { it.isEmpty() }.joinToString(" ").trim(),
krontab,
board,
count,
gallery
)
scope.launchSafelyWithoutExceptions {
repo.set(chatId, chatSettings)
}
}
}