Skip to content

Commit

Permalink
Update music plugin
Browse files Browse the repository at this point in the history
- Show author for albums and tracks in autocomplete
- Update sponsorblock state more often
- Update Gradle
  • Loading branch information
DRSchlaubi committed Dec 16, 2023
1 parent 5b57b3e commit 557b2ca
Show file tree
Hide file tree
Showing 7 changed files with 36 additions and 14 deletions.
2 changes: 1 addition & 1 deletion .idea/compiler.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Binary file modified gradle/wrapper/gradle-wrapper.jar
Binary file not shown.
2 changes: 1 addition & 1 deletion gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-8.4-bin.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-8.5-bin.zip
networkTimeout=10000
validateDistributionUrl=true
zipStoreBase=GRADLE_USER_HOME
Expand Down
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.4.0-SNAPSHOT"
version = "3.4.1-SNAPSHOT"
}
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,11 @@ fun Arguments.autoCompletedYouTubeQuery(description: String): SingleConverter<St
}
result.tracks.take(5).forEach { track ->
val uri = track.info.uri ?: return@forEach
choice("${Emojis.notes} ${track.info.title}", uri)
choice("${Emojis.notes} ${track.info.author} - ${track.info.title}", uri)
}
result.albums.take(5).forEach { playlist ->
choice("${Emojis.cd} ${playlist.info.name}", playlist.lavaSrcInfo.url)
val albumInfo = playlist.lavaSrcInfo
choice("${Emojis.cd} ${albumInfo.author} - ${playlist.info.name}", albumInfo.url)
}
result.playlists.take(5).forEach { playlist ->
choice("${Emojis.scroll} ${playlist.info.name}", playlist.lavaSrcInfo.url)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import dev.schlaubi.mikmusic.core.audio.LavalinkManager
import dev.schlaubi.mikmusic.player.MusicPlayer
import dev.schlaubi.mikmusic.player.PersistentPlayerState
import dev.schlaubi.mikmusic.player.applyToPlayer
import dev.schlaubi.mikmusic.player.voiceStateWatcher
import kotlinx.coroutines.coroutineScope
import kotlinx.coroutines.launch
import org.koin.core.component.inject
Expand Down Expand Up @@ -71,6 +72,7 @@ class MusicModule(context: PluginContext) : MikBotModule(context) {
}
}

voiceStateWatcher()
event<ReadyEvent> {
action {
reconnectPlayers()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ 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.getSponsorblockCategories
import dev.schlaubi.lavakord.plugins.sponsorblock.rest.putSponsorblockCategories
import dev.schlaubi.mikmusic.core.settings.MusicSettingsDatabase
import dev.schlaubi.mikmusic.musicchannel.updateMessage
Expand All @@ -37,8 +38,7 @@ import kotlin.time.toDuration

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

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

private val lock = Mutex()

Expand All @@ -62,23 +62,36 @@ class MusicPlayer(val link: Link, private val guild: GuildBehavior) :
val settings = MusicSettingsDatabase.findGuild(guild)

settings.defaultSchedulerSettings?.applyToPlayer(this@MusicPlayer)

if (settings.useSponsorBlock) {
player.putSponsorblockCategories(Category.MusicOfftopic, Category.Filler, Category.Selfpromo, Category.Sponsor)
} else {
player.disableSponsorblock()
}
}

updateSponsorBlock()

link.player.on(consumer = ::onTrackEnd)
link.player.on(consumer = ::onTrackStart)
link.player.on(consumer = ::onChaptersLoaded)
link.player.on(consumer = ::onChapterStarted)
}

private fun updateSponsorBlock() = guild.kord.launch {
val settings = MusicSettingsDatabase.findGuild(guild)
val categories = player.getSponsorblockCategories()

if (categories.isEmpty() && settings.useSponsorBlock) {
player.putSponsorblockCategories(
Category.MusicOfftopic,
Category.Filler,
Category.Selfpromo,
Category.Sponsor
)
} else if (categories.isNotEmpty()) {
player.disableSponsorblock()
}
}

suspend fun getChannel() = link.lastChannelId
?.let { guild.kord.getChannelOf<VoiceChannel>(Snowflake(it)) }

@Suppress("unused") // used by other plugins
fun updateMusicChannelState(to: Boolean) {
if (to) {
queue.clear()
Expand Down Expand Up @@ -201,7 +214,12 @@ class MusicPlayer(val link: Link, private val guild: GuildBehavior) :
updateMusicChannelMessage()
}

suspend fun injectTrack(identifier: String, noReplace: Boolean = false, playOptionsBuilder: PlayOptions.() -> Unit) = lock.withLock {
@Suppress("unused") // used by Tonbrett
suspend fun injectTrack(
identifier: String,
noReplace: Boolean = false,
playOptionsBuilder: PlayOptions.() -> Unit,
) = lock.withLock {
dontQueue = true
val currentTrack = playingTrack
if (currentTrack != null && !noReplace) {
Expand Down Expand Up @@ -318,6 +336,7 @@ class MusicPlayer(val link: Link, private val guild: GuildBehavior) :

// called under lock
private suspend fun startNextSong(lastSong: Track? = null, force: Boolean = false, position: Duration? = null) {
updateSponsorBlock()
val nextTrack: QueuedTrack? = when {
lastSong != null && repeat -> playingTrack!!
!force && (shuffle || (loopQueue && queue.isEmpty())) -> {
Expand Down

0 comments on commit 557b2ca

Please sign in to comment.