/kord

Idiomatic Kotlin Wrapper for The Discord API

Primary LanguageKotlinMIT LicenseMIT

Kord

Discord Download JitPack Gitlab pipeline status (branch)

Kord is still in an experimental stage, as such we can't guarantee API stability between releases. While we'd love for you to try out our library, we don't recommend you use this in production just yet.

If you have any feedback, we'd love to hear it, hit us up on discord or write up an issue if you have any suggestions!

What is Kord

Kord is a coroutine-based, modularized implementation of the Discord API, written 100% in Kotlin.

Why use Kord

Kord was created as an answer to the frustrations of writing Discord bots with other JVM libraries, which either use thread-blocking code or verbose and scope restrictive reactive systems. We believe an API written from the ground up in Kotlin with coroutines can give you the best of both worlds: The conciseness of imperative code with the concurrency of reactive code.

Aside from coroutines, we also wanted to give the user full access to lower level APIs. Sometimes you have to do some unconventional things, and we want to allow you to do those in a safe and supported way.

Status of Kord

Right now Kord should provide a full mapping of the non-voice API. We're currently working on a testing library for easy bot testing against a semi mocked client as well as our own command system to facilitate more complex bot development.

Installation

Replace {version} with the latest version number on bintray. Download

Gradle (groovy)

repositories {
    jcenter()
    maven {
        url = "https://dl.bintray.com/kordlib/Kord"
    }
}
dependencies {
   implementation("com.gitlab.kordlib.kord:kord-core:{version}")
}

Gradle (kotlin)

repositories {
   jcenter()
   maven(url = "https://dl.bintray.com/kordlib/Kord")
}
dependencies {
   implementation("com.gitlab.kordlib.kord:kord-core:{version}")
}

Maven

<repository>
    <id>jcenter</id>
    <url>https://jcenter.bintray.com/</url>
</repository>
<repository>
    <id>bintray-kord</id>
    <url>https://dl.bintray.com/kordlib/Kord</url>
</repository>
<dependency>
    <groupId>com.gitlab.kordlib.kord</groupId>
    <artifactId>kord-core</artifactId>
    <version>{version}</version>
</dependency>

Modules

Core

A higher level API, combining rest and gateway, with additional (optional) caching. Unless you're writing your own abstractions, we'd recommend using this.

suspend fun main() {
    val client = Kord("your bot token")
    val pingPong = ReactionEmoji.Unicode("\uD83C\uDFD3")

    client.on<MessageCreateEvent> {
        if(message.content != "!ping") return@on

        val response = message.channel.createMessage("Pong!")
        response.addReaction(pingPong)

        delay(5000)
        message.delete()
        response.delete()
    }

    client.login()
}

Rest

A low level mapping of Discord's REST API. Requests follow Discord's rate limits.

suspend fun main() {
    val rest = RestClient("your bot token")

    rest.channel.createMessage("605212557522763787") {
        content = "Hello Kord!"

        embed {
            color = Color.BLUE
            description = "Hello embed!"
        }
    }

}

Gateway

A low level mapping of Discord's Gateway, which maintains the connection and rate limits commands.

suspend fun main() {
    val gateway = DefaultGateway()

    gateway.events.filterIsInstance<MessageCreate>()
            .flowOn(Dispatchers.IO)
            .onEach {
                when (it.message.content) {
                    "!close" -> gateway.stop()
                    "!restart" -> gateway.restart()
                    "!detach" -> gateway.detach()
                }
            }
            .launchIn(GlobalScope)

    gateway.start("your bot token")
}

FAQ

Will you support kotlin multi-platform

We originally intended to. We exclusively depend on multi-platform libraries and try to minimize JVM exclusive code. However, we've found support for multi-platform and non-JVM platforms to be lacking (too experimental). We'll revisit multi-platform support once IR (intermediate representation) has been implemented, or our library is feature complete, whichever comes first.

Will you publish your kotlin docs online

The only real documentation engine for kotlin is Dokka. It's currently not in a happy place, but we've been told it'll get a rather extensive release sooner than later, so we're holding off until that happens.