From 895148f6433391f520ad162a6732557def68b242 Mon Sep 17 00:00:00 2001 From: Michael Rittmeister Date: Wed, 17 Jan 2024 20:49:52 +0100 Subject: [PATCH] Add autocomplete to playlist commands --- music/build.gradle.kts | 2 +- .../mikmusic/playlist/commands/Common.kt | 24 ++++++++++++++++++- .../mikmusic/playlist/commands/LoadCommand.kt | 2 +- .../mikmusic/playlist/commands/SaveCommand.kt | 1 + 4 files changed, 26 insertions(+), 3 deletions(-) diff --git a/music/build.gradle.kts b/music/build.gradle.kts index 52e56cf7f..704f5d674 100644 --- a/music/build.gradle.kts +++ b/music/build.gradle.kts @@ -1,3 +1,3 @@ subprojects { - version = "3.5.5-SNAPSHOT" + version = "3.5.6-SNAPSHOT" } diff --git a/music/commands/src/main/kotlin/dev/schlaubi/mikmusic/playlist/commands/Common.kt b/music/commands/src/main/kotlin/dev/schlaubi/mikmusic/playlist/commands/Common.kt index ba7f63938..864879587 100644 --- a/music/commands/src/main/kotlin/dev/schlaubi/mikmusic/playlist/commands/Common.kt +++ b/music/commands/src/main/kotlin/dev/schlaubi/mikmusic/playlist/commands/Common.kt @@ -6,6 +6,7 @@ import com.kotlindiscord.kord.extensions.commands.CommandContext import com.kotlindiscord.kord.extensions.commands.application.slash.EphemeralSlashCommandContext import com.kotlindiscord.kord.extensions.commands.converters.impl.string import dev.kord.core.behavior.UserBehavior +import dev.kord.core.behavior.interaction.suggestString import dev.schlaubi.mikbot.plugin.api.PluginContext import dev.schlaubi.mikbot.plugin.api.module.SubCommandModule import dev.schlaubi.mikbot.plugin.api.util.extension @@ -13,11 +14,14 @@ import dev.schlaubi.mikmusic.core.MusicModule import dev.schlaubi.mikmusic.player.MusicPlayer import dev.schlaubi.mikmusic.playlist.Playlist import dev.schlaubi.mikmusic.playlist.PlaylistDatabase +import kotlinx.coroutines.flow.take +import kotlinx.coroutines.flow.toList import org.litote.kmongo.and import org.litote.kmongo.eq import org.litote.kmongo.or +import org.litote.kmongo.util.KMongoUtil -abstract class PlaylistArguments : Arguments() { +abstract class PlaylistArguments(val onlyMine:Boolean = true) : Arguments() { val name by string { name = "name" description = "The name of the playlist" @@ -25,6 +29,24 @@ abstract class PlaylistArguments : Arguments() { validate { getPlaylistOrNull(context.getUser()!!, value) ?: context.notFound(value) } + + autoComplete { + val genericFilter = if (onlyMine) { + Playlist::authorId eq user.id + } else { + or(Playlist::public eq true, Playlist::authorId eq user.id) + } + val input = focusedOption.value + val names = PlaylistDatabase.collection.find( + and(genericFilter, + KMongoUtil.toBson("{name: /$input/i}")) + ).toFlow() + .take(25) + .toList() + suggestString { + names.forEach { choice(it.name, it.name) } + } + } } private suspend fun CommandContext.notFound(value: String): Nothing { diff --git a/music/commands/src/main/kotlin/dev/schlaubi/mikmusic/playlist/commands/LoadCommand.kt b/music/commands/src/main/kotlin/dev/schlaubi/mikmusic/playlist/commands/LoadCommand.kt index 84cdb8048..20b16e898 100644 --- a/music/commands/src/main/kotlin/dev/schlaubi/mikmusic/playlist/commands/LoadCommand.kt +++ b/music/commands/src/main/kotlin/dev/schlaubi/mikmusic/playlist/commands/LoadCommand.kt @@ -4,7 +4,7 @@ import dev.schlaubi.mikmusic.checks.joinSameChannelCheck import dev.schlaubi.mikmusic.playlist.PlaylistDatabase import dev.schlaubi.mikmusic.util.mapToQueuedTrack -class LoadArguments : PlaylistArguments() +class LoadArguments : PlaylistArguments(onlyMine = false) fun PlaylistModule.loadCommand() = ephemeralSubCommand(::LoadArguments) { name = "load" diff --git a/music/commands/src/main/kotlin/dev/schlaubi/mikmusic/playlist/commands/SaveCommand.kt b/music/commands/src/main/kotlin/dev/schlaubi/mikmusic/playlist/commands/SaveCommand.kt index cc03d5f07..bd3b5b4a3 100644 --- a/music/commands/src/main/kotlin/dev/schlaubi/mikmusic/playlist/commands/SaveCommand.kt +++ b/music/commands/src/main/kotlin/dev/schlaubi/mikmusic/playlist/commands/SaveCommand.kt @@ -42,6 +42,7 @@ fun PlaylistModule.saveCommand() = ephemeralSubCommand(::PlaylistSaveArguments) respond { content = translate("music.checks.not_playing") } + return@action } checkName(arguments.name, arguments.public) {