Skip to content

Commit fa38f5b

Browse files
committed
Add enableResuming() and disableResuming() methods
1 parent e98de0e commit fa38f5b

File tree

2 files changed

+24
-0
lines changed

2 files changed

+24
-0
lines changed

src/main/kotlin/dev/arbjerg/lavalink/client/LavalinkNode.kt

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import reactor.core.publisher.Sinks.Many
2525
import reactor.kotlin.core.publisher.toMono
2626
import java.io.Closeable
2727
import java.io.IOException
28+
import java.time.Duration
2829
import java.util.concurrent.ConcurrentHashMap
2930
import java.util.concurrent.TimeUnit
3031
import java.util.function.Consumer
@@ -234,6 +235,22 @@ class LavalinkNode(
234235
return rest.getNodeInfo()
235236
}
236237

238+
/**
239+
* Enables resuming. This causes Lavalink to continue playing for a certain duration of time, during which
240+
* we can reconnect without losing our session data. */
241+
fun enableResuming(timeout: Duration): Mono<Session> {
242+
return rest.patchSession(Session(resuming = true, timeout.seconds))
243+
}
244+
245+
/**
246+
* Disables resuming, causing Lavalink to immediately drop all players upon this client disconnecting from it.
247+
*
248+
* This is the default behavior, reversing calls to [enableResuming].
249+
*/
250+
fun disableResuming(): Mono<Session> {
251+
return rest.patchSession(Session(resuming = false, timeoutSeconds = 0))
252+
}
253+
237254
/**
238255
* Send a custom request to the lavalink node. Any host and port you set will be replaced with the node host automatically.
239256
* The scheme must match your node's scheme, however.

src/main/kotlin/dev/arbjerg/lavalink/internal/LavalinkRestClient.kt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,13 @@ class LavalinkRestClient(val node: LavalinkNode) {
6969
}.toMono()
7070
}
7171

72+
fun patchSession(session: Session): Mono<Session> {
73+
return newRequest {
74+
path("/v4/sessions/${node.sessionId}")
75+
patch(json.encodeToString(session).toRequestBody("application/json".toMediaType()))
76+
}.toMono()
77+
}
78+
7279
/**
7380
* Make a request to the lavalink node. This is internal to keep it looking nice in kotlin. Java compatibility is in the node class.
7481
*/

0 commit comments

Comments
 (0)