1
1
const { SlashCommandBuilder } = require ( '@discordjs/builders' ) ;
2
2
const { MessageSelectMenu, MessageActionRow } = require ( 'discord.js' ) ;
3
3
const Player = require ( '../../utils/music/Player' ) ;
4
- const Youtube = require ( 'simple-youtube-api' ) ;
5
- const ytsr = require ( 'ytsr' ) ;
6
4
const { getData } = require ( 'spotify-url-info' ) ;
7
- const { youtubeAPI } = require ( '../../config.json' ) ;
5
+ const YouTube = require ( 'youtube-sr' ) . default ;
8
6
let {
9
7
playLiveStreams,
10
8
playVideosLongerThan1Hour,
@@ -23,7 +21,6 @@ const {
23
21
} = require ( '@discordjs/voice' ) ;
24
22
const createGuildData = require ( '../../utils/createGuildData' ) ;
25
23
26
- const youtube = new Youtube ( youtubeAPI ) ;
27
24
// Check If Options are Valid
28
25
if ( typeof playLiveStreams !== 'boolean' ) playLiveStreams = true ;
29
26
if ( typeof maxQueueLength !== 'number' || maxQueueLength < 1 ) {
@@ -404,17 +401,17 @@ module.exports = {
404
401
const artistsAndName = concatSongNameAndArtists (
405
402
spotifyPlaylistItems [ i ] . track
406
403
) ;
407
- const ytResult = await ytsr ( artistsAndName , { limit : 1 } ) ;
404
+ const ytResult = await YouTube . search ( artistsAndName , {
405
+ limit : 1
406
+ } ) ;
408
407
const video = {
409
- title : ytResult . items [ 0 ] . title ,
410
- url : ytResult . items [ 0 ] . url ,
411
- thumbnails : {
412
- high : {
413
- url : `https://i.ytimg.com/vi/${ ytResult . items [ 0 ] . id } /hqdefault.jpg`
414
- }
408
+ title : ytResult [ 0 ] . title ,
409
+ url : ytResult [ 0 ] . url ,
410
+ thumbnail : {
411
+ url : video . thumbnail . url
415
412
} ,
416
- // the true value is used to differentiate this duration from the rawDuration recieved from the YT API
417
- duration : [ ytResult . items [ 0 ] . duration , true ]
413
+ durationFormatted : ytResult [ 0 ] . durationFormatted ,
414
+ duration : ytResult [ 0 ] . duration
418
415
} ;
419
416
if ( nextFlag || jumpFlag ) {
420
417
flagLogic ( interaction , video , jumpFlag ) ;
@@ -439,17 +436,15 @@ module.exports = {
439
436
else {
440
437
const artistsAndName = concatSongNameAndArtists ( data ) ;
441
438
// Search on YT
442
- const ytResult = await ytsr ( artistsAndName , { limit : 1 } ) ;
439
+ const ytResult = await YouTube . search ( artistsAndName , { limit : 1 } ) ;
443
440
const video = {
444
- title : ytResult . items [ 0 ] . title ,
445
- url : ytResult . items [ 0 ] . url ,
446
- thumbnails : {
447
- high : {
448
- url : `https://i.ytimg.com/vi/${ ytResult . items [ 0 ] . id } /hqdefault.jpg`
449
- }
441
+ title : ytResult [ 0 ] . title ,
442
+ url : `https://www.youtube.com/watch?v=${ ytResult [ 0 ] . id } ` ,
443
+ thumbnail : {
444
+ url : ytResult [ 0 ] . thumbnail . url
450
445
} ,
451
- // the true value is used to differentiate this duration from the rawDuration recieved from the YT API
452
- duration : [ ytResult . items [ 0 ] . duration , true ]
446
+ durationFormatted : ytResult [ 0 ] . durationFormatted ,
447
+ duration : ytResult [ 0 ] . duration
453
448
} ;
454
449
if ( nextFlag || jumpFlag ) {
455
450
flagLogic ( interaction , video , jumpFlag ) ;
@@ -477,21 +472,22 @@ module.exports = {
477
472
}
478
473
479
474
if ( isYouTubePlaylistURL ( query ) ) {
480
- const playlist = await youtube . getPlaylist ( query ) ;
475
+ const playlist = await YouTube . getPlaylist ( query ) ;
481
476
if ( ! playlist ) {
482
477
deletePlayerIfNeeded ( interaction ) ;
483
478
return interaction . followUp (
484
479
':x: Playlist is either private or it does not exist!'
485
480
) ;
486
481
}
487
482
488
- let videosArr = await playlist . getVideos ( ) ;
483
+ let videosArr = await playlist . fetch ( ) ;
489
484
if ( ! videosArr ) {
490
485
deletePlayerIfNeeded ( interaction ) ;
491
486
return interaction . followUp (
492
487
":x: I hit a problem when trying to fetch the playlist's videos"
493
488
) ;
494
489
}
490
+ videosArr = videosArr . videos ;
495
491
496
492
if ( AutomaticallyShuffleYouTubePlaylists || shuffleFlag ) {
497
493
videosArr = shuffleArray ( videosArr ) ;
@@ -510,42 +506,32 @@ module.exports = {
510
506
//variable to know how many songs were skipped because of privacyStatus
511
507
var skipAmount = 0 ;
512
508
513
- await videosArr . reduce ( async ( memo , video , key ) => {
514
- await memo ;
509
+ await videosArr . reduce ( async ( __ , video , key ) => {
515
510
// don't process private videos
516
- if (
517
- video . raw . status . privacyStatus == 'private' ||
518
- video . raw . status . privacyStatus == 'privacyStatusUnspecified'
519
- ) {
511
+ if ( video . private ) {
520
512
skipAmount ++ ;
521
513
return ;
522
514
}
523
-
524
- try {
525
- const fetchedVideo = await video . fetch ( ) ;
526
- if ( nextFlag || jumpFlag ) {
527
- player . queue . splice (
528
- key - skipAmount ,
529
- 0 ,
530
- constructSongObj (
531
- fetchedVideo ,
532
- interaction . member . voice . channel ,
533
- interaction . member . user
534
- )
535
- ) ;
536
- } else {
537
- player . queue . push (
538
- constructSongObj (
539
- fetchedVideo ,
540
- interaction . member . voice . channel ,
541
- interaction . member . user
542
- )
543
- ) ;
544
- }
545
- } catch ( err ) {
546
- return console . error ( err ) ;
515
+ if ( nextFlag || jumpFlag ) {
516
+ player . queue . splice (
517
+ key - skipAmount ,
518
+ 0 ,
519
+ constructSongObj (
520
+ video ,
521
+ interaction . member . voice . channel ,
522
+ interaction . member . user
523
+ )
524
+ ) ;
525
+ } else {
526
+ player . queue . push (
527
+ constructSongObj (
528
+ video ,
529
+ interaction . member . voice . channel ,
530
+ interaction . member . user
531
+ )
532
+ ) ;
547
533
}
548
- } , undefined ) ;
534
+ } ) ;
549
535
if (
550
536
jumpFlag &&
551
537
player . audioPlayer . state . status === AudioPlayerStatus . Playing
@@ -560,15 +546,15 @@ module.exports = {
560
546
// interactiveEmbed(interaction)
561
547
// .addField('Added Playlist', `[${playlist.title}](${playlist.url})`)
562
548
// .build();
563
- return ;
549
+ return interaction . followUp ( 'Added playlist to queue!' ) ;
564
550
}
565
551
}
566
552
567
553
if ( isYouTubeVideoURL ( query ) ) {
568
- const id = query
569
- . replace ( / ( > | < ) / gi, '' )
570
- . split ( / ( v i \/ | v = | \/ v \/ | y o u t u \. b e \/ | \/ e m b e d \/ ) / ) [ 2 ]
571
- . split ( / [ ^ 0 - 9 a - z _ \- ] / i) [ 0 ] ;
554
+ // const id = query
555
+ // .replace(/(>|<)/gi, '')
556
+ // .split(/(vi\/|v=|\/v\/|youtu\.be\/|\/embed\/)/)[2]
557
+ // .split(/[^0-9a-z_\-]/i)[0];
572
558
573
559
const timestampRegex = / t = ( [ ^ # & \n \r ] + ) / g;
574
560
let timestamp = timestampRegex . exec ( query ) ;
@@ -583,18 +569,15 @@ module.exports = {
583
569
}
584
570
timestamp = Number ( timestamp ) ;
585
571
586
- const video = await youtube . getVideoByID ( id ) . catch ( function ( ) {
572
+ const video = await YouTube . getVideo ( query ) . catch ( function ( ) {
587
573
deletePlayerIfNeeded ( interaction ) ;
588
574
interaction . followUp (
589
575
':x: There was a problem getting the video you provided!'
590
576
) ;
591
577
} ) ;
592
578
if ( ! video ) return ;
593
-
594
- if (
595
- video . raw . snippet . liveBroadcastContent === 'live' &&
596
- ! playLiveStreams
597
- ) {
579
+ console . log ( video ) ;
580
+ if ( video . live === 'live' && ! playLiveStreams ) {
598
581
deletePlayerIfNeeded ( interaction ) ;
599
582
interaction . followUp (
600
583
'Live streams are disabled in this server! Contact the owner'
@@ -713,11 +696,13 @@ var searchYoutube = async (
713
696
nextFlag ,
714
697
jumpFlag
715
698
) => {
716
- const videos = await youtube . searchVideos ( query , 5 ) . catch ( async function ( ) {
717
- return interaction . followUp (
718
- ':x: There was a problem searching the video you requested!'
719
- ) ;
720
- } ) ;
699
+ const videos = await YouTube . search ( query , { limit : 5 } ) . catch (
700
+ async function ( ) {
701
+ return interaction . followUp (
702
+ ':x: There was a problem searching the video you requested!'
703
+ ) ;
704
+ }
705
+ ) ;
721
706
if ( ! videos ) {
722
707
player . commandLock = false ;
723
708
return interaction . followUp (
@@ -768,13 +753,11 @@ var searchYoutube = async (
768
753
}
769
754
const videoIndex = parseInt ( value ) ;
770
755
771
- youtube
772
- . getVideoByID ( videos [ videoIndex - 1 ] . id )
756
+ YouTube . getVideo (
757
+ `https://www.youtube.com/watch?v=${ videos [ videoIndex - 1 ] . id } `
758
+ )
773
759
. then ( function ( video ) {
774
- if (
775
- video . raw . snippet . liveBroadcastContent === 'live' &&
776
- ! playLiveStreams
777
- ) {
760
+ if ( video . live && ! playLiveStreams ) {
778
761
if ( playOptions ) {
779
762
playOptions . delete ( ) . catch ( console . error ) ;
780
763
return ;
@@ -912,20 +895,6 @@ var shuffleArray = arr => {
912
895
return arr ;
913
896
} ;
914
897
915
- // timeString = timeObj => 'HH:MM:SS' // if HH is missing > MM:SS
916
- var timeString = timeObj => {
917
- if ( timeObj [ 1 ] === true ) return timeObj [ 0 ] ;
918
- return `${ timeObj . hours ? timeObj . hours + ':' : '' } ${
919
- timeObj . minutes ? timeObj . minutes : '00'
920
- } :${
921
- timeObj . seconds < 10
922
- ? '0' + timeObj . seconds
923
- : timeObj . seconds
924
- ? timeObj . seconds
925
- : '00'
926
- } `;
927
- } ;
928
-
929
898
// var millisecondsToTimeObj = ms => ({
930
899
// seconds: Math.floor((ms / 1000) % 60),
931
900
// minutes: Math.floor((ms / (1000 * 60)) % 60),
@@ -944,20 +913,16 @@ var concatSongNameAndArtists = data => {
944
913
} ;
945
914
946
915
var constructSongObj = ( video , voiceChannel , user , timestamp ) => {
947
- let duration = timeString ( video . duration ) ;
916
+ let duration = video . durationFormatted ;
948
917
if ( duration === '00:00' ) duration = 'Live Stream' ;
949
918
// checks if the user searched for a song using a Spotify URL
950
- let url =
951
- video . duration [ 1 ] == true
952
- ? video . url
953
- : `https://www.youtube.com/watch?v=${ video . raw . id } ` ;
954
919
return {
955
- url,
920
+ url : video . url ,
956
921
title : video . title ,
957
922
rawDuration : video . duration ,
958
923
duration,
959
924
timestamp,
960
- thumbnail : video . thumbnails . high . url ,
925
+ thumbnail : video . thumbnail . url ,
961
926
voiceChannel,
962
927
memberDisplayName : user . username ,
963
928
memberAvatar : user . avatarURL ( 'webp' , false , 16 )
0 commit comments