From 88b95665199c9edfa2edf0ebd551070742be363a Mon Sep 17 00:00:00 2001 From: Marius Begby Date: Mon, 10 Jul 2023 19:36:43 +0200 Subject: [PATCH] feat: Add playlist support to /play command --- commands/play.js | 115 +++++++++++++++-------------------------------- 1 file changed, 37 insertions(+), 78 deletions(-) diff --git a/commands/play.js b/commands/play.js index 00fb7d7..b2c7008 100644 --- a/commands/play.js +++ b/commands/play.js @@ -29,7 +29,9 @@ module.exports = { const player = useMainPlayer(); const query = interaction.options.getString('query'); - const searchResult = await player.search(query); + const searchResult = await player.search(query, { + requestedBy: interaction.user + }); if (!searchResult || searchResult.tracks.length === 0) { return await interaction.editReply({ @@ -43,22 +45,22 @@ module.exports = { }); } - try { - const { track } = await player.play( - interaction.member.voice.channel, - searchResult.tracks[0].url, - { - requestedBy: interaction.user, - nodeOptions: { - leaveOnEmptyCooldown: 60000, - leaveOnEndCooldown: 60000, - leaveOnStopCooldown: 60000, - maxSize: 10000, - maxHistorySize: 100 - } + const { track } = await player.play( + interaction.member.voice.channel, + searchResult, + { + requestedBy: interaction.user, + nodeOptions: { + leaveOnEmptyCooldown: 60000, + leaveOnEndCooldown: 60000, + leaveOnStopCooldown: 60000, + maxSize: 10000, + maxHistorySize: 100 } - ); + } + ); + if (searchResult.playlist && searchResult.tracks.length > 1) { return await interaction.editReply({ embeds: [ new EmbedBuilder() @@ -67,74 +69,31 @@ module.exports = { iconURL: interaction.user.avatarURL() }) .setDescription( - `**Added to queue**\n\`[${track.duration}]\` **[${track.title}](${track.url})**` + `**Added playlist to queue**\n\`[${ + track.duration + }]\` **[${track.title}](${track.url})**\n\nAnd \`${ + searchResult.tracks.length - 1 + }\` more tracks... \`/queue\` to view all.` ) .setThumbnail(track.thumbnail) .setColor(embedColors.colorSuccess) ] }); - } catch (e) { - if (e.message.includes('Could not extract stream for this track')) { - if (searchResult.tracks.length > 1) { - try { - const { track } = await player.play( - interaction.member.voice.channel, - searchResult.tracks[1].url, - { - requestedBy: interaction.user, - nodeOptions: { - leaveOnEmptyCooldown: 60000, - leaveOnEndCooldown: 60000, - leaveOnStopCooldown: 60000 - } - } - ); - - return await interaction.editReply({ - embeds: [ - new EmbedBuilder() - .setAuthor({ - name: interaction.user.username, - iconURL: interaction.user.avatarURL() - }) - .setDescription( - `**Added to queue**\n\`[${track.duration}]\` **[${track.title}](${track.url})**` - ) - .setThumbnail(track.thumbnail) - .setColor(embedColors.colorSuccess) - ] - }); - } catch (e) { - if ( - e.message.includes( - 'Could not extract stream for this track' - ) - ) { - return await interaction.editReply({ - embeds: [ - new EmbedBuilder() - .setDescription( - `**Failed**\nFound a result for query \`${query}\` with source ${searchResult.tracks[1].url}, but was unable to extract audio stream from track.\n\nThis is most likely due to audio source blocking download from the bot. Please try another track or refine your query.` - ) - .setColor(embedColors.colorError) - ] - }); - } - } - } else { - return await interaction.editReply({ - embeds: [ - new EmbedBuilder() - .setDescription( - `**Failed**\nFound a result for query \`${query}\` with source ${searchResult.tracks[0].url}, but was unable to extract audio stream from track.\n\nThis is most likely due to audio source blocking download from the bot or unsupported audio encoding. Please try another track or refine your query.` - ) - .setColor(embedColors.colorError) - ] - }); - } - } else { - throw e; - } } + + return await interaction.editReply({ + embeds: [ + new EmbedBuilder() + .setAuthor({ + name: interaction.user.username, + iconURL: interaction.user.avatarURL() + }) + .setDescription( + `**Added to queue**\n\`[${track.duration}]\` **[${track.title}](${track.url})**` + ) + .setThumbnail(track.thumbnail) + .setColor(embedColors.colorSuccess) + ] + }); } };