Skip to content

Commit

Permalink
Cache an Arguments object per-command for autocomplete purposes
Browse files Browse the repository at this point in the history
  • Loading branch information
gdude2002 committed Jan 8, 2025
1 parent 8de96ab commit c3d4baa
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,9 @@ public abstract class SlashCommand<C : SlashCommandContext<*, A, M>, A : Argumen
public open val parentCommand: SlashCommand<*, *, *>? = null,
public open val parentGroup: SlashGroup? = null,
) : ApplicationCommand<ChatInputCommandInteractionCreateEvent>(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 {}

Expand Down Expand Up @@ -174,6 +177,8 @@ public abstract class SlashCommand<C : SlashCommandContext<*, A, M>, A : Argumen
"instead."
)
}

cachedArguments = arguments?.invoke()
}

/** Call this to supply a command [body], to be called when the command is executed. **/
Expand Down

0 comments on commit c3d4baa

Please sign in to comment.