From e7001f9946259d1b7acfab1f7c22003b3ce16fe3 Mon Sep 17 00:00:00 2001 From: limited_dev Date: Thu, 18 May 2023 03:19:27 +0200 Subject: [PATCH] feat: added UpsertExtension to add a Queue into the first place of the queue Signed-off-by: limited_dev --- src/main/kotlin/de/limited_dev/botendo/Bot.kt | 1 + .../botendo/extensions/music/PlayExtension.kt | 12 +-- .../extensions/music/UpsertExtension.kt | 100 ++++++++++++++++++ .../music/components/GuildTrackScheduler.kt | 19 +++- .../music/components/LinkArguments.kt | 30 ++++++ .../music/components/MusicManager.kt | 33 ++++-- 6 files changed, 172 insertions(+), 23 deletions(-) create mode 100644 src/main/kotlin/de/limited_dev/botendo/extensions/music/UpsertExtension.kt create mode 100644 src/main/kotlin/de/limited_dev/botendo/extensions/music/components/LinkArguments.kt diff --git a/src/main/kotlin/de/limited_dev/botendo/Bot.kt b/src/main/kotlin/de/limited_dev/botendo/Bot.kt index 1e3ba7a..91d756f 100644 --- a/src/main/kotlin/de/limited_dev/botendo/Bot.kt +++ b/src/main/kotlin/de/limited_dev/botendo/Bot.kt @@ -63,6 +63,7 @@ object Bot { add(::SkipExtension) add(::NowPlayingExtension) add(::QueueExtension) + add(::UpsertExtension) } this.presence { diff --git a/src/main/kotlin/de/limited_dev/botendo/extensions/music/PlayExtension.kt b/src/main/kotlin/de/limited_dev/botendo/extensions/music/PlayExtension.kt index 301c2c4..fa92f32 100644 --- a/src/main/kotlin/de/limited_dev/botendo/extensions/music/PlayExtension.kt +++ b/src/main/kotlin/de/limited_dev/botendo/extensions/music/PlayExtension.kt @@ -19,12 +19,11 @@ package de.limited_dev.botendo.extensions.music -import com.kotlindiscord.kord.extensions.commands.Arguments -import com.kotlindiscord.kord.extensions.commands.converters.impl.string import com.kotlindiscord.kord.extensions.extensions.Extension import com.kotlindiscord.kord.extensions.extensions.publicSlashCommand import com.kotlindiscord.kord.extensions.types.respond import de.limited_dev.botendo.Bot +import de.limited_dev.botendo.extensions.music.components.LinkArguments import de.limited_dev.botendo.extensions.music.components.MusicManager import de.limited_dev.botendo.util.MessageUtil import dev.kord.common.Color @@ -37,7 +36,7 @@ class PlayExtension : Extension() { override val name = "play" override suspend fun setup() { - publicSlashCommand(::PlayArgs) { + publicSlashCommand(::LinkArguments) { name = "play" description = "Play music" this.action { @@ -97,12 +96,5 @@ class PlayExtension : Extension() { } } } - - inner class PlayArgs : Arguments() { - val linkquery by string { - name = "linkqery" - description = "Song link or search query" - } - } } diff --git a/src/main/kotlin/de/limited_dev/botendo/extensions/music/UpsertExtension.kt b/src/main/kotlin/de/limited_dev/botendo/extensions/music/UpsertExtension.kt new file mode 100644 index 0000000..2b0fe14 --- /dev/null +++ b/src/main/kotlin/de/limited_dev/botendo/extensions/music/UpsertExtension.kt @@ -0,0 +1,100 @@ +/* + * Botendo + * Copyright (C) 2023 moonleay + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + * + */ + +package de.limited_dev.botendo.extensions.music + +import com.kotlindiscord.kord.extensions.extensions.Extension +import com.kotlindiscord.kord.extensions.extensions.publicSlashCommand +import com.kotlindiscord.kord.extensions.types.respond +import de.limited_dev.botendo.Bot +import de.limited_dev.botendo.extensions.music.components.LinkArguments +import de.limited_dev.botendo.extensions.music.components.MusicManager +import de.limited_dev.botendo.util.MessageUtil +import dev.kord.common.Color +import dev.schlaubi.lavakord.audio.Link +import dev.schlaubi.lavakord.kord.getLink + + +class UpsertExtension : Extension() { + + override val name = "upsert" + + override suspend fun setup() { + publicSlashCommand(::LinkArguments) { + name = "upsert" + description = "Upsert music" + this.action { + val guildId = this.guild!!.id + val link = Bot.lava.getLink(guildId) + val u = this.user + val vcsUser = u.asMember(guildId).getVoiceStateOrNull() + if (vcsUser == null) { + this.respond { + embeds.add( + MessageUtil.getEmbed( + Color(0xE0311A), + "You are not connected to a VC", + "Please connect to a VC", + u.asUser().username + "#" + u.asUser().discriminator + ) + ) + } + return@action + } + + val channelId = vcsUser.channelId + + if (link.state != Link.State.CONNECTED) { + link.connectAudio(channelId!!.value) + } else if (link.state == Link.State.CONNECTED && link.lastChannelId != channelId!!.value) { + this.respond { + embeds.add( + MessageUtil.getEmbed( + Color(0xE0311A), + "You are not in my VC", + "We are not in the same VC and therefore, you cannot play any music", + u.asUser().username + "#" + u.asUser().discriminator + ) + ) + } + return@action + } + + val query = arguments.linkquery + val search = if (query.startsWith("http")) { + query + } else { + "ytsearch:$query" + } + this.respond { + embeds.add( + MessageUtil.getEmbed( + Color(0xE0A81A), + "Searching...", + "We are looking for $query", + u.asUser().username + "#" + u.asUser().discriminator + ) + ) + } + MusicManager.upsertIntoQueue(this, link, search) + } + } + } +} + diff --git a/src/main/kotlin/de/limited_dev/botendo/extensions/music/components/GuildTrackScheduler.kt b/src/main/kotlin/de/limited_dev/botendo/extensions/music/components/GuildTrackScheduler.kt index 992fee8..57505be 100644 --- a/src/main/kotlin/de/limited_dev/botendo/extensions/music/components/GuildTrackScheduler.kt +++ b/src/main/kotlin/de/limited_dev/botendo/extensions/music/components/GuildTrackScheduler.kt @@ -34,11 +34,24 @@ class GuildTrackScheduler(val pl: Player) { private var hasRegisteredEvents = false ///Add a track to queue and start playing, if there is no song currently playing - suspend fun queue(track: PartialTrack) { + suspend fun queue(track: PartialTrack, type: MusicManager.AddType) { if (this.pl.playingTrack == null) { play(track) - } else { - queue.offer(track) + return + } + when (type) { + MusicManager.AddType.QUEUE -> { + queue.offer(track) + } + + MusicManager.AddType.UPSERT -> { + val nq = LinkedBlockingQueue() + nq.offer(track) + for (t in queue.toList()) { + nq.offer(t) + } + queue = nq + } } } diff --git a/src/main/kotlin/de/limited_dev/botendo/extensions/music/components/LinkArguments.kt b/src/main/kotlin/de/limited_dev/botendo/extensions/music/components/LinkArguments.kt new file mode 100644 index 0000000..f364e01 --- /dev/null +++ b/src/main/kotlin/de/limited_dev/botendo/extensions/music/components/LinkArguments.kt @@ -0,0 +1,30 @@ +/* + * Botendo + * Copyright (C) 2023 moonleay + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + * + */ + +package de.limited_dev.botendo.extensions.music.components + +import com.kotlindiscord.kord.extensions.commands.Arguments +import com.kotlindiscord.kord.extensions.commands.converters.impl.string + +class LinkArguments : Arguments() { + val linkquery by string { + name = "linkqery" + description = "Song link or search query" + } +} diff --git a/src/main/kotlin/de/limited_dev/botendo/extensions/music/components/MusicManager.kt b/src/main/kotlin/de/limited_dev/botendo/extensions/music/components/MusicManager.kt index a1342a9..c9ebd9d 100644 --- a/src/main/kotlin/de/limited_dev/botendo/extensions/music/components/MusicManager.kt +++ b/src/main/kotlin/de/limited_dev/botendo/extensions/music/components/MusicManager.kt @@ -22,7 +22,6 @@ package de.limited_dev.botendo.extensions.music.components import com.kotlindiscord.kord.extensions.commands.application.slash.PublicSlashCommandContext import com.kotlindiscord.kord.extensions.components.forms.ModalForm import com.kotlindiscord.kord.extensions.types.respond -import de.limited_dev.botendo.extensions.music.PlayExtension import de.limited_dev.botendo.util.ButtonUtil import de.limited_dev.botendo.util.MessageUtil import de.limited_dev.botendo.util.TimeUtil @@ -47,17 +46,26 @@ object MusicManager { suspend fun addToQueue( - ctx: PublicSlashCommandContext, + ctx: PublicSlashCommandContext, link: Link, search: String ) { - addToQueue(ctx, link, search, false) + addToQueue(ctx, link, search, AddType.QUEUE, false) + } + + suspend fun upsertIntoQueue( + ctx: PublicSlashCommandContext, + link: Link, + search: String + ) { + addToQueue(ctx, link, search, AddType.UPSERT, false) } suspend fun addToQueue( - ctx: PublicSlashCommandContext, + ctx: PublicSlashCommandContext, link: Link, search: String, + type: AddType, silent: Boolean ) { val player = link.player @@ -69,13 +77,13 @@ object MusicManager { when (item.loadType) { TrackResponse.LoadType.TRACK_LOADED -> { - gts.queue(item.track) + gts.queue(item.track, type) if (!silent) ctx.respond { this.embeds.add( MessageUtil.getEmbedWithImage( Color(0x52E01A), - "Queuing track from link", + "${type.s} track from link", "**${item.track.info.title}**\n*Queue*\nby ${item.track.info.author} ;: ${ TimeUtil.getTimeFormatedRaw( item.track.info.length @@ -100,14 +108,14 @@ object MusicManager { TrackResponse.LoadType.PLAYLIST_LOADED -> { val l = item.tracks.reversed() for (partialTrack in l) { - gts.queue(partialTrack) + gts.queue(partialTrack, type) } if (!silent) ctx.respond { this.embeds.add( MessageUtil.getEmbedWithImage( Color(0x52E01A), - "Queuing playlist from link", + "${type.s} playlist from link", "**${item.tracks.first().info.title}**\n*${item.playlistInfo.name}*\nby ${item.tracks.first().info.author} ;: ${ TimeUtil.getTimeFormatedRaw( item.tracks.first().info.length @@ -130,13 +138,13 @@ object MusicManager { } TrackResponse.LoadType.SEARCH_RESULT -> { - gts.queue(item.tracks.first()) + gts.queue(item.tracks.first(), type) if (!silent) ctx.respond { this.embeds.add( MessageUtil.getEmbedWithImage( Color(0x52E01A), - "Queuing track from query", + "${type.s} track from query", "**${item.tracks.first().info.title}**\n*Queue*\nby ${item.tracks.first().info.author} ;: ${ TimeUtil.getTimeFormatedRaw( item.tracks.first().info.length @@ -186,4 +194,9 @@ object MusicManager { } } } + + enum class AddType(val s: String) { + QUEUE("Added"), + UPSERT("Upserted") + } }