From d496945949e58d7430c61177a3d38f0e178195e2 Mon Sep 17 00:00:00 2001 From: Marius Begby Date: Tue, 19 Dec 2023 12:13:53 +0100 Subject: [PATCH] fix: Add track position to footer when track(s) added to queue in /play --- locales/en/bot.json | 3 +- locales/no/bot.json | 3 +- locales/resources.d.ts | 1 + src/interactions/commands/player/play.ts | 47 ++++++++++++++++++++---- 4 files changed, 44 insertions(+), 10 deletions(-) diff --git a/locales/en/bot.json b/locales/en/bot.json index 231fb76..1102a02 100644 --- a/locales/en/bot.json +++ b/locales/en/bot.json @@ -190,7 +190,8 @@ "playlistAddedTrackCount_one": "And **1** more track... Use {{queueCommand}} to view all.", "playlistAddedTrackCount": "And **{{count}}** more tracks... Use {{queueCommand}} to view all.", "playlistTooLarge": "{{icon}} **Playlist too large**\nThis playlist is too large to be added to the queue.\n\nThe maximum amount of tracks that can be added to the queue is **{{count}}**.", - "trackNotFound": "{{icon}} **No track found\nNo results found for **{{query}}**.\n\nIf you specified a URL, please make sure it is valid and public." + "trackNotFound": "{{icon}} **No track found\nNo results found for **{{query}}**.\n\nIf you specified a URL, please make sure it is valid and public.", + "footerAddedPosition": "Added to queue at position {{position}}" }, "queue": { "metadata": { diff --git a/locales/no/bot.json b/locales/no/bot.json index 201ce6d..ce867ec 100644 --- a/locales/no/bot.json +++ b/locales/no/bot.json @@ -190,7 +190,8 @@ "playlistAddedTrackCount_one": "Og **1** til låt... Bruk {{queueCommand}} for å se alle.", "playlistAddedTrackCount": "Og **{{count}}** flere låter... Bruk {{queueCommand}} for å se alle.", "playlistTooLarge": "{{icon}} **Spilleliste for stor**\nDenne spillelisten er for stor til å legges til i køen.\n\nMaksimalt antall låter som kan legges til i køen er **{{count}}**.", - "trackNotFound": "{{icon}} **Ingen låter funnet**\nIngen resultater funnet for **{{query}}**.\n\nHvis du angav en URL, vær sikker på at den er gyldig og offentlig." + "trackNotFound": "{{icon}} **Ingen låter funnet**\nIngen resultater funnet for **{{query}}**.\n\nHvis du angav en URL, vær sikker på at den er gyldig og offentlig.", + "footerAddedPosition": "Lagt til i posisjon {{position}} i køen" }, "queue": { "metadata": { diff --git a/locales/resources.d.ts b/locales/resources.d.ts index a9d7115..b0c2350 100644 --- a/locales/resources.d.ts +++ b/locales/resources.d.ts @@ -192,6 +192,7 @@ interface Resources { playlistAddedTrackCount: 'And **{{count}}** more tracks... Use {{queueCommand}} to view all.'; playlistTooLarge: '{{icon}} **Playlist too large**\nThis playlist is too large to be added to the queue.\n\nThe maximum amount of tracks that can be added to the queue is **{{count}}**.'; trackNotFound: '{{icon}} **No track found\nNo results found for **{{query}}**.\n\nIf you specified a URL, please make sure it is valid and public.'; + footerAddedPosition: 'Added to queue at position {{position}}'; }; queue: { metadata: { diff --git a/src/interactions/commands/player/play.ts b/src/interactions/commands/player/play.ts index 7fa7e5a..3dab4e0 100644 --- a/src/interactions/commands/player/play.ts +++ b/src/interactions/commands/player/play.ts @@ -1,5 +1,12 @@ import { GuildQueue, Player, SearchResult, Track, useMainPlayer, useQueue } from 'discord-player'; -import { ChatInputCommandInteraction, EmbedBuilder, GuildMember, Message, SlashCommandBuilder } from 'discord.js'; +import { + ChatInputCommandInteraction, + EmbedBuilder, + EmbedFooterData, + GuildMember, + Message, + SlashCommandBuilder +} from 'discord.js'; import { Logger } from 'pino'; import { BaseSlashCommandInteraction, CustomError } from '../../../classes/interactions'; import { BaseSlashCommandParams, BaseSlashCommandReturnType } from '../../../types/interactionTypes'; @@ -155,6 +162,10 @@ class PlayCommand extends BaseSlashCommandInteraction { const queue: GuildQueue = useQueue(interaction.guild!.id)!; const trackUrl = this.getDisplayTrackDurationAndUrl(track, translator); + let embedFooter: EmbedFooterData | undefined = this.getDisplayFooterTrackPosition( + queue.tracks.data.length ?? 0, + translator + ); let message = translator('commands.play.addedToQueueTitle', { icon: this.embedOptions.icons.success @@ -174,6 +185,10 @@ class PlayCommand extends BaseSlashCommandInteraction { count: searchResult.tracks.length, queueCommand: formatSlashCommand('queue', translator) }); + if (queue.tracks.data.length != searchResult.tracks.length) { + const posistionFirstTrackInPlaylist = queue.tracks.data.length - searchResult.tracks.length + 1; + embedFooter = this.getDisplayFooterTrackPosition(posistionFirstTrackInPlaylist, translator); + } } else if (queue.currentTrack === track && queue.tracks.data.length === 0) { message = translator('musicPlayerCommon.nowPlayingTitle', { @@ -181,20 +196,36 @@ class PlayCommand extends BaseSlashCommandInteraction { }) + '\n' + trackUrl; + embedFooter = undefined; + } + + const embed = new EmbedBuilder() + .setAuthor(this.getEmbedUserAuthor(interaction)) + .setDescription(message) + .setThumbnail(this.getTrackThumbnailUrl(track)) + .setColor(this.embedOptions.colors.success); + + if (embedFooter) { + embed.setFooter(embedFooter); } logger.debug('Responding with success embed.'); + return await interaction.editReply({ - embeds: [ - new EmbedBuilder() - .setAuthor(this.getEmbedUserAuthor(interaction)) - .setDescription(message) - .setThumbnail(this.getTrackThumbnailUrl(track)) - .setColor(this.embedOptions.colors.success) - ] + embeds: [embed] }); } + private getDisplayFooterTrackPosition(position: number, translator: TFunction): EmbedFooterData { + const fullFooterData = { + text: translator('commands.play.footerAddedPosition', { + position: position + }) + }; + + return fullFooterData; + } + private async handleNoResultsFound( transformedQuery: string, interaction: ChatInputCommandInteraction,