Skip to content

Commit

Permalink
Add autocomplete to playlist commands
Browse files Browse the repository at this point in the history
  • Loading branch information
DRSchlaubi committed Jan 17, 2024
1 parent b559647 commit 895148f
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 3 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.5-SNAPSHOT"
version = "3.5.6-SNAPSHOT"
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,25 +6,47 @@ 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
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"

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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ fun PlaylistModule.saveCommand() = ephemeralSubCommand(::PlaylistSaveArguments)
respond {
content = translate("music.checks.not_playing")
}
return@action
}

checkName(arguments.name, arguments.public) {
Expand Down

0 comments on commit 895148f

Please sign in to comment.