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.krontab.utils.asFlow
import dev.inmo.micro_utils.coroutines.* import dev.inmo.micro_utils.coroutines.*
import dev.inmo.micro_utils.pagination.utils.doForAllWithNextPaging 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.FullKVCache
import dev.inmo.micro_utils.repos.cache.cache.KVCache import dev.inmo.micro_utils.repos.cache.cache.KVCache
import dev.inmo.micro_utils.repos.cache.cached import dev.inmo.micro_utils.repos.cache.cached
import dev.inmo.micro_utils.repos.exposed.keyvalue.ExposedKeyValueRepo import dev.inmo.micro_utils.repos.exposed.keyvalue.ExposedKeyValueRepo
import dev.inmo.micro_utils.repos.exposed.onetomany.ExposedKeyValuesRepo import dev.inmo.micro_utils.repos.exposed.onetomany.ExposedKeyValuesRepo
import dev.inmo.micro_utils.repos.mappers.withMapper 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.bot.ktor.telegramBot
import dev.inmo.tgbotapi.extensions.api.bot.getMe import dev.inmo.tgbotapi.extensions.api.bot.getMe
import dev.inmo.tgbotapi.extensions.api.bot.setMyCommands 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.ChannelChat
import dev.inmo.tgbotapi.types.chat.PrivateChat import dev.inmo.tgbotapi.types.chat.PrivateChat
import dev.inmo.tgbotapi.types.media.TelegramMediaPhoto import dev.inmo.tgbotapi.types.media.TelegramMediaPhoto
import dev.inmo.tgbotapi.types.message.content.PhotoContent
import java.io.File import java.io.File
import kotlinx.coroutines.* import kotlinx.coroutines.*
import kotlinx.coroutines.sync.Mutex import kotlinx.coroutines.sync.Mutex
@ -97,7 +95,7 @@ suspend fun main(args: Array<String>) {
result.dropLast(toDrop) result.dropLast(toDrop)
}.takeIf { it.isNotEmpty() } ?: return }.takeIf { it.isNotEmpty() } ?: return
runCatchingSafely { runCatchingSafely {
val urls = result.map { it.url.also(::println) } val urls = result.map { it.url }
chatsUrlsSeen.add(chatId, urls) chatsUrlsSeen.add(chatId, urls)
when { when {
urls.isEmpty() -> return@runCatchingSafely urls.isEmpty() -> return@runCatchingSafely
@ -155,13 +153,14 @@ suspend fun main(args: Array<String>) {
} }
onCommand(Regex("(help|start)"), requireOnlyCommandInMessage = true) { 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) { onCommand("enable", requireOnlyCommandInMessage = false) {
val args = it.content.textSources.drop(1).joinToString("") { it.source }.split(" ") val args = it.content.textSources.drop(1).joinToString("") { it.source }.split(" ")
val parser = EnableArgsParser(it.chat.id, repo, this) val parser = EnableArgsParser()
runCatchingSafely { runCatchingSafely {
parser.parse(args) parser.parse(args)
repo.set(it.chat.id, parser.resultSettings ?: return@runCatchingSafely)
}.onFailure { e -> }.onFailure { e ->
e.printStackTrace() e.printStackTrace()
if (it.chat is PrivateChat) { 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) { onCommand("disable", requireOnlyCommandInMessage = true) {
runCatchingSafely { runCatchingSafely {
repo.unset(it.chat.id) repo.unset(it.chat.id)
@ -187,6 +211,7 @@ suspend fun main(args: Array<String>) {
listOf( listOf(
BotCommand("start", "Will return the help for the enable command"), BotCommand("start", "Will return the help for the enable command"),
BotCommand("help", "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("enable", "Will enable images grabbing for current chat or update exists settings"),
BotCommand("disable", "Will disable bot for current chat"), 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 dev.inmo.tgbotapi.types.ChatId
import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.CoroutineScope
class EnableArgsParser( class EnableArgsParser: CliktCommand(name = "enable") {
private val chatId: ChatId,
private val repo: KeyValueRepo<ChatId, ChatSettings>,
private val scope: CoroutineScope
) : 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") { 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 it in 1 .. 10
} }
@ -25,16 +21,16 @@ class EnableArgsParser(
}.required().help("Board type. Possible values: ${ChatSettings.BoardSerializer.types.keys.joinToString { it }}") }.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") 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() { override fun run() {
val chatSettings = ChatSettings( resultSettings = ChatSettings(
query.filterNot { it.isEmpty() }.joinToString(" ").trim(), query.filterNot { it.isEmpty() }.joinToString(" ").trim(),
krontab, krontab,
board, board,
count, count,
gallery gallery
) )
scope.launchSafelyWithoutExceptions {
repo.set(chatId, chatSettings)
}
} }
} }