feat: added UpsertExtension to add a Queue into the first place of the queue

Signed-off-by: limited_dev <loginakkisativ@gmail.com>
This commit is contained in:
limited_dev 2023-05-18 03:19:27 +02:00
parent 1654d24378
commit e7001f9946
6 changed files with 172 additions and 23 deletions

View file

@ -63,6 +63,7 @@ object Bot {
add(::SkipExtension)
add(::NowPlayingExtension)
add(::QueueExtension)
add(::UpsertExtension)
}
this.presence {

View file

@ -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"
}
}
}

View file

@ -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 <https://www.gnu.org/licenses/>.
*
*/
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)
}
}
}
}

View file

@ -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<PartialTrack>()
nq.offer(track)
for (t in queue.toList()) {
nq.offer(t)
}
queue = nq
}
}
}

View file

@ -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 <https://www.gnu.org/licenses/>.
*
*/
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"
}
}

View file

@ -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<PlayExtension.PlayArgs, ModalForm>,
ctx: PublicSlashCommandContext<LinkArguments, ModalForm>,
link: Link,
search: String
) {
addToQueue(ctx, link, search, false)
addToQueue(ctx, link, search, AddType.QUEUE, false)
}
suspend fun upsertIntoQueue(
ctx: PublicSlashCommandContext<LinkArguments, ModalForm>,
link: Link,
search: String
) {
addToQueue(ctx, link, search, AddType.UPSERT, false)
}
suspend fun addToQueue(
ctx: PublicSlashCommandContext<PlayExtension.PlayArgs, ModalForm>,
ctx: PublicSlashCommandContext<LinkArguments, ModalForm>,
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")
}
}