Skip to content

Kotlin extensions

In addition to the BotCommands-jda-ktx module offers, Kotlin users have access to top-level functions and extensions in various categories:

Resolvers

enumResolver — Creates a parameter resolver which transforms arguments into an enum entry, compatible with most handlers
enum class MyEnum {
    FIRST,
    SECOND,
    THIRD
}

@BConfiguration
class MyEnumResolverProvider {
    // Creates an enum resolver for all values
    // you can also customize what values can be used, per-guild,
    // and also change how they are displayed
    @Resolver
    fun myEnumResolver() = enumResolver<MyEnum>()
}
resolverFactory — Creates a factory for parameter resolvers, useful to avoid the boilerplate of using TypedParameterResolverFactory

See example on the docs

I/O

readResource — Gets an InputStream of a resource from the classpath
readResource("/file.txt").use { contentStream ->
    // ...
}
readResourceAsString — Gets a resource from the classpath as a String
val content = readResourceAsString("/file.txt")
withResource — Uses an InputStream of a resource from the classpath
withResource("/file.txt") { contentStream ->
    // ...
}

Coroutines

namedDefaultScope — Creates a CoroutineScope with a thread name and a fixed thread pool
// 1 thread named "[feature] timeout"
// You can also configure other CoroutineScope characteristics
private val timeoutScope = namedDefaultScope("[feature] timeout", corePoolSize = 1)

// ...

timeoutScope.launch {
    // Async task
}

Logging

KotlinLogging.loggerOf — Creates a logger targeting the specified class
private val logger = KotlinLogging.loggerOf<MyService>()

@BService
class MyServiceImpl : MyService {
    // ...
}

Collections

Emojis