Kord

repository·main·Indexed 21 days ago

https://github.com/kordlib/kord

A coroutine-based, modularized implementation of the Discord API written in Kotlin. Kord provides a high-level, non-blocking, and event-driven representation of Discord entities across several modules, including kord-core for abstractions and caching, kord-rest for the REST API, kord-gateway for gateway implementation, and kord-voice for Discord Voice API (JVM only).

Tokens
4.8K
Snippets
15
Records
18
Agent score
72%

What's inside Kord

  1. Overview of Kord modules

    main

    Kord is a modularized, coroutine-based implementation of the Discord API. It provides a full mapping of the non-voice API on Kotlin/JVM and Kotlin/JS, and an experimental mapping of the Voice API on Kotlin/JVM.

    Available modules:

    • kord-common: Common logic.
    • kord-rest: Discord REST API implementation.
    • kord-gateway: Discord Gateway implementation.
    • kord-core: High-level abstractions and caching.
    • kord-voice: Discord Voice API (JVM only).
    • kord-core-voice: Core abstractions for Voice.
  2. Overview of the core-voice module

    main
    The core-voice module provides extensions to the core module specifically designed for use with kord-voice. It acts as a bridge to enable voice-related capabilities within the Kord ecosystem. For detailed information on how to implement and use voice features, you should refer to the primary voice module documentation.
  3. How Kord Voice works

    main
    Kord Voice is an implementation of Discord's Voice Connection. It acts as a middleman between Discord and your audio source. To function, it requires an audio source capable of outputting 20ms of Opus-encoded 48k stereo audio data. While the library is agnostic to the audio source, it is commonly used with libraries like LavaPlayer to handle audio retrieval and decoding.
  4. Quickstart with Kord Core

    main

    Kord core provides a high-level, non-blocking, coroutine-focused, and event-driven representation of Discord entities. You can interact with Discord events using two primary styles:

    1. Flow style: Use Kotlin Coroutines Flow to filter and transform events. This is useful for complex reactive pipelines.
    2. Simplified style: Use the kord.on<T> extension function for a more direct, imperative event handling approach.

    When logging in, you must configure your intents via the kord.login block. Note that certain intents, like Intent.MessageContent, require the @OptIn(PrivilegedIntent::class) annotation.

    suspend fun main() {
        val kord = Kord("your bot token")
    
        // Flow style
        kord.events
            .filterIsInstance<MessageCreateEvent>()
            .map { it.message }
            .filter { it.author?.isBot == false }
            .filter { it.content == "!ping" }
            .onEach { it.channel.createMessage("pong") }
            .launchIn(kord)
    
        // Simplified style
        kord.on<MessageCreateEvent> {
            if (message.author?.isBot == true) return@on
            if (message.content == "!ping") message.channel.createMessage("pong")
        }
    
        kord.login {
            @OptIn(PrivilegedIntent::class)
            intents += Intent.MessageContent
        }
    }
  5. Play audio in a voice channel using Kord Voice

    main

    To play audio, you must connect to a voice channel using the connect method on a VoiceChannel. Inside the VoiceConnectionBuilder block, you must provide an audioProvider that returns AudioFrame objects. Each AudioFrame should contain the Opus-encoded data you wish to send to Discord.

    voiceChannel.connect {
        // audio provider wants a AudioFrame each time its called by Kord
        audioProvider { 
            AudioFrame.fromData(
                /* an opus audio frame, you can use something like lavaplayer to do this for you*/
            )
        }
    }
  6. Install Kord gateway via Maven

    main

    Add the following dependency to your pom.xml file. Replace {version} with the desired version of the library.

    <dependency>
        <groupId>dev.kord</groupId>
        <artifactId>kord-gateway-jvm</artifactId>
        <version>{version}</version>
    </dependency>
  7. Install kord-rest via Maven

    main

    Add the following dependency to your pom.xml file. Replace {version} with the desired version of the library.

    <dependency>
        <groupId>dev.kord</groupId>
        <artifactId>kord-rest-jvm</artifactId>
        <version>{version}</version>
    </dependency>
  8. Install Kord

    main

    Kord can be installed using Gradle (Kotlin or Groovy) or Maven. Replace {version} with the latest version from Maven Central. For snapshots, use {branch}-SNAPSHOT (e.g., main-SNAPSHOT).

    Note: For Gradle versions older than 5.3 or when using Maven, you must append -jvm to the artifact ID (e.g., kord-core-jvm).

    ### Gradle (Kotlin)
    ```kotlin
    repositories {
        mavenCentral()
        maven("https://snapshots.kord.dev")
    }
    
    dependencies {
        implementation("dev.kord:kord-core:{version}")
    }

    Gradle (Groovy)

    repositories {
        mavenCentral()
        maven {
            url "https://snapshots.kord.dev/"
        }
    }
    
    dependencies {
        implementation("dev.kord:kord-core:{version}")
    }

    Maven

    <repository>
        <id>snapshots-repo</id>
        <url>https://snapshots.kord.dev</url>
        <releases>
            <enabled>false</enabled>
        </releases>
        <snapshots>
            <enabled>true</enabled>
        </snapshots>
    </repository>
    
    <dependency>
        <groupId>dev.kord</groupId>
        <artifactId>kord-core-jvm</artifactId>
        <version>{version}</version>
    </dependency>
  9. Receive audio from Discord

    main

    Kord supports receiving audio from Discord (note: this is not officially documented by Discord and stability is not guaranteed). To enable receiving, set receiveVoice = true within the VoiceConnectionBuilder. Once enabled, you can access the incoming audio via the streams property on the VoiceConnection, which exposes an incomingAudioFrames flow.

    val connection = voiceChannel.connect {
        receiveVoice = true
    }
    
    // Access the incoming audio frames via the streams property
    processIncomingAudio(connection.streams.incomingAudioFrames)