Skip to content

Commit

Permalink
VC join inactivity 90-second timer
Browse files Browse the repository at this point in the history
  • Loading branch information
ileukocyte committed Aug 31, 2022
1 parent d10bb3c commit 7a23940
Showing 1 changed file with 38 additions and 0 deletions.
38 changes: 38 additions & 0 deletions src/main/kotlin/io/ileukocyte/hibernum/handlers/EventHandler.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
package io.ileukocyte.hibernum.handlers

import com.sedmelluq.discord.lavaplayer.player.AudioPlayer
import com.sedmelluq.discord.lavaplayer.player.event.AudioEventAdapter
import com.sedmelluq.discord.lavaplayer.track.AudioTrack

import io.ileukocyte.hibernum.audio.*
import io.ileukocyte.hibernum.commands.`fun`.AkinatorCommand
import io.ileukocyte.hibernum.commands.`fun`.ChomskyCommand
Expand All @@ -12,6 +16,7 @@ import io.ileukocyte.hibernum.utils.kill
import java.util.concurrent.TimeUnit

import kotlin.time.Duration.Companion.minutes
import kotlin.time.Duration.Companion.seconds

import kotlinx.coroutines.*
import kotlinx.coroutines.selects.select
Expand Down Expand Up @@ -58,6 +63,39 @@ object EventHandler : ListenerAdapter() {
override fun onUserContextInteraction(event: UserContextInteractionEvent) =
CommandHandler(event)

override fun onGuildVoiceJoin(event: GuildVoiceJoinEvent) {
if (event.guild.selfMember.idLong == event.member.idLong) {
val player = event.guild.audioPlayer ?: return

if (player.player.playingTrack === null) {
CoroutineScope(MusicContext).launch {
val deferred = CompletableDeferred<Unit>()

val listener = object : AudioEventAdapter() {
override fun onTrackStart(player: AudioPlayer, track: AudioTrack) {
player.removeListener(this)

deferred.complete(Unit)
}
}

player.player.addListener(listener)

try {
withTimeout((90).seconds) { deferred.await() }
} catch (_: TimeoutCancellationException) {
player.player.removeListener(listener)
player.stop()

if (event.member.voiceState?.inAudioChannel() == true) {
event.guild.audioManager.closeAudioConnection()
}
}
}
}
}
}

@OptIn(ExperimentalCoroutinesApi::class)
override fun onGuildVoiceLeave(event: GuildVoiceLeaveEvent) {
if (event.member != event.guild.selfMember) {
Expand Down

0 comments on commit 7a23940

Please sign in to comment.