From c3d4baaa70887faebc6f6c8dd7c8e4fa7f882ae0 Mon Sep 17 00:00:00 2001 From: Gareth Coles Date: Wed, 8 Jan 2025 18:39:58 +0000 Subject: [PATCH] Cache an Arguments object per-command for autocomplete purposes --- .../application/DefaultApplicationCommandRegistry.kt | 3 ++- .../application/StorageAwareApplicationCommandRegistry.kt | 2 +- .../kordex/core/commands/application/slash/SlashCommand.kt | 5 +++++ 3 files changed, 8 insertions(+), 2 deletions(-) diff --git a/kord-extensions/src/main/kotlin/dev/kordex/core/commands/application/DefaultApplicationCommandRegistry.kt b/kord-extensions/src/main/kotlin/dev/kordex/core/commands/application/DefaultApplicationCommandRegistry.kt index 82e830a613..6aaa8d449d 100644 --- a/kord-extensions/src/main/kotlin/dev/kordex/core/commands/application/DefaultApplicationCommandRegistry.kt +++ b/kord-extensions/src/main/kotlin/dev/kordex/core/commands/application/DefaultApplicationCommandRegistry.kt @@ -383,7 +383,8 @@ public open class DefaultApplicationCommandRegistry : ApplicationCommandRegistry option ?: return logger.trace { "Autocomplete event for command $command doesn't have a focused option." } - val arguments = command.arguments!!() + val arguments = command.cachedArguments + ?: return logger.trace { "Command $command doesn't have a cached arguments object for some reason." } val arg = arguments.args.firstOrNull { it.getDefaultTranslatedDisplayName() == option.first diff --git a/kord-extensions/src/main/kotlin/dev/kordex/core/commands/application/StorageAwareApplicationCommandRegistry.kt b/kord-extensions/src/main/kotlin/dev/kordex/core/commands/application/StorageAwareApplicationCommandRegistry.kt index 4e27b98feb..d45467d883 100644 --- a/kord-extensions/src/main/kotlin/dev/kordex/core/commands/application/StorageAwareApplicationCommandRegistry.kt +++ b/kord-extensions/src/main/kotlin/dev/kordex/core/commands/application/StorageAwareApplicationCommandRegistry.kt @@ -117,7 +117,7 @@ public open class StorageAwareApplicationCommandRegistry( option ?: return logger.trace { "Autocomplete event for command $command doesn't have a focused option." } - val arguments = command.arguments!!() + val arguments = command.cachedArguments!! val arg = arguments.args.firstOrNull { it.getDefaultTranslatedDisplayName() == option.first diff --git a/kord-extensions/src/main/kotlin/dev/kordex/core/commands/application/slash/SlashCommand.kt b/kord-extensions/src/main/kotlin/dev/kordex/core/commands/application/slash/SlashCommand.kt index 1dfdbdab7e..cd36657cfc 100644 --- a/kord-extensions/src/main/kotlin/dev/kordex/core/commands/application/slash/SlashCommand.kt +++ b/kord-extensions/src/main/kotlin/dev/kordex/core/commands/application/slash/SlashCommand.kt @@ -52,6 +52,9 @@ public abstract class SlashCommand, A : Argumen public open val parentCommand: SlashCommand<*, *, *>? = null, public open val parentGroup: SlashGroup? = null, ) : ApplicationCommand(extension) { + /** @suppress Cached arguments object only used by the autocomplete event handler. **/ + public var cachedArguments: A? = null + /** @suppress This is only meant for use by code that extends the command system. **/ public val kxLogger: KLogger = KotlinLogging.logger {} @@ -174,6 +177,8 @@ public abstract class SlashCommand, A : Argumen "instead." ) } + + cachedArguments = arguments?.invoke() } /** Call this to supply a command [body], to be called when the command is executed. **/