Skip to content

Commit

Permalink
Refactor message handlers to accept player as a parameter
Browse files Browse the repository at this point in the history
  • Loading branch information
alongotv committed Apr 30, 2024
1 parent 2d00a2b commit 2f06a93
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 41 deletions.
Original file line number Diff line number Diff line change
@@ -1,14 +1,18 @@
package com.alongo.discordbot.domain.message_handlers.audio

import com.alongo.discordbot.data.audio.LavaPlayerClient
import com.alongo.discordbot.data.datasource.PlayerStorage
import com.alongo.discordbot.domain.message_handlers.BaseMessageHandler
import dev.kord.core.event.message.MessageCreateEvent
import javax.inject.Inject

class PauseAudioMessageHandler @Inject constructor(private val lavaPlayerClient: LavaPlayerClient) :
BaseMessageHandler() {
class PauseAudioMessageHandler @Inject constructor(
private val lavaPlayerClient: LavaPlayerClient,
private val playerStorage: PlayerStorage,
) : BaseMessageHandler() {
override suspend fun handle(command: String, event: MessageCreateEvent) {
val voiceChannelId = event.member?.getVoiceStateOrNull()?.channelId ?: return
lavaPlayerClient.pauseTrack(voiceChannelId)
val player = playerStorage[voiceChannelId]
lavaPlayerClient.pauseTrack(player)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,18 @@ package com.alongo.discordbot.domain.message_handlers.audio
import com.alongo.discordbot.data.audio.KordAudioConnectionClient
import com.alongo.discordbot.data.audio.LavaPlayerClient
import com.alongo.discordbot.data.audio.LavaPlayerQueryWrapper
import com.alongo.discordbot.domain.exceptions.EndOfTrackQueueException
import com.alongo.discordbot.data.datasource.PlayerStorage
import com.alongo.discordbot.domain.message_handlers.BaseMessageHandler
import com.sedmelluq.discord.lavaplayer.player.AudioLoadResultHandler
import com.sedmelluq.discord.lavaplayer.player.AudioPlayer
import com.sedmelluq.discord.lavaplayer.player.DefaultAudioPlayerManager
import com.sedmelluq.discord.lavaplayer.tools.FriendlyException
import com.sedmelluq.discord.lavaplayer.track.AudioPlaylist
import com.sedmelluq.discord.lavaplayer.track.AudioTrack
import dev.kord.common.entity.Snowflake
import dev.kord.core.behavior.reply
import dev.kord.core.event.message.MessageCreateEvent
import kotlinx.coroutines.CancellationException
import kotlinx.coroutines.launch
import javax.inject.Inject
import kotlin.coroutines.resume
Expand All @@ -22,47 +24,52 @@ import kotlin.coroutines.suspendCoroutine
class PlayAudioMessageHandler @Inject constructor(
private val kordAudioConnectionClient: KordAudioConnectionClient,
private val lavaPlayerClient: LavaPlayerClient,
private val playerStorage: PlayerStorage,
private val lavaPlayerQueryWrapper: LavaPlayerQueryWrapper
) : BaseMessageHandler() {

override suspend fun handle(command: String, event: MessageCreateEvent) {
with(event) {
val voiceChannelId = member?.getVoiceStateOrNull()?.channelId ?: return
val query = lavaPlayerQueryWrapper.wrap(command)
messageHandlerScope.launch {
lavaPlayerClient.errors.collect {
when (it) {
is EndOfTrackQueueException -> {
kordAudioConnectionClient.disconnect(guildId!!)
}
is FriendlyException -> {
message.reply {
content = "There was an error during loading the track."
println(it.message)
}
}
is IllegalArgumentException -> {
message.reply {
content = "Track with provided description not found."
}
}
else -> {

}
messageHandlerScope.launch {
with(event) {
val voiceChannelId = member?.getVoiceStateOrNull()?.channelId ?: return@launch
val query = lavaPlayerQueryWrapper.wrap(command)
val player = playerStorage[voiceChannelId]
try {
lavaPlayerClient.playTrack(player, query) {
// Just disconnect from the voice channel
disconnect(guildId)
}
kordAudioConnectionClient.connect(guildId!!, voiceChannelId, player)
message.reply {
content = "Playing track: ${player.playingTrack?.info?.title}"
}
} catch (e: CancellationException) {
disconnect(guildId)
throw e
} catch (e: FriendlyException) {
message.reply {
content = "There was an error during loading the track."
println(e.message)
}
disconnect(guildId)
} catch (e: IllegalArgumentException) {
message.reply {
content = "Track with provided description not found."
}
disconnect(guildId)
} catch (e: Exception) {
println(e.localizedMessage)
disconnect(guildId)
}
}

val player = lavaPlayerClient.playTrack(voiceChannelId, query)
kordAudioConnectionClient.connect(guildId!!, voiceChannelId, player)
message.reply {
content = "Playing track: ${player.playingTrack.info.title}"
}
}
}

private suspend fun disconnect(guildId: Snowflake?) {
guildId?.let { kordAudioConnectionClient.disconnect(it) }
}
}

// lavaplayer isn't super kotlin-friendly, so we'll make it nicer to work with
suspend fun DefaultAudioPlayerManager.playTrack(query: String, player: AudioPlayer): AudioTrack {
val track = suspendCoroutine<AudioTrack> {
this.loadItem(query, object : AudioLoadResultHandler {
Expand All @@ -79,9 +86,12 @@ suspend fun DefaultAudioPlayerManager.playTrack(query: String, player: AudioPlay
}

override fun loadFailed(exception: FriendlyException?) {
if (exception != null) {
it.resumeWithException(exception)
}
it.resumeWithException(
exception ?: FriendlyException(
"Unknown cause",
FriendlyException.Severity.SUSPICIOUS, null
)
)
}
})
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,19 @@
package com.alongo.discordbot.domain.message_handlers.audio

import com.alongo.discordbot.data.audio.LavaPlayerClient
import com.alongo.discordbot.data.datasource.PlayerStorage
import com.alongo.discordbot.domain.message_handlers.BaseMessageHandler
import dev.kord.core.event.message.MessageCreateEvent
import javax.inject.Inject

class ResumeAudioMessageHandler @Inject constructor(private val lavaPlayerClient: LavaPlayerClient) :
class ResumeAudioMessageHandler @Inject constructor(
private val lavaPlayerClient: LavaPlayerClient,
private val playerStorage: PlayerStorage,
) :
BaseMessageHandler() {
override suspend fun handle(command: String, event: MessageCreateEvent) {
val voiceChannelId = event.member?.getVoiceStateOrNull()?.channelId ?: return
lavaPlayerClient.resumeTrack(voiceChannelId)
val player = playerStorage[voiceChannelId]
lavaPlayerClient.resumeTrack(player)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,20 @@ package com.alongo.discordbot.domain.message_handlers.audio

import com.alongo.discordbot.data.audio.KordAudioConnectionClient
import com.alongo.discordbot.data.audio.LavaPlayerClient
import com.alongo.discordbot.data.datasource.PlayerStorage
import com.alongo.discordbot.domain.message_handlers.BaseMessageHandler
import dev.kord.core.event.message.MessageCreateEvent
import javax.inject.Inject

class StopAudioMessageHandler @Inject constructor(
private val kordAudioConnectionClient: KordAudioConnectionClient,
private val lavaPlayerClient: LavaPlayerClient,
) : BaseMessageHandler() {
private val playerStorage: PlayerStorage,
) : BaseMessageHandler() {
override suspend fun handle(command: String, event: MessageCreateEvent) {
event.guildId?.let { kordAudioConnectionClient.disconnect(it) }
val voiceChannelId = event.member?.getVoiceStateOrNull()?.channelId ?: return
lavaPlayerClient.stopTrack(voiceChannelId)
val player = playerStorage[voiceChannelId]
lavaPlayerClient.stopTrack(player)
}
}

0 comments on commit 2f06a93

Please sign in to comment.