Advertisement
danyooooo

playlist with context

Jan 1st, 2022
19
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.48 KB | None | 0 0
  1.  
  2. const search = args.join(" ");
  3.  
  4. // the pattern inside the link
  5. const pattern = /^.*(youtu.be\/|list=)([^#\&\?]*).*/gi;
  6. const url = args[0];
  7. const urlValid = pattern.test(args[0]);
  8.  
  9. let playlist = null;
  10. let videos = [];
  11.  
  12. // converter
  13. if (urlValid) { / /if its a valid url ..
  14. try {
  15. // trying to get infos about the playlist first
  16. playlist = await youtube.getPlaylist(url, { part: "snippet" });
  17. videos = await playlist.getVideos(MAX_PLAYLIST_SIZE || 50, { part: "snippet" });
  18. } catch (error) {
  19. console.error(error);
  20. return message.reply(i18n.__("playlist.errorNotFoundPlaylist")).catch(console.error);
  21. }
  22.  
  23. // if it cant extract the songs from the playlist, it will only search the first song in the playlist
  24. } else {
  25. try {
  26. const results = await youtube.searchPlaylists(search, 1, { part: "id" });
  27. playlist = results[0];
  28. videos = await playlist.getVideos(MAX_PLAYLIST_SIZE, { part: "snippet" });
  29. } catch (error) {
  30. console.error(error);
  31. return message.reply(error.message).catch(console.error);
  32. }
  33. }
  34.  
  35. // some filtering checks
  36. const newSongs = videos
  37. .filter((video) => video.title != "Private video" && video.title != "Deleted video")
  38. .map((video) => {
  39. return (song = {
  40. title: video.title,
  41. url: video.url,
  42. duration: video.durationSeconds
  43. });
  44. });
  45.  
  46. // push the playlist into the queue, change the serverQueue and queueContruct to your code design
  47. serverQueue ? serverQueue.songs.push(...newSongs) : queueConstruct.songs.push(...newSongs);
  48.  
  49. // playing first song message, just replace all of this with the message.channel.send() if you dont want embed
  50. let playlistEmbed = new MessageEmbed()
  51. .setTitle(`${playlist.title}`)
  52. .setDescription(newSongs.map((song, index) => `${index + 1}. ${song.title}`))
  53. .setURL(playlist.url)
  54. .setColor("#00FF22")
  55. .setTimestamp();
  56.  
  57.  
  58. if (playlistEmbed.description.length >= 2048)
  59. playlistEmbed.description =
  60. playlistEmbed.description.substr(0, 2007) + i18n.__("playlist.playlistCharLimit");
  61.  
  62. // it sends the entire playlist with all the song name that it can extract, replace it with other response rather than using mine
  63. message.channel.send(i18n.__mf("playlist.startedPlaylist", { author: message.author }), playlistEmbed);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement