Compare commits

...

2 Commits

Author SHA1 Message Date
InsanusMokrassar ea801e6e0d improvements 2022-12-11 12:17:15 +06:00
InsanusMokrassar 2b7a21342b update dependencies 2022-12-11 12:02:36 +06:00
4 changed files with 37 additions and 19 deletions

View File

@ -1,12 +1,12 @@
[versions]
kotlin = "1.7.10"
tgbotapi = "3.2.1"
microutils = "0.12.11"
kotlin = "1.7.22"
tgbotapi = "4.2.1"
microutils = "0.16.1"
imageboard = "2.5.2"
krontab = "0.8.0"
kslog = "0.5.1"
exposed = "0.39.2"
krontab = "0.8.4"
kslog = "0.5.4"
exposed = "0.41.1"
psql = "42.5.0"
clikt = "3.5.0"

View File

@ -1,5 +1,5 @@
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-7.5.1-bin.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-7.6-bin.zip
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists

View File

@ -21,6 +21,7 @@ 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.utils.code
import java.io.File
import kotlinx.coroutines.*
import kotlinx.coroutines.sync.Mutex
@ -28,7 +29,9 @@ import kotlinx.coroutines.sync.withLock
import kotlinx.serialization.json.Json
import models.Config
import net.kodehawa.lib.imageboards.ImageBoard
import net.kodehawa.lib.imageboards.boards.DefaultBoards
import net.kodehawa.lib.imageboards.entities.BoardImage
import kotlin.reflect.full.memberProperties
/**
* This method by default expects one argument in [args] field: telegram bot configuration
@ -162,7 +165,7 @@ suspend fun main(args: Array<String>) {
val parser = EnableArgsParser(onlyQueryIsRequired = false)
runCatchingSafely {
parser.parse(args)
repo.set(it.chat.id, parser.resultSettings ?: return@runCatchingSafely)
repo.set(ChatId(it.chat.id.chatId), parser.resultSettings ?: return@runCatchingSafely)
}.onFailure { e ->
e.printStackTrace()
if (it.chat is PrivateChat) {
@ -179,14 +182,14 @@ suspend fun main(args: Array<String>) {
val args = it.content.textSources.drop(1).joinToString("") { it.source }.trim().takeIf { it.isNotBlank() } ?.split(" ")
val chatSettings = if (args.isNullOrEmpty()) {
repo.get(it.chat.id) ?: run {
repo.get(ChatId(it.chat.id.chatId)) ?: run {
if (it.chat is PrivateChat) {
reply(it, "Unable to find default config")
}
return@onCommand
}
} else {
val parser = EnableArgsParser(onlyQueryIsRequired = true, repo.get(it.chat.id) ?: ChatSettings.DEFAULT)
val parser = EnableArgsParser(onlyQueryIsRequired = true, repo.get(ChatId(it.chat.id.chatId)) ?: ChatSettings.DEFAULT)
runCatchingSafely {
parser.parse(args)
parser.resultSettings
@ -198,16 +201,36 @@ suspend fun main(args: Array<String>) {
}.getOrNull()
}
triggerSendForChat(it.chat.id, chatSettings ?: return@onCommand)
triggerSendForChat(ChatId(it.chat.id.chatId), chatSettings ?: return@onCommand)
}
onCommand("disable", requireOnlyCommandInMessage = true) {
runCatchingSafely {
repo.unset(it.chat.id)
repo.unset(ChatId(it.chat.id.chatId))
}
runCatchingSafely {
delete(it)
}
}
onCommand("take_settings", requireOnlyCommandInMessage = true) {
val settings = runCatchingSafely {
repo.get(ChatId(it.chat.id.chatId))
}.getOrNull()
runCatchingSafely {
if (settings == null) {
reply(it, "You didn't enable requesting")
} else {
reply(it, ) {
+"Query: " + code(settings.query) + "\n"
+"Krontab: " + code(settings.krontabTemplate ?: "unset") + "\n"
+"Board: " + code(DefaultBoards.values().first { it == settings.board.boardType }.name) + "\n"
+"Count: " + code(settings.count.toString()) + "\n"
+"Gallery: " + code(settings.gallery.toString()) + "\n"
+"Rating: " + code(settings.rating ?.name ?: "unset") + "\n"
+"Attach urls: " + code(settings.attachUrls.toString())
}
}
}
}
setMyCommands(
listOf(
@ -216,6 +239,7 @@ suspend fun main(args: Array<String>) {
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"),
BotCommand("take_settings", "Take your current settings"),
)
)

View File

@ -31,14 +31,8 @@ class EnableArgsParser(
.transformValues(5) { it.joinToString(" ") }
.help("Krontab in format * * * * *. See https://bookstack.inmo.dev/books/krontab/page/string-format")
val board by option("-b", "--board")
.default("safebooru")
.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)