now requests do not require any query

This commit is contained in:
InsanusMokrassar 2022-10-07 22:15:06 +06:00
parent 96ec7bbb0c
commit 1eb5f75c78
3 changed files with 52 additions and 25 deletions

View File

@ -155,11 +155,11 @@ suspend fun main(args: Array<String>) {
} }
onCommand(Regex("(help|start)"), requireOnlyCommandInMessage = true) { onCommand(Regex("(help|start)"), requireOnlyCommandInMessage = true) {
reply(it, EnableArgsParser().getFormattedHelp().takeIf { it.isNotBlank() } ?: return@onCommand) reply(it, EnableArgsParser(onlyQueryIsRequired = false).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() val parser = EnableArgsParser(onlyQueryIsRequired = false)
runCatchingSafely { runCatchingSafely {
parser.parse(args) parser.parse(args)
repo.set(it.chat.id, parser.resultSettings ?: return@runCatchingSafely) repo.set(it.chat.id, parser.resultSettings ?: return@runCatchingSafely)
@ -186,7 +186,7 @@ suspend fun main(args: Array<String>) {
return@onCommand return@onCommand
} }
} else { } else {
val parser = EnableArgsParser() val parser = EnableArgsParser(onlyQueryIsRequired = true, repo.get(it.chat.id) ?: ChatSettings.DEFAULT)
runCatchingSafely { runCatchingSafely {
parser.parse(args) parser.parse(args)
parser.resultSettings parser.resultSettings

View File

@ -15,9 +15,9 @@ import net.kodehawa.lib.imageboards.entities.Rating
@Serializable @Serializable
data class ChatSettings( data class ChatSettings(
val query: String, val query: String,
val krontabTemplate: KrontabTemplate?, val krontabTemplate: KrontabTemplate? = null,
@Serializable(BoardSerializer::class) @Serializable(BoardSerializer::class)
private val boardBase: DefaultBoards, private val boardBase: DefaultBoards = DefaultBoards.SAFEBOORU,
val count: Int = 1, val count: Int = 1,
val gallery: Boolean = false, val gallery: Boolean = false,
val rating: Rating? = null, val rating: Rating? = null,
@ -58,4 +58,8 @@ data class ChatSettings(
encoder.encodeString(value.name.lowercase()) encoder.encodeString(value.name.lowercase())
} }
} }
companion object {
val DEFAULT = ChatSettings("", null, DefaultBoards.SAFEBOORU)
}
} }

View File

@ -3,27 +3,50 @@ import com.github.ajalt.clikt.parameters.arguments.*
import com.github.ajalt.clikt.parameters.options.* import com.github.ajalt.clikt.parameters.options.*
import com.github.ajalt.clikt.parameters.types.enum import com.github.ajalt.clikt.parameters.types.enum
import com.github.ajalt.clikt.parameters.types.int import com.github.ajalt.clikt.parameters.types.int
import dev.inmo.micro_utils.coroutines.launchSafelyWithoutExceptions import net.kodehawa.lib.imageboards.boards.DefaultBoards
import dev.inmo.micro_utils.repos.KeyValueRepo
import dev.inmo.micro_utils.repos.set
import dev.inmo.tgbotapi.types.ChatId
import kotlinx.coroutines.CoroutineScope
import net.kodehawa.lib.imageboards.entities.Rating import net.kodehawa.lib.imageboards.entities.Rating
class EnableArgsParser: CliktCommand(name = "enable") { class EnableArgsParser(
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") { onlyQueryIsRequired: Boolean,
it in 1 .. 10 private val base: ChatSettings = ChatSettings("", null, DefaultBoards.SAFEBOORU)
) : CliktCommand(name = "enable") {
private fun <EachT : Any, ValueT> NullableOption<EachT, ValueT>.default(
value: EachT?,
defaultForHelp: String = value.toString(),
): NullableOption<EachT, ValueT> {
return if (value != null) {
transformAll(defaultForHelp) { it.lastOrNull() ?: value }
} else {
this
} }
val query by argument().multiple(required = true).help("Your query to booru. Use syntax \"-- -sometag\" to add excluding of some tag in query") }
val krontab by option("-k", "--krontab").transformValues(5) { val count by option("-n")
it.joinToString(" ") .int()
}.help("Krontab in format * * * * *. See https://bookstack.inmo.dev/books/krontab/page/string-format") .help("Amount of pictures to grab each trigger time")
val board by option("-b", "--board").convert { .check("Count should be in range 1-10") { it in 1 .. 10 }
ChatSettings.BoardSerializer.types.getValue(it) val query by argument()
}.required().help("Board type. Possible values: ${ChatSettings.BoardSerializer.types.keys.joinToString { it }}") .multiple(required = true)
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") .help("Your query to booru. Use syntax \"-- -sometag\" to add excluding of some tag in query")
val rating by option("-r", "--rating").enum<Rating> { it.name.lowercase() } val krontab by option("-k", "--krontab")
val attachUrls by option("-a", "--attach_urls").flag(default = false) .transformValues(5) { it.joinToString(" ") }
.help("Krontab in format * * * * *. See https://bookstack.inmo.dev/books/krontab/page/string-format")
val board by option("-b", "--board")
.convert { ChatSettings.BoardSerializer.types.getValue(it) }
.run {
if (onlyQueryIsRequired) {
this
} else {
required()
}
}
.help("Board type. Possible values: ${ChatSettings.BoardSerializer.types.keys.joinToString { it }}")
val gallery by option("-g", "--gallery")
.flag(default = base.gallery)
.help("Effective only when count passed > 1. Will send chosen images as gallery instead of separated images")
val rating by option("-r", "--rating")
.enum<Rating> { it.name.lowercase() }
val attachUrls by option("-a", "--attach_urls")
.flag(default = base.attachUrls)
var resultSettings: ChatSettings? = null var resultSettings: ChatSettings? = null
private set private set
@ -32,8 +55,8 @@ class EnableArgsParser: CliktCommand(name = "enable") {
resultSettings = ChatSettings( resultSettings = ChatSettings(
query.filterNot { it.isEmpty() }.joinToString(" ").trim(), query.filterNot { it.isEmpty() }.joinToString(" ").trim(),
krontab, krontab,
board, board ?: base.board.boardType as DefaultBoards,
count, count ?: base.count,
gallery, gallery,
rating, rating,
attachUrls attachUrls