resolverFactory

inline fun <T : ParameterResolver<T, R>, R : Any> resolverFactory(priority: Int = 0, crossinline producer: (request: ResolverRequest) -> T): ParameterResolverFactory<T>(source)

Creates a parameter resolver factory from the provided resolver producer.

The producer is called for each function parameter with the exact R type.

This should be returned in a service factory, using @ResolverFactory.

Example using a custom localization service:

@BConfiguration
object MyCustomLocalizationResolverProvider {
// The parameter resolver, which will be created once per parameter
class MyCustomLocalizationResolver(
private val localizationService: LocalizationService,
private val guildSettingsService: GuildSettingsService,
private val bundleName: String,
private val prefix: String?
) : ClassParameterResolver<MyCustomLocalizationResolver, MyCustomLocalization>(MyCustomLocalization::class),
ICustomResolver<MyCustomLocalizationResolver, MyCustomLocalization> {

// Called when a command is used
override suspend fun resolveSuspend(executable: Executable, event: Event): MyCustomLocalization {
return if (event is Interaction) {
val guild = event.guild
?: throw IllegalStateException("Cannot get localization outside of guilds")
// The root localization file not existing isn't an issue on production
val localization = localizationService.getInstance(bundleName, guildSettingsService.getGuildLocale(guild.idLong))
?: throw IllegalArgumentException("No root bundle exists for '$bundleName'")

// Return resolved object
MyCustomLocalization(localization, prefix)
} else {
throw UnsupportedOperationException("Unsupported event: ${event.javaClass.simpleNestedName}")
}
}
}

// Service factory returning a resolver factory
// The returned factory is used on each command/handler parameter of type "MyCustomLocalization",
// which is the same type as what MyCustomLocalizationResolver returns
@ResolverFactory
fun myCustomLocalizationResolverProvider(localizationService: LocalizationService, guildSettingsService: GuildSettingsService) = resolverFactory { parameter ->
// Find @LocalizationBundle on the parameter
val bundle = parameter.parameter.findAnnotation<LocalizationBundle>()
?: throw IllegalArgumentException("Parameter ${parameter.parameter} must be annotated with LocalizationBundle")

// Return our resolver for that parameter
MyCustomLocalizationResolver(localizationService, guildSettingsService, bundle.value, bundle.prefix.nullIfBlank())
}
}

Parameters

priority

Priority of this resolver factory, see ParameterResolverFactory.priority

producer

Function providing a resolver for the provided function parameter

T

Type of the produced parameter resolver

R

Type of the object returned by the resolver

See also