From 3158f95baec958e8ce70704b551006b162fadd19 Mon Sep 17 00:00:00 2001 From: Michael Rittmeister Date: Tue, 3 Sep 2024 17:20:06 +0200 Subject: [PATCH] Improve music plugin --- gradle/libs.versions.toml | 4 +- music/build.gradle.kts | 2 +- .../mikmusic/player/queue/SongSearch.kt | 47 ++++++++----------- .../mikmusic/player/queue/TrackFinder.kt | 2 +- 4 files changed, 23 insertions(+), 32 deletions(-) diff --git a/gradle/libs.versions.toml b/gradle/libs.versions.toml index f50616251..ad5fb7596 100644 --- a/gradle/libs.versions.toml +++ b/gradle/libs.versions.toml @@ -6,9 +6,9 @@ coroutines = "1.8.1" serialization = "1.7.1" ktor = "2.3.11" kord = "feature-user-apps-20240825.234248-3" -api = "3.35.4" +api = "3.35.5" ksp = "2.0.0-1.0.22" -lavakord = "6.4.0-SNAPSHOT" +lavakord = "7.1.0" [libraries] kord-common = { group = "dev.kord", name = "kord-common", version.ref = "kord" } diff --git a/music/build.gradle.kts b/music/build.gradle.kts index 6482d145f..c74158fe4 100644 --- a/music/build.gradle.kts +++ b/music/build.gradle.kts @@ -1,3 +1,3 @@ subprojects { - version = "3.9.0-SNAPSHOT" + version = "3.10.0-SNAPSHOT" } diff --git a/music/player/src/main/kotlin/dev/schlaubi/mikmusic/player/queue/SongSearch.kt b/music/player/src/main/kotlin/dev/schlaubi/mikmusic/player/queue/SongSearch.kt index 8727c63ec..03e9a6723 100644 --- a/music/player/src/main/kotlin/dev/schlaubi/mikmusic/player/queue/SongSearch.kt +++ b/music/player/src/main/kotlin/dev/schlaubi/mikmusic/player/queue/SongSearch.kt @@ -1,30 +1,27 @@ package dev.schlaubi.mikmusic.player.queue import com.kotlindiscord.kord.extensions.commands.CommandContext +import com.kotlindiscord.kord.extensions.components.publicStringSelectMenu import com.kotlindiscord.kord.extensions.pagination.BaseButtonPaginator import com.kotlindiscord.kord.extensions.pagination.builders.PaginatorBuilder -import com.kotlindiscord.kord.extensions.utils.permissionsForMember -import com.kotlindiscord.kord.extensions.utils.waitForResponse import dev.arbjerg.lavalink.protocol.v4.LoadResult import dev.arbjerg.lavalink.protocol.v4.Track -import dev.kord.common.entity.Permission import dev.kord.core.behavior.UserBehavior -import dev.kord.core.entity.channel.GuildChannel -import dev.schlaubi.mikbot.plugin.api.util.EditableMessageSender +import dev.schlaubi.mikbot.plugin.api.util.discordError import dev.schlaubi.mikbot.plugin.api.util.forList import dev.schlaubi.mikmusic.util.format -import kotlin.time.Duration.Companion.minutes +import dev.schlaubi.stdx.core.limit +import kotlinx.coroutines.CompletableDeferred typealias EditingPaginatorBuilder = PaginatorBuilder.() -> Unit typealias EditingPaginatorSender = suspend (EditingPaginatorBuilder) -> BaseButtonPaginator suspend fun CommandContext.searchSong( - respond: EditableMessageSender, editingPaginator: EditingPaginatorSender, user: UserBehavior, - result: LoadResult.SearchResult + result: LoadResult.SearchResult, ): SingleTrack? { - val tracks = result.data.tracks + val tracks = result.data.tracks.take(25) val paginator = editingPaginator { forList( user, @@ -35,30 +32,24 @@ suspend fun CommandContext.searchSong( } ) } - - paginator.send() - - val response = waitForResponse(timeout = 2.minutes.inWholeMilliseconds) { - val index = message.content.toIntOrNull() ?: -1 - val pass = index <= tracks.size && index > 0 - - if (!pass) { - respond { content = "Please enter a valid number!" } + val future = CompletableDeferred() + paginator.components.publicStringSelectMenu { + result.data.tracks.forEach { (_, info) -> + option("${info.title} - ${info.author}".limit(100), info.identifier) } - pass + action { + future.complete(this.selected.first()) + } } + + paginator.send() + + val selection = future.await() paginator.destroy() - val channel = response?.channel?.asChannel() ?: return null - val kord = channel.kord - if ((channel as GuildChannel).permissionsForMember(kord.getUser(kord.selfId)!!) - .contains(Permission.ManageMessages) - ) { - response.delete() - } - val index = response.content.toInt() - 1 - val track = tracks[index] + val track = tracks.firstOrNull { it.info.identifier == selection } + ?: discordError(translate("music.queue.search.not_found")) return SingleTrack(track) } diff --git a/music/player/src/main/kotlin/dev/schlaubi/mikmusic/player/queue/TrackFinder.kt b/music/player/src/main/kotlin/dev/schlaubi/mikmusic/player/queue/TrackFinder.kt index b65ed6f9a..c8908af23 100644 --- a/music/player/src/main/kotlin/dev/schlaubi/mikmusic/player/queue/TrackFinder.kt +++ b/music/player/src/main/kotlin/dev/schlaubi/mikmusic/player/queue/TrackFinder.kt @@ -124,7 +124,7 @@ internal suspend fun CommandContext.findTracks( editingPaginator: EditingPaginatorSender, ): QueueSearchResult? = findTracks(node, arguments, respond) search@{ result -> if (search) { - searchSong(respond, editingPaginator, getUser()!!, result) ?: return@search null + searchSong(editingPaginator, getUser()!!, result) } else { val foundTrack = result.data.tracks.first() SingleTrack(foundTrack)