enumResolver

inline fun <E : Enum<E>> enumResolver(vararg values: E = enumValues(), noinline nameFunction: (e: E) -> String = { it.toHumanName() }, block: EnumResolverBuilder<E>.() -> Unit = {}): ClassParameterResolver<*, E>(source)

Creates an enum resolver for slash commands, as well as component data and timeout data.

Text command support

To add support for text command options, you have to use EnumResolverBuilder.withTextSupport in the configuration block.

Using choices

You have to enable SlashOption.usePredefinedChoices for the choices to appear on your slash command option.

Registration

The created resolver needs to be registered as a service factory, with @Resolver, for example:

@BConfiguration
object EnumResolvers {
// Resolver for DAYS/HOURS/MINUTES, where the displayed name is given by 'Resolvers.Enum#toHumanName'
@Resolver
fun timeUnitResolver() = enumResolver<TimeUnit>(TimeUnit.DAYS, TimeUnit.HOURS, TimeUnit.MINUTES)

...other resolvers...
}

Localization

The choices are localized automatically by using the bundles defined by BApplicationConfigBuilder.addLocalizations, using a path similar to my.command.path.options.my_option.choices.choice_name.name, as required by LocalizationFunction.

The choice name is produced by the name function, and is then lowercase with spaces modified to underscore by LocalizationFunction.

For example, using the default name function:

  1. MY_ENUM_VALUE (Raw enum name)

  2. My enum value (Choice name displayed on Discord)

  3. my_enum_value (Choice name in your localization file)

Parameters

E

The enum type

nameFunction

Retrieves a human friendly name for the enum value, defaults to toHumanName

See also


inline fun <E : Enum<E>> enumResolver(guildValuesSupplier: EnumValuesSupplier<E>, noinline nameFunction: (e: E) -> String = { it.toHumanName() }, block: EnumResolverBuilder<E>.() -> Unit = {}): ClassParameterResolver<*, E>(source)

Creates an enum resolver for slash commands, as well as component data and timeout data.

Text command support

To add support for text command options, you have to use EnumResolverBuilder.withTextSupport in the configuration block.

Using choices

You have to enable SlashOption.usePredefinedChoices for the choices to appear on your slash command option.

Registration

The created resolver needs to be registered as a service factory, with @Resolver, for example:

@BConfiguration
object EnumResolvers {
// Resolver for DAYS/HOURS/MINUTES (and SECONDS in the test guild), where the displayed name is given by 'Resolvers.Enum#toHumanName'
@Resolver
fun timeUnitResolver() = enumResolver<TimeUnit> { guild ->
if (guild.isTestGuild()) {
enumSetOf(TimeUnit.DAYS, TimeUnit.HOURS, TimeUnit.MINUTES, TimeUnit.SECONDS)
} else {
enumSetOf(TimeUnit.DAYS, TimeUnit.HOURS, TimeUnit.MINUTES)
}
}

...other resolvers...
}

Localization

The choices are localized automatically by using the bundles defined by BApplicationConfigBuilder.addLocalizations, using a path similar to my.command.path.options.my_option.choices.choice_name.name, as required by LocalizationFunction.

The choice name is produced by the name function, and is then lowercase with spaces modified to underscore by LocalizationFunction.

For example, using the default name function:

  1. MY_ENUM_VALUE (Raw enum name)

  2. My enum value (Choice name displayed on Discord)

  3. my_enum_value (Choice name in your localization file)

Parameters

E

The enum type

guildValuesSupplier

Retrieves the values used for slash command choices, for each Guild

nameFunction

Retrieves a human friendly name for the enum value, defaults to toHumanName

See also