Skip to content

Commit

Permalink
Add the ability to specify playOptions for injected tracks
Browse files Browse the repository at this point in the history
  • Loading branch information
DRSchlaubi committed Aug 29, 2023
1 parent 2c0cd9d commit 0943fc6
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 16 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.1.7-SNAPSHOT"
version = "3.1.8-SNAPSHOT"
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,7 @@ package dev.schlaubi.mikmusic.player
import com.kotlindiscord.kord.extensions.i18n.TranslationsProvider
import com.kotlindiscord.kord.extensions.koin.KordExKoinComponent
import dev.arbjerg.lavalink.protocol.v4.Message
import dev.arbjerg.lavalink.protocol.v4.PlayerUpdate
import dev.arbjerg.lavalink.protocol.v4.Track
import dev.arbjerg.lavalink.protocol.v4.toOmissible
import dev.kord.common.entity.Snowflake
import dev.kord.core.behavior.GuildBehavior
import dev.kord.core.entity.channel.VoiceChannel
Expand All @@ -15,13 +13,13 @@ import dev.schlaubi.lavakord.audio.TrackEndEvent
import dev.schlaubi.lavakord.audio.TrackStartEvent
import dev.schlaubi.lavakord.audio.on
import dev.schlaubi.lavakord.audio.player.Filters
import dev.schlaubi.lavakord.audio.player.PlayOptions
import dev.schlaubi.lavakord.audio.player.applyFilters
import dev.schlaubi.lavakord.plugins.sponsorblock.model.Category
import dev.schlaubi.lavakord.plugins.sponsorblock.model.ChapterStartedEvent
import dev.schlaubi.lavakord.plugins.sponsorblock.model.ChaptersLoadedEvent
import dev.schlaubi.lavakord.plugins.sponsorblock.rest.disableSponsorblock
import dev.schlaubi.lavakord.plugins.sponsorblock.rest.putSponsorblockCategories
import dev.schlaubi.lavakord.rest.updatePlayer
import dev.schlaubi.mikmusic.core.settings.MusicSettingsDatabase
import dev.schlaubi.mikmusic.musicchannel.updateMessage
import kotlinx.coroutines.Job
Expand All @@ -38,9 +36,8 @@ import kotlin.time.Duration.Companion.seconds
import kotlin.time.DurationUnit
import kotlin.time.toDuration

private data class SavedTrack(val track: QueuedTrack, val position: Duration)
private data class SavedTrack(val track: QueuedTrack, val position: Duration, val filters: Filters)

@OptIn(UnsafeRestApi::class)
class MusicPlayer(val link: Link, private val guild: GuildBehavior) :
Link by link, KordExKoinComponent {

Expand All @@ -56,7 +53,6 @@ class MusicPlayer(val link: Link, private val guild: GuildBehavior) :
private set
private val translationsProvider: TranslationsProvider by inject()

private var chapterUpdater: Job? = null
private var leaveTimeout: Job? = null
internal var autoPlay: AutoPlayContext? = null
private var savedTrack: SavedTrack? = null
Expand Down Expand Up @@ -206,21 +202,16 @@ class MusicPlayer(val link: Link, private val guild: GuildBehavior) :
updateMusicChannelMessage()
}

suspend fun injectTrack(identifier: String, noReplace: Boolean = false) = lock.withLock<Unit> {
suspend fun injectTrack(identifier: String, noReplace: Boolean = false, playOptionsBuilder: PlayOptions.() -> Unit) = lock.withLock<Unit> {
dontQueue = true
val playOptions = PlayOptions().apply(playOptionsBuilder)
val currentTrack = playingTrack
if (currentTrack != null && !noReplace) {
val currentPosition = player.positionDuration

savedTrack = SavedTrack(currentTrack, currentPosition)
savedTrack = SavedTrack(currentTrack, currentPosition, player.filters)
}
link.node.updatePlayer(
guildId,
noReplace = false,
PlayerUpdate(
identifier = identifier.toOmissible()
)
)
link.player.searchAndPlayTrack(identifier, playOptionsBuilder)
}

suspend fun applyFilters(builder: Filters.() -> Unit) {
Expand Down Expand Up @@ -256,6 +247,7 @@ class MusicPlayer(val link: Link, private val guild: GuildBehavior) :
savedTrack = null
player.playTrack(track.track.track) {
position = track.position
filters = track.filters
}
return
}
Expand Down Expand Up @@ -303,6 +295,7 @@ class MusicPlayer(val link: Link, private val guild: GuildBehavior) :
savedTrack = null
player.playTrack(maybeSavedTrack.track.track) {
position = maybeSavedTrack.position
filters = maybeSavedTrack.filters
}
return
}
Expand Down

0 comments on commit 0943fc6

Please sign in to comment.