@@ -23,6 +23,21 @@ const unpause = require("./unpause.js");
2323// eslint-disable-next-line
2424// const ytRegex = /http(?:s?):\/\/(?:www\.)?youtu(?:be\.com\/watch\?v=|\.be\/)([\w\-\_]*)(&(amp;)?[\w\?=]*)?/;
2525
26+ async function convertSPDataToYT ( spotifyData ) {
27+ if ( spotifyData && spotifyData . type == "track" ) {
28+ // search youtube
29+ const data = await playdl . search ( `${ spotifyData . artists [ 0 ] . name } - ${ spotifyData . name } topic` , { "limit" :1 , "source" :{ "youtube" :"video" } } ) ;
30+ // if result, return first result
31+ if ( data && data [ 0 ] ) {
32+ return data [ 0 ] ;
33+ }
34+ else {
35+ return false ;
36+ }
37+ }
38+ }
39+
40+
2641function AddSCdataToQueue ( message , data ) {
2742 let artist = false ;
2843 if ( data . publisher && data . publisher . artist ) {
@@ -190,17 +205,9 @@ async function play(message, args, command) {
190205 else if ( validate == "sp_track" ) {
191206 // get spotify song data so we can search on soundcloud
192207 const spotifyData = await playdl . spotify ( args ) ;
193- if ( spotifyData && spotifyData . type == "track" ) {
194- // search youtube
195- const data = await playdl . search ( `${ spotifyData . artists [ 0 ] . name } - ${ spotifyData . name } topic` , { "limit" :1 , "source" :{ "youtube" :"video" } } ) ;
196- // if result, add first result to queue
197- if ( data && data [ 0 ] ) {
198- addYTdataToQueue ( message , data [ 0 ] ) ;
199- }
200- else {
201- message . react ( reactions . warning ) ;
202- return false ;
203- }
208+ const YTData = await convertSPDataToYT ( spotifyData ) ;
209+ if ( YTData ) {
210+ addYTdataToQueue ( message , YTData ) ;
204211 }
205212 else {
206213 message . react ( reactions . confused ) ;
@@ -279,6 +286,55 @@ async function play(message, args, command) {
279286 return false ;
280287 }
281288
289+ }
290+ else if ( validate == "sp_playlist" ) {
291+ // get spotify playlist info
292+ const spotifyData = await playdl . spotify ( args ) ;
293+
294+ if ( spotifyData && spotifyData . tracksCount > 0 ) {
295+ const tracks = await spotifyData . all_tracks ( ) ;
296+ if ( tracks ) {
297+ // ask user to wait
298+ const replyMessage = await message . reply ( "Adding playlist to queue, this might take a couple seconds!" ) ;
299+ // add result to queue if data
300+ let length = 0 ;
301+ for ( const i in tracks ) {
302+ const track = tracks [ i ] ;
303+ length += track . durationInSec ;
304+ const YTData = await convertSPDataToYT ( track ) ;
305+ addYTdataToQueue ( message , YTData , true ) ;
306+ }
307+ // mock song data to create embed
308+ const queueData = {
309+ "title" : spotifyData . name ,
310+ "url" : spotifyData . url || spotifyData . link ,
311+ "author" : spotifyData . owner . name ,
312+ "durationInSec" : length ,
313+ "thumbURL" : spotifyData . thumbnail . url ,
314+ "type" : "yt_track" ,
315+ "requester" : message . author ,
316+ "channel" : message . channel ,
317+ "stream_url" : spotifyData . url || spotifyData . link ,
318+ } ;
319+ // remove original reply
320+ replyMessage . delete ( )
321+ . catch ( ( err ) => { console . log ( "[NONFATAL] Failed to delete message" , err ) ; } ) ;
322+ // reply with added to queue message
323+ message . reply ( { embeds :[ songAdded ( queueData , queue [ message . guild . id ] . length - 1 ) ] } )
324+ . then ( msg => {
325+ setTimeout ( ( ) => {
326+ msg . delete ( )
327+ . catch ( ( err ) => { console . log ( "[NONFATAL] Failed to delete message" , err ) ; } ) ;
328+ } , 60000 ) ;
329+ } ) ;
330+ // end of mock
331+ }
332+ }
333+ else {
334+ message . react ( reactions . warning ) ;
335+ return false ;
336+ }
337+
282338 }
283339 else {
284340 message . react ( reactions . confused ) ;
0 commit comments