Skip to content

Commit

Permalink
Update to ktor 2.0.0
Browse files Browse the repository at this point in the history
  • Loading branch information
DRSchlaubi committed Apr 17, 2022
1 parent 52590b3 commit 68151da
Show file tree
Hide file tree
Showing 19 changed files with 105 additions and 75 deletions.
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -131,4 +131,6 @@ gradle-app.setting

## Docs are only supposed to be there on gh-pages branch or on CI runners
docs/
!docs/index.html
!docs/index.html

.idea/artifacts
5 changes: 5 additions & 0 deletions .idea/compiler.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ plugins {
}

group = "dev.schlaubi.lavakord"
version = "3.5.1"
version = "3.6.0"

allprojects {
repositories {
Expand Down
8 changes: 4 additions & 4 deletions buildSrc/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@ repositories {
}

dependencies {
implementation(kotlin("gradle-plugin", version = "1.6.10"))
implementation(kotlin("serialization", version = "1.6.10"))
implementation("org.jetbrains.dokka", "dokka-gradle-plugin", "1.6.10")
implementation(kotlin("gradle-plugin-api", version = "1.6.10"))
implementation(kotlin("gradle-plugin", version = "1.6.20"))
implementation(kotlin("serialization", version = "1.6.20"))
implementation("org.jetbrains.dokka", "dokka-gradle-plugin", "1.6.20")
implementation(kotlin("gradle-plugin-api", version = "1.6.20"))
implementation(gradleApi())
implementation(localGroovy())
}
3 changes: 2 additions & 1 deletion core/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ kotlin {
implementation(libs.ktor.utils)
implementation(libs.ktor.client.websockets)
implementation(libs.ktor.client.core)
implementation(libs.ktor.client.serialization)
implementation(libs.ktor.serialization.kotlinx.json)
implementation(libs.ktor.client.content.negotiation)
implementation(libs.ktor.client.logging)

implementation(libs.kotlinlogging)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@ import dev.schlaubi.lavakord.internal.RestNodeImpl
import dev.schlaubi.lavakord.rest.RoutePlannerModule
import io.ktor.client.*
import io.ktor.client.engine.*
import io.ktor.client.features.json.*
import io.ktor.client.features.json.serializer.*
import io.ktor.client.features.logging.*
import io.ktor.client.features.websocket.*
import io.ktor.client.plugins.contentnegotiation.*
import io.ktor.client.plugins.logging.*
import io.ktor.client.plugins.websocket.*
import io.ktor.http.*
import io.ktor.serialization.kotlinx.json.*
import kotlinx.atomicfu.atomic
import kotlinx.coroutines.launch
import kotlinx.serialization.modules.plus
Expand Down Expand Up @@ -57,8 +57,8 @@ public abstract class AbstractLavakord internal constructor(


internal val restClient = HttpClient(httpClientEngine) {
install(JsonFeature) {
serializer = KotlinxSerializer(json)
install(ContentNegotiation) {
json(json)
}

install(Logging) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,23 +1,25 @@
package dev.schlaubi.lavakord.audio.internal

import dev.schlaubi.lavakord.audio.*
import io.ktor.client.features.*
import io.ktor.client.features.websocket.*
import io.ktor.client.network.sockets.*
import io.ktor.client.plugins.*
import io.ktor.client.plugins.websocket.*
import io.ktor.client.request.*
import io.ktor.http.*
import io.ktor.http.cio.websocket.*
import io.ktor.network.sockets.*
import kotlinx.coroutines.*
import io.ktor.websocket.*
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.channels.Channel
import kotlinx.coroutines.flow.*
import kotlinx.coroutines.flow.MutableSharedFlow
import kotlinx.coroutines.flow.SharedFlow
import kotlinx.coroutines.flow.asSharedFlow
import kotlinx.coroutines.launch
import kotlinx.serialization.SerializationException
import kotlinx.serialization.decodeFromString
import kotlinx.serialization.encodeToString
import mu.KotlinLogging

internal val LOG = KotlinLogging.logger { }

@OptIn(ExperimentalCoroutinesApi::class)
internal class NodeImpl(
override val host: Url,
override val name: String,
Expand All @@ -37,11 +39,9 @@ internal class NodeImpl(
override val coroutineScope: CoroutineScope
get() = lavakord

@OptIn(FlowPreview::class)
override val events: SharedFlow<TrackEvent>
get() = eventPublisher.asSharedFlow()

@OptIn(InternalCoroutinesApi::class)
internal suspend fun connect(resume: Boolean = false) {
session = try {
lavakord.gatewayClient.webSocketSession {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ internal class WebsocketPlayer(internal val node: NodeImpl, internal val guildId
node.send(GatewayPayload.SeekCommand(guildId.toString(), position))
}

@Deprecated("Please use the new filters system to specify volume")
override suspend fun setVolume(volume: Int) {
require(volume >= 0) { "Volume can't be negative" }
require(volume <= 500) { "Volume can't be greater than 500" } // Volume <= 5.0
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@ public data class Track constructor(
*
* @see Track.track
*/
@OptIn(InternalAPI::class)
@Suppress("ReplaceNotNullAssertionWithElvisReturn")
public suspend fun fromLavalink(encoded: String): Track {
val bytes = encoded.decodeBase64Bytes()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,23 @@ package dev.schlaubi.lavakord.rest
import dev.schlaubi.lavakord.audio.RestNode
import dev.schlaubi.lavakord.audio.internal.AbstractLavakord
import io.ktor.client.*
import io.ktor.client.call.*
import io.ktor.client.request.*
import io.ktor.http.*
import kotlinx.serialization.json.Json

internal suspend inline fun <reified T> RestNode.get(noinline urlBuilder: URLBuilder.() -> Unit): T =
restClient.get(buildUrl(urlBuilder).build()) { addHeader(this@get); accept(ContentType.Application.JavaScript) }
restClient.get(buildUrl(urlBuilder).build()) {
addHeader(this@get); accept(ContentType.Application.JavaScript)
}.body()

internal suspend inline fun <reified T> RestNode.post(
urlBuilder: URLBuilder,
block: HttpRequestBuilder.() -> Unit
) =
restClient.get<T>(urlBuilder.build()) { addHeader(this@post); accept(ContentType.Application.JavaScript); block(this) }
restClient.get(urlBuilder.build()) {
addHeader(this@post); accept(ContentType.Application.JavaScript); block(this)
}.body<T>()

private fun HttpRequestBuilder.addHeader(socket: RestNode) {
header(HttpHeaders.Authorization, socket.authenticationHeader)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import dev.schlaubi.lavakord.NoRoutePlannerException
import dev.schlaubi.lavakord.audio.Link
import dev.schlaubi.lavakord.audio.Node
import dev.schlaubi.lavakord.audio.RestNode
import io.ktor.client.request.*
import io.ktor.http.*
import kotlinx.serialization.Serializable
import kotlinx.serialization.SerializationException
Expand Down Expand Up @@ -89,7 +90,7 @@ public suspend fun RestNode.unmarkAddress(address: String) {

return post(url) {
contentType(ContentType.Application.Json)
body = UnmarkAddressBody(address)
setBody(UnmarkAddressBody(address))
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import dev.schlaubi.lavakord.audio.Link
import dev.schlaubi.lavakord.audio.Node
import dev.schlaubi.lavakord.audio.RestNode
import dev.schlaubi.lavakord.audio.player.Track
import io.ktor.util.*
import io.ktor.http.*

/**
* Maps a [List] of [TrackResponse.PartialTrack]s to a List of [Track]s.
Expand All @@ -30,7 +30,6 @@ public suspend fun Link.loadItem(query: String): TrackResponse = node.loadItem(q
*
* @see TrackResponse
*/
@OptIn(InternalAPI::class) // There is no other way for parameters
public suspend fun RestNode.loadItem(query: String): TrackResponse = get {
path("loadtracks")
parameters.append("identifier", query)
Expand Down
2 changes: 1 addition & 1 deletion core/src/commonTest/kotlin/rest/RoutePlannerTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ class RoutePlannerTest {
private val engine = RestHttpEngine {
addHandler { request ->
checkAuth(request) {
when (request.url.fullPath.substringAfter('/').substringBefore('?')) {
when (request.url.fullPath) {
"/routeplanner/free/all" -> respond("", HttpStatusCode.NoContent)
"/routeplanner/free/address" -> {
val body = request.body.toByteArray().decodeToString()
Expand Down
17 changes: 9 additions & 8 deletions example/src/main/java/Javakord.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
import dev.schlaubi.lavakord.interop.TrackUtil;
import dev.schlaubi.lavakord.interop.jda.LavakordJDABuilder;
import net.dv8tion.jda.api.JDABuilder;
import net.dv8tion.jda.api.events.interaction.SlashCommandEvent;
import net.dv8tion.jda.api.events.interaction.command.SlashCommandInteractionEvent;
import net.dv8tion.jda.api.hooks.ListenerAdapter;
import net.dv8tion.jda.api.interactions.commands.OptionType;
import net.dv8tion.jda.api.interactions.commands.build.CommandData;
import net.dv8tion.jda.api.interactions.commands.build.Commands;
import org.jetbrains.annotations.NotNull;

public class Javakord extends ListenerAdapter {
Expand All @@ -27,17 +27,17 @@ public Javakord() {
var jda = container.getJda();
jda.updateCommands()
.addCommands(
new CommandData("connect", "Joins the current channel"),
new CommandData("play", "Plays a new song")
Commands.slash("connect", "Joins the current channel"),
Commands.slash("play", "Plays a new song")
.addOption(OptionType.STRING, "query", "The query you want to play"),
new CommandData("destroy", "Let the bot leave the channel"),
new CommandData("pause", "Pauses or unpauses playpack")
Commands.slash("destroy", "Let the bot leave the channel"),
Commands.slash("pause", "Pauses or unpauses playpack")
)
.queue();
}

@Override
public void onSlashCommand(@NotNull SlashCommandEvent event) {
public void onSlashCommandInteraction(@NotNull SlashCommandInteractionEvent event) {
var invoke = event.getName();
var link = lavakord.getLink(event.getGuild().getIdLong());
var player = link.getPlayer();
Expand All @@ -61,7 +61,8 @@ public void onSlashCommand(@NotNull SlashCommandEvent event) {
case TRACK_LOADED -> player.playTrack(track.getTrack());
case PLAYLIST_LOADED, SEARCH_RESULT -> player.playTrack(track.getTracks().get(0));
case NO_MATCHES -> event.getChannel().sendMessage("No tracks found!").queue();
case LOAD_FAILED -> event.getChannel().sendMessage("Load failed: %s".formatted(track.getException().getMessage())).queue();
case LOAD_FAILED ->
event.getChannel().sendMessage("Load failed: %s".formatted(track.getException().getMessage())).queue();
}
});
}
Expand Down
2 changes: 1 addition & 1 deletion java/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ kotlin {
dependencies {
api(projects.core)
api(projects.jda)
api("net.dv8tion:JDA:5.0.0-alpha.4") {
api("net.dv8tion:JDA:5.0.0-alpha.5") {
exclude(module = "opus-java")
}
implementation(libs.kotlinx.coroutines.jdk8)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ public class JavaPlayer(internal val suspendingPlayer: Player) : CoroutineScope
/**
* Seeks to a specific [position] in the current playback.
*/
public suspend fun seekTo(position: Duration): CompletableFuture<Void> = seekTo(position.toMillis())
public fun seekTo(position: Duration): CompletableFuture<Void> = seekTo(position.toMillis())

/**
* Seeks to a specific [position] in the current playback.
Expand All @@ -83,6 +83,8 @@ public class JavaPlayer(internal val suspendingPlayer: Player) : CoroutineScope
/**
* Changes the volume of the current player.
*/
@Suppress("DeprecatedCallableAddReplaceWith", "DEPRECATION")
@Deprecated("Please use the new filters system to specify volume")
public fun setVolume(volume: Int): CompletableFuture<Void> = run { suspendingPlayer.setVolume(volume) }

/**
Expand Down
2 changes: 1 addition & 1 deletion jda/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ kotlin {
api(projects.core)
api(libs.kotlinlogging)
api(libs.kotlinx.coroutines.jdk8)
implementation("net.dv8tion:JDA:5.0.0-alpha.4") {
implementation("net.dv8tion:JDA:5.0.0-alpha.5") {
exclude(module = "opus-java")
}
}
Expand Down
43 changes: 28 additions & 15 deletions kotlin-js-store/yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +49,6 @@ argparse@^2.0.1:
resolved "https://registry.yarnpkg.com/argparse/-/argparse-2.0.1.tgz#246f50f3ca78a3240f6c997e8a9bd1eac49e4b38"
integrity sha512-8+9WqebbFzpX9OR+Wa6O29asIogeRMzcGtAINdpMHHyAg10f05aSFVBbcEqGf/PXw1EjAZ+q2/bEBg3DvurK3Q==

async-limiter@~1.0.0:
version "1.0.1"
resolved "https://registry.yarnpkg.com/async-limiter/-/async-limiter-1.0.1.tgz#dd379e94f0db8310b08291f9d64c3209766617fd"
integrity sha512-csOlWGAcRFJaI6m+F2WKdnMKr4HhdhFVBk0H/QbJFMCr+uO2kwohwXQPxw/9OCxp05r5ghVBFSyioixx3gfkNQ==

balanced-match@^1.0.0:
version "1.0.2"
resolved "https://registry.yarnpkg.com/balanced-match/-/balanced-match-1.0.2.tgz#e83e3a7e3f300b34cb9d87f615fa0cbf357690ee"
Expand Down Expand Up @@ -398,10 +393,12 @@ [email protected]:
resolved "https://registry.yarnpkg.com/nanoid/-/nanoid-3.1.25.tgz#09ca32747c0e543f0e1814b7d3793477f9c8e152"
integrity sha512-rdwtIXaXCLFAQbnfqDRnI6jaRHp9fTcYBjtFKE8eezcZ7LuLjhUaQGNeMXf1HmRoCH32CLz6XwX0TtxEOS/A3Q==

[email protected]:
version "2.6.0"
resolved "https://registry.yarnpkg.com/node-fetch/-/node-fetch-2.6.0.tgz#e633456386d4aa55863f676a7ab0daa8fdecb0fd"
integrity sha512-8dG4H5ujfvFiqDmVu9fQ5bOHUC15JMjMY/Zumv26oOvvVJjM67KF8koCWIabKQ1GJIa9r2mMZscBq/TbdOcmNA==
[email protected]:
version "2.6.7"
resolved "https://registry.yarnpkg.com/node-fetch/-/node-fetch-2.6.7.tgz#24de9fba827e3b4ae44dc8b20256a379160052ad"
integrity sha512-ZjMPFEfVx5j+y2yF35Kzx5sF7kDzxuDj6ziH4FFbOp87zKDZNx8yExJIb05OGF4Nlt9IHFIMBkRl41VdvcNdbQ==
dependencies:
whatwg-url "^5.0.0"

normalize-path@^3.0.0, normalize-path@~3.0.0:
version "3.0.0"
Expand Down Expand Up @@ -530,11 +527,29 @@ to-regex-range@^5.0.1:
dependencies:
is-number "^7.0.0"

tr46@~0.0.3:
version "0.0.3"
resolved "https://registry.yarnpkg.com/tr46/-/tr46-0.0.3.tgz#8184fd347dac9cdc185992f3a6622e14b9d9ab6a"
integrity sha1-gYT9NH2snNwYWZLzpmIuFLnZq2o=

[email protected]:
version "3.9.5"
resolved "https://registry.yarnpkg.com/typescript/-/typescript-3.9.5.tgz#586f0dba300cde8be52dd1ac4f7e1009c1b13f36"
integrity sha512-hSAifV3k+i6lEoCJ2k6R2Z/rp/H3+8sdmcn5NrS3/3kE7+RyZXm9aqvxWqjEXHAd8b0pShatpcdMTvEdvAJltQ==

webidl-conversions@^3.0.0:
version "3.0.1"
resolved "https://registry.yarnpkg.com/webidl-conversions/-/webidl-conversions-3.0.1.tgz#24534275e2a7bc6be7bc86611cc16ae0a5654871"
integrity sha1-JFNCdeKnvGvnvIZhHMFq4KVlSHE=

whatwg-url@^5.0.0:
version "5.0.0"
resolved "https://registry.yarnpkg.com/whatwg-url/-/whatwg-url-5.0.0.tgz#966454e8765462e37644d3626f6742ce8b70965d"
integrity sha1-lmRU6HZUYuN2RNNib2dCzotwll0=
dependencies:
tr46 "~0.0.3"
webidl-conversions "^3.0.0"

[email protected]:
version "2.0.2"
resolved "https://registry.yarnpkg.com/which/-/which-2.0.2.tgz#7c6a8dd0a636a0327e10b59c9286eee93f3f51b1"
Expand All @@ -561,12 +576,10 @@ wrappy@1:
resolved "https://registry.yarnpkg.com/wrappy/-/wrappy-1.0.2.tgz#b5243d8f3ec1aa35f1364605bc0d1036e30ab69f"
integrity sha1-tSQ9jz7BqjXxNkYFvA0QNuMKtp8=

[email protected]:
version "6.2.1"
resolved "https://registry.yarnpkg.com/ws/-/ws-6.2.1.tgz#442fdf0a47ed64f59b6a5d8ff130f4748ed524fb"
integrity sha512-GIyAXC2cB7LjvpgMt9EKS2ldqr0MTrORaleiOno6TweZ6r3TKtoFQWay/2PceJ3RuBasOHzXNn5Lrw1X0bEjqA==
dependencies:
async-limiter "~1.0.0"
[email protected]:
version "8.5.0"
resolved "https://registry.yarnpkg.com/ws/-/ws-8.5.0.tgz#bfb4be96600757fe5382de12c670dab984a1ed4f"
integrity sha512-BWX0SWVgLPzYwF8lTzEy1egjhS4S4OEAHfsO8o65WOVsrnSRGaSiUaa9e0ggGlkMTtBlmOpEXiie9RUcBO86qg==

y18n@^5.0.5:
version "5.0.8"
Expand Down
Loading

0 comments on commit 68151da

Please sign in to comment.