Skip to content

Commit 9bacf56

Browse files
committed
Merge and slightly improve impl from freya
1 parent 5884947 commit 9bacf56

File tree

3 files changed

+18
-6
lines changed

3 files changed

+18
-6
lines changed

settings.gradle.kts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ fun VersionCatalogBuilder.versionRefs() {
2020
version("dokka", "2.1.0")
2121
version("grgit", "5.2.0")
2222
version("maven-publish", "0.32.0")
23-
// version("lavalink", "4.0.3")
23+
// TODO: Update with actual version number when it arrives
2424
version("lavalink", "74a765017f39f01dd01d3eb9bc976bcb08ffef92-SNAPSHOT")
2525

2626
version("logger", "2.0.7")

src/main/kotlin/dev/arbjerg/lavalink/libraries/discord4j/D4JVoiceUpdateHandler.kt

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ import discord4j.core.event.domain.VoiceStateUpdateEvent
1212
import reactor.core.Disposable
1313
import reactor.core.Disposables
1414
import reactor.core.publisher.Mono
15+
import java.util.concurrent.ConcurrentHashMap
16+
import java.util.concurrent.atomic.AtomicReference
1517
import kotlin.jvm.optionals.getOrNull
1618

1719
/**
@@ -24,12 +26,21 @@ import kotlin.jvm.optionals.getOrNull
2426
*/
2527
@JvmName("install")
2628
fun GatewayDiscordClient.installVoiceHandler(lavalink: LavalinkClient): Disposable.Composite {
29+
// guild_id -> channel_id
30+
val channelIdCache = ConcurrentHashMap<Long, String>()
31+
2732
val voiceStateUpdate = on(VoiceStateUpdateEvent::class.java) { event ->
2833
val update = event.current
2934
if (update.userId != update.client.selfId) return@on Mono.empty()
3035
val channel = update.channelId.getOrNull()
31-
val link = lavalink.getLinkIfCached(update.guildId.asLong()) ?: return@on Mono.empty()
32-
val player = link.node.playerCache[update.guildId.asLong()] ?: return@on Mono.empty()
36+
val guildId = update.guildId.asLong()
37+
38+
channel?.let { channelSnowflake ->
39+
channelIdCache[guildId] = channelSnowflake.asString()
40+
}
41+
42+
val link = lavalink.getLinkIfCached(guildId) ?: return@on Mono.empty()
43+
val player = link.node.playerCache[guildId] ?: return@on Mono.empty()
3344
val playerState = player.state
3445

3546
if (channel == null && playerState.connected) {
@@ -42,15 +53,16 @@ fun GatewayDiscordClient.installVoiceHandler(lavalink: LavalinkClient): Disposab
4253
}.subscribe()
4354

4455
val voiceServerUpdate = on(VoiceServerUpdateEvent::class.java) { update ->
56+
val guildId = update.guildId.asLong()
4557
val state = VoiceState(
4658
update.token,
4759
update.endpoint!!,
4860
getGatewayClient(update.shardInfo.index).get().sessionId,
49-
null // TODO: find where to get the channel id from
61+
channelIdCache.remove(guildId) // TODO: Test if this works, is the order of events correct?
5062
)
5163

5264
val region = VoiceRegion.fromEndpoint(update.endpoint!!)
53-
val link = lavalink.getOrCreateLink(update.guildId.asLong(), region)
65+
val link = lavalink.getOrCreateLink(guildId, region)
5466

5567
link.onVoiceServerUpdate(state)
5668
Mono.empty<Unit>()

src/main/kotlin/dev/arbjerg/lavalink/libraries/jda/JDAVoiceUpdateListener.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ class JDAVoiceUpdateListener(private val lavalink: LavalinkClient) : VoiceDispat
1212
update.token,
1313
update.endpoint,
1414
update.sessionId,
15-
null // TODO: find where to get the channel id from
15+
update.guild.selfMember.voiceState?.channel?.id // TODO: Test if this works, is the order of events correct?
1616
)
1717
val region = VoiceRegion.fromEndpoint(update.endpoint)
1818
val link = lavalink.getOrCreateLink(update.guildIdLong, region)

0 commit comments

Comments
 (0)