From 7f4a182f39d4b940b6811b59a1a873b48c60262c Mon Sep 17 00:00:00 2001 From: Michael Rittmeister Date: Tue, 26 Dec 2023 17:36:15 +0100 Subject: [PATCH] Fix issues with play request timing out --- .../schlaubi/tonbrett/bot/core/SoundPlayer.kt | 2 +- .../tonbrett/bot/server/SoundsRoute.kt | 20 ++++--------------- build.gradle.kts | 2 +- 3 files changed, 6 insertions(+), 18 deletions(-) diff --git a/bot/src/main/kotlin/dev/schlaubi/tonbrett/bot/core/SoundPlayer.kt b/bot/src/main/kotlin/dev/schlaubi/tonbrett/bot/core/SoundPlayer.kt index 35ca3ee..d29e3c0 100644 --- a/bot/src/main/kotlin/dev/schlaubi/tonbrett/bot/core/SoundPlayer.kt +++ b/bot/src/main/kotlin/dev/schlaubi/tonbrett/bot/core/SoundPlayer.kt @@ -63,7 +63,7 @@ class SoundPlayer(guild: GuildBehavior) : CoroutineScope { } @Suppress("INVISIBLE_MEMBER", "SuspendFunctionOnCoroutineScope") - suspend fun playSound(sound: Sound, user: Snowflake, channelId: Snowflake?) { + suspend fun playSound(sound: Sound, user: Snowflake) { val alreadyLocked = locked locked = true updateAvailability(false, sound, user) diff --git a/bot/src/main/kotlin/dev/schlaubi/tonbrett/bot/server/SoundsRoute.kt b/bot/src/main/kotlin/dev/schlaubi/tonbrett/bot/server/SoundsRoute.kt index f4d7707..6a39c42 100644 --- a/bot/src/main/kotlin/dev/schlaubi/tonbrett/bot/server/SoundsRoute.kt +++ b/bot/src/main/kotlin/dev/schlaubi/tonbrett/bot/server/SoundsRoute.kt @@ -4,7 +4,6 @@ import com.kotlindiscord.kord.extensions.koin.KordExContext import dev.kord.common.annotation.KordExperimental import dev.kord.common.annotation.KordUnsafe import dev.kord.core.Kord -import dev.kord.core.event.guild.VoiceServerUpdateEvent import dev.schlaubi.lavakord.kord.connectAudio import dev.schlaubi.tonbrett.bot.core.soundPlayer import dev.schlaubi.tonbrett.bot.core.voiceState @@ -22,12 +21,9 @@ import io.ktor.server.application.* import io.ktor.server.resources.* import io.ktor.server.response.* import io.ktor.server.routing.Route -import kotlinx.coroutines.flow.filter -import kotlinx.coroutines.flow.filterIsInstance -import kotlinx.coroutines.flow.first +import kotlinx.coroutines.delay import kotlinx.coroutines.flow.toList -import kotlinx.coroutines.withTimeout -import kotlin.time.Duration.Companion.seconds +import kotlin.time.Duration.Companion.milliseconds @OptIn(KordUnsafe::class, KordExperimental::class) fun Route.sounds() { @@ -64,19 +60,11 @@ fun Route.sounds() { @Suppress("INVISIBLE_MEMBER", "EQUALITY_NOT_APPLICABLE") if (player.channelId == null) { player.player.link.connectAudio(voiceState.channelId) - withTimeout(5.seconds) { - kord.events - .filterIsInstance() - .filter { - it.guildId == player.player.guildId - } - // wait for "Connect event" - .first() - } + delay(500.milliseconds) // let's wait a bit before playing the sound to avoid issues with cutting off audio } else if (player.channelId != null && player.channelId != voiceState.channelId) { badRequest(call.translate("rest.errors.vc_mismatch")) } - player.playSound(sound, user, voiceState.channelId?.takeIf { player.channelId == null }) + player.playSound(sound, user) call.respond(HttpStatusCode.Accepted) } } diff --git a/build.gradle.kts b/build.gradle.kts index 16da445..df5ea3a 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -7,7 +7,7 @@ plugins { allprojects { group = "dev.schlaubi.tonbrett" - version = "1.18.4" + version = "1.18.5" repositories { mavenCentral()