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¶
enumSetOf— Creates an enum set of the provided typeenumSetOfAll— Creates an enum set of the provided type, with all the entries in itenumMapOf— Creates a map with an enum keyList<T>.toImmutableList— Creates an immutable copy of the listSet<T>.toImmutableSet— Creates an immutable copy of the setMap<K, V>.toImmutableMap— Creates an immutable copy of the mapIterable<T>.containsAny— Checks if the collection contains any of the provided elements
Emojis¶
Emoji.asUnicodeEmoji— Converts a JEmoji'sEmojiinto a JDAUnicodeEmojilazyUnicodeEmoji— Lazily fetches the Unicode from the given shortcode