Skip to content

Commit

Permalink
ExportCommand impl
Browse files Browse the repository at this point in the history
  • Loading branch information
ileukocyte committed Aug 15, 2022
1 parent aa92bea commit 3956fbf
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 6 deletions.
14 changes: 8 additions & 6 deletions src/main/kotlin/io/ileukocyte/hibernum/audio/music.kt
Original file line number Diff line number Diff line change
Expand Up @@ -114,8 +114,7 @@ data class TrackUserData(
val playCount: Int = 0,
)

@Suppress("UNUSED")
fun GuildMusicManager.exportQueueAsJson(): JsonObject {
fun GuildMusicManager.exportQueueAsJson(includeInternalData: Boolean): JsonObject {
fun AudioTrack.exportTrackAsJson(isCurrent: Boolean): JsonObject {
val uri = info.uri
val title = info.title
Expand All @@ -135,14 +134,17 @@ fun GuildMusicManager.exportQueueAsJson(): JsonObject {
"requester_id" to JsonPrimitive(requester),
"channel_id" to JsonPrimitive(channel),
"thumbnail" to JsonPrimitive(thumbnail),
"announce_queueing" to JsonPrimitive(announceQueueing),
"first_to_play" to JsonPrimitive(isFirstToPlay),
"play_count" to JsonPrimitive(playCount),
)

if (isCurrent) {
map["position_millis"] = JsonPrimitive(position)
if (includeInternalData) {
map["first_to_play"] = JsonPrimitive(isFirstToPlay)
map["announce_queueing"] = JsonPrimitive(announceQueueing)
map["announcement_id"] = JsonPrimitive(announcement)

if (isCurrent) {
map["position_millis"] = JsonPrimitive(position)
}
}

return JsonObject(map)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package io.ileukocyte.hibernum.commands.music

import io.ileukocyte.hibernum.audio.audioPlayer
import io.ileukocyte.hibernum.audio.exportQueueAsJson
import io.ileukocyte.hibernum.commands.CommandException
import io.ileukocyte.hibernum.commands.TextCommand
import io.ileukocyte.hibernum.extensions.EmbedType
import io.ileukocyte.hibernum.extensions.defaultEmbed

import kotlinx.serialization.json.Json
import kotlinx.serialization.serializer

import net.dv8tion.jda.api.events.interaction.command.SlashCommandInteractionEvent
import net.dv8tion.jda.api.events.message.MessageReceivedEvent
import net.dv8tion.jda.api.utils.FileUpload

class ExportCommand : TextCommand {
override val name = "export"
override val description = "Sends the current queue and parameters of the player as a JSON file"
override val aliases = setOf("export-queue", "save-queue")
override val cooldown = 10L

private val json = Json { prettyPrint = true }

override suspend fun invoke(event: MessageReceivedEvent, args: String?) {
val audioPlayer = event.guild.audioPlayer ?: return

if (audioPlayer.player.playingTrack === null) {
throw CommandException("No track is currently playing!")
}

val queue = audioPlayer.exportQueueAsJson(false)
val file = FileUpload.fromData(
json.encodeToString(serializer(), queue).toByteArray(),
"queue.json",
)

val embed = defaultEmbed("The queue has been exported as a file!", EmbedType.SUCCESS)

event.channel.sendFiles(file).setEmbeds(embed).queue(null) { file.close() }
}

override suspend fun invoke(event: SlashCommandInteractionEvent) {
val audioPlayer = event.guild?.audioPlayer ?: return

if (audioPlayer.player.playingTrack === null) {
throw CommandException("No track is currently playing!")
}

val queue = audioPlayer.exportQueueAsJson(false)
val file = FileUpload.fromData(
json.encodeToString(serializer(), queue).toByteArray(),
"queue.json",
)

val embed = defaultEmbed("The queue has been exported as a file!", EmbedType.SUCCESS)

event.replyFiles(file).setEmbeds(embed).queue(null) {
event.channel.sendFiles(file).setEmbeds(embed).queue(null) {
file.close()
}
}
}
}

0 comments on commit 3956fbf

Please sign in to comment.