Skip to content

Commit

Permalink
improve codegen init
Browse files Browse the repository at this point in the history
  • Loading branch information
vendelieu committed Dec 24, 2023
1 parent 5709d3a commit 888ebe7
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 6 deletions.
13 changes: 8 additions & 5 deletions telegram-bot/src/main/kotlin/eu/vendeli/tgbot/TelegramBot.kt
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ import eu.vendeli.tgbot.types.media.File
import eu.vendeli.tgbot.utils.BotConfigurator
import eu.vendeli.tgbot.utils.ManualHandlingBlock
import eu.vendeli.tgbot.utils.RESPONSE_UPDATES_LIST_TYPEREF
import eu.vendeli.tgbot.utils.asClass
import eu.vendeli.tgbot.utils.getActions
import eu.vendeli.tgbot.utils.getConfiguredHttpClient
import eu.vendeli.tgbot.utils.getConfiguredMapper
import eu.vendeli.tgbot.utils.level
Expand All @@ -41,8 +43,10 @@ class TelegramBot(
/**
* Constructor to build through configuration loader.
*/
constructor(configLoader: ConfigLoader = EnvConfigLoaderImpl) :
this(configLoader.token, configLoader.commandsPackage) {
constructor(configLoader: ConfigLoader = EnvConfigLoaderImpl) : this(
configLoader.token,
configLoader.commandsPackage,
) {
config.apply(configLoader.load())
}

Expand Down Expand Up @@ -74,9 +78,8 @@ class TelegramBot(
*/
val update by lazy {
val postFix = commandsPackage?.let { "_$it".replace(".", "_") } ?: ""
val codegenActions = runCatching {
Class.forName("eu.vendeli.tgbot.ActionsData${postFix}Kt")
}.getOrNull()?.let { it.getMethod("get\$ACTIONS$postFix").invoke(null) as? List<*> }
val codegenActions = "eu.vendeli.tgbot.ActionsDataKt".asClass().getActions()
?: "eu.vendeli.tgbot.ActionsData${postFix}Kt".asClass().getActions(postFix)

if (codegenActions != null) CodegenUpdateHandler(
codegenActions,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ class CodegenUpdateHandler(
private val unprocessedHandler = actions[4] as InvocationLambda?

override suspend fun handle(update: Update): Unit = update.processUpdate().run {
logger.debug { "Handling update: $update" }
// check general user limits
if (checkIsLimited(bot.config.rateLimiter.limits, userOrNull?.id))
return@run
Expand All @@ -49,6 +50,8 @@ class CodegenUpdateHandler(
actionId = it.key.pattern
}?.value

logger.debug { "Result of finding action - ${invocation?.second}" }

// if we found any action > check for its limits
if (invocation != null && checkIsLimited(invocation.second.rateLimits, userOrNull?.id, actionId))
return@run
Expand Down Expand Up @@ -82,7 +85,7 @@ class CodegenUpdateHandler(
) { "Method ${second.qualifier} > ${second.function} invocation error at handling update: $pUpdate" }
caughtExceptions.send((it.cause ?: it) to pUpdate.update)
}.onSuccess {
logger.info { "Handled update#${pUpdate.updateId} to method ${second.function}" }
logger.info { "Handled update#${pUpdate.updateId} to method ${second.qualifier + "::" + second.function}" }
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -136,4 +136,11 @@ internal suspend inline fun <T> asyncAction(crossinline block: suspend () -> T):
async { block() }
}

@Suppress("NOTHING_TO_INLINE")
internal inline fun String.asClass(): Class<*>? = kotlin.runCatching { Class.forName(this) }.getOrNull()

@Suppress("NOTHING_TO_INLINE")
internal inline fun Class<*>?.getActions(postFix: String? = null) =
this?.getMethod("get\$ACTIONS".also { if (postFix != null) it + postFix })?.invoke(null) as? List<*>

fun <T : ChainLink> InputListener.setChain(user: User, firstLink: T) = set(user, firstLink::class.qualifiedName!!)

0 comments on commit 888ebe7

Please sign in to comment.