A Kotlin Discord Voice API wrapper kept very, very simple. This is for demonstration purpose only and shouldn't be used in production! I made this library to understand how the Voice API works and get a basic example down.
The library handles the voice sockets as well as the main gateway. Both are really simple and do not feature full support for everything.
The example uses Lavaplayer to get opus encoded audio.
suspend fun main() = coroutineScope {
VoiceApi.apply {
token = "YOUR_BOT_TOKEN"
connectGateway()
}.awaitReady()
val playerManager = DefaultAudioPlayerManager()
playerManager.registerSourceManager(YoutubeAudioSourceManager())
val track = suspendCoroutine<AudioTrack> { c ->
playerManager.loadItem(
"https://www.youtube.com/watch?v=dQw4w9WgXcQ",
FunctionalResultHandler(
{ c.resume(it) },
{ c.resume(it.tracks.first()) },
{},
{}
)
)
}
val server = VoiceStateUpdate(
guildId = "642809436515074053",
channelId = "712567184727212094",
selfMute = false,
selfDeaf = false
)
val connection: VoiceConnection = VoiceApi.connect(server)
connection.openConnection()
val player = playerManager.createPlayer()
player.playTrack(track)
connection.udp?.audioProvider?.provide { player.provide()?.data }
while (true) {
}
}
- Ktor - responsible for websockets
- xsalsa20poly1305 - Voice encryption
- Lavaplayer - Getting Opus audio
- Logback - Logging
- Kotlinx-serialization - Serializing/Deserializing opus packets