Skip to content

Commit

Permalink
A bunch of playback and queuing fixes and improvements
Browse files Browse the repository at this point in the history
- Use less distorting volume implementation
- Improve player state saving for /re-deploy
- Reimplement queuing system improving handling of shuffle mode, causing /queue to display the correct shuffle order and adding new tracks at the top of the queue
- Displaying the user who queued the track in queue displaying things
  • Loading branch information
DRSchlaubi committed Mar 14, 2024
1 parent 926095d commit 8635792
Show file tree
Hide file tree
Showing 16 changed files with 312 additions and 234 deletions.
2 changes: 1 addition & 1 deletion music/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
subprojects {
version = "3.5.10-SNAPSHOT"
version = "3.5.11-SNAPSHOT"
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ suspend fun MusicModule.clearCommand() = ephemeralControlSlashCommand {
description = "commands.clear.description"

action {
musicPlayer.clearQueue()
musicPlayer.queue.clear()
musicPlayer.updateMusicChannelMessage()
respond {
content = translate("commands.clear.cleared")
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ private object ReEstablishVoiceConnection : RecoveryStep {
musicPlayer.stop()
delay(200.milliseconds)
musicPlayer.link.connectAudio(channelId)
state.applyToPlayer(musicPlayer)
musicPlayer.applyState(state)
}
}

Expand All @@ -157,10 +157,7 @@ private object SwitchVoiceServers : RecoveryStep {
val channel = musicPlayer.getChannel()!!
val currentRegion = channel.rtcRegion

val availableRegions = (
safeGuild.regions
.map { it.id }.toList() - (currentRegion ?: "")
)
val availableRegions = safeGuild.regions.map { it.id }.toList() - (currentRegion ?: "")
val fallbackRegion = availableRegions.random()

channel.edit {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,8 @@ suspend fun MusicModule.moveCommand() {
val fromRaw = safeArguments.from()
val fromValue = if (fromRaw == -1) musicPlayer.queuedTracks.size else fromRaw.coerceAtLeast(1)

val moved = musicPlayer.moveQueuedEntry(fromValue - 1, toValue - 1, swap)
val moved = musicPlayer.queue.moveQueuedEntry(fromValue - 1, toValue - 1, swap)
musicPlayer.updateMusicChannelMessage()

if (moved != null) {
respond {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import com.kotlindiscord.kord.extensions.extensions.ephemeralSlashCommand
import dev.schlaubi.mikbot.plugin.api.util.forList
import dev.schlaubi.mikmusic.checks.anyMusicPlaying
import dev.schlaubi.mikmusic.core.MusicModule
import dev.schlaubi.mikmusic.player.QueuedTrack
import dev.schlaubi.mikmusic.player.addAutoPlaySongs
import dev.schlaubi.mikmusic.util.format

Expand All @@ -17,7 +18,7 @@ suspend fun MusicModule.queueCommand() = ephemeralSlashCommand {

action {
if (musicPlayer.queuedTracks.isEmpty()) {
val track = player.playingTrack
val track = musicPlayer.playingTrack
if (track != null) {
respond {
content = translate("commands.queue.now_playing", arrayOf(track.format()))
Expand All @@ -31,10 +32,10 @@ suspend fun MusicModule.queueCommand() = ephemeralSlashCommand {
}

editingPaginator {
forList(user, musicPlayer.queuedTracks, { (track) -> track.format() }, { current, total ->
forList(user, musicPlayer.queuedTracks, QueuedTrack::format, { current, total ->
translate("music.queue.info.title", arrayOf(current.toString(), total.toString()))
}) {
val playingTrack = player.playingTrack
val playingTrack = musicPlayer.playingTrack
if (playingTrack != null) {
field {
name = translate("music.queue.now_playing")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ suspend fun MusicModule.removeCommand() = ephemeralControlSlashCommand {
) =
action {
val removed = remove()
musicPlayer.updateMusicChannelMessage()
if (removed > 0) {
respond {
content = translate("commands.remove.removed", arrayOf(removed))
Expand All @@ -56,7 +57,8 @@ suspend fun MusicModule.removeCommand() = ephemeralControlSlashCommand {
description = "commands.remove.song.description"

action {
val track = musicPlayer.removeQueueEntry(arguments.position - 1)
val track = musicPlayer.queue.removeQueueEntry(arguments.position - 1)
musicPlayer.updateMusicChannelMessage()
if (track != null) {
respond {
content = translate("commands.remove.song.removed", arrayOf(track.info.title))
Expand All @@ -79,15 +81,15 @@ suspend fun MusicModule.removeCommand() = ephemeralControlSlashCommand {
}
val range = arguments.from..arguments.to

musicPlayer.removeQueueEntries(range)
musicPlayer.queue.removeQueueEntries(range)
}
}

ephemeralSubCommand {
name = "doubles"
description = "commands.remove.doubles.description"

doRemove { musicPlayer.removeDoubles() }
doRemove { musicPlayer.queue.removeDoubles() }
}

ephemeralSubCommand {
Expand All @@ -102,7 +104,7 @@ suspend fun MusicModule.removeCommand() = ephemeralControlSlashCommand {
.map { it.userId }
.toList()

musicPlayer.removeFromUser { it !in users }
musicPlayer.queue.removeFromUser { it !in users }
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ class VolumeArguments : Arguments() {
val volume by optionalInt {
name = "volume"
description = "commands.volume.arguments.volume.description"
maxValue = 100
maxValue = 1000
minValue = 0
}
}
Expand All @@ -20,11 +20,7 @@ suspend fun MusicModule.volumeCommand() = ephemeralControlSlashCommand(::VolumeA
action {
val volume = arguments.volume
if (volume != null) {
val filterVolume = volume.toFloat() / 100
musicPlayer.applyFilters {
this.volume = filterVolume
}

musicPlayer.changeVolume(volume)
respond { content = translate("commands.volume.set", arrayOf(volume.toString())) }
} else {
respond { content = translate("commands.volume.current", arrayOf(player.volume.toString())) }
Expand Down
11 changes: 11 additions & 0 deletions music/player/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ plugins {
alias(libs.plugins.kotlinx.serialization)
com.google.devtools.ksp
dev.schlaubi.mikbot.`gradle-plugin`
`jvm-test-suite`
}

group = "dev.schlaubi.mikbot"
Expand All @@ -32,6 +33,8 @@ dependencies {
api(projects.clients.imageColorClientKord)

implementation(libs.ktor.client.logging)

testImplementation(kotlin("test-junit5"))
}

tasks {
Expand All @@ -42,6 +45,14 @@ tasks {
}
}

testing {
suites {
named<JvmTestSuite>("test") {
useJUnitJupiter()
}
}
}

mikbotPlugin {
pluginId = "music-player"
description = "Plugin providing full music functionality for the bot"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ class MusicModule(context: PluginContext) : MikBotModule(context) {
val player = getMusicPlayer(guild)
it.schedulerOptions.applyToPlayer(player)
player.connectAudio(channelId)
it.applyToPlayer(player)
player.applyState(it)
}
}
playerStates.drop()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ suspend fun updateMessage(
title = translate("music.music_channel.queue")
description = musicPlayer.queuedTracks.take(5).mapIndexed { index, track -> track to index }
.joinToString("\n") { (track, index) ->
(index + 1).toString() + ". " + track.track.format()
(index + 1).toString() + ". " + track.format()
}.ifBlank { translate("music.music_channel.queue.empty") }

musicPlayer.addAutoPlaySongs(::translate)
Expand Down
Loading

0 comments on commit 8635792

Please sign in to comment.