You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

117 lines
4.7 KiB

  1. const { google } = require('googleapis');
  2. const youtube = google.youtube('v3');
  3. const secrets = require('./secrets.json');
  4. const ytdl = require("ytdl-core");
  5. module.exports = {
  6. name: 'playlist',
  7. description: 'Add playlist to the queue.',
  8. roles: ['732550362199752764', '732345527143759943'],
  9. async execute(message) {
  10. var message = message;
  11. const args = message.content.split(" ");
  12. try{
  13. let playlist_id = args[1].match(/https:\/\/www\.youtube\.com\/watch\?.*list=(.{34}).*/)[1]
  14. youtube.playlistItems.list({
  15. key: secrets.web.api_key,
  16. part: 'snippet, contentDetails',
  17. playlistId: playlist_id,
  18. maxResults: 50,
  19. }, async (err, results) => {
  20. try {
  21. const queue = message.client.queue;
  22. const serverQueue = message.client.queue.get(message.guild.id);
  23. const voiceChannel = message.member.voice.channel;
  24. if (!voiceChannel)
  25. return message.channel.send(
  26. "You need to be in a voice channel to play music!"
  27. );
  28. const permissions = voiceChannel.permissionsFor(message.client.user);
  29. if (!permissions.has("CONNECT") || !permissions.has("SPEAK")) {
  30. return message.channel.send(
  31. "I need the permissions to join and speak in your voice channel!"
  32. );
  33. }
  34. if (!serverQueue) {
  35. const queueContruct = {
  36. textChannel: message.channel,
  37. voiceChannel: voiceChannel,
  38. connection: null,
  39. songs: [],
  40. volume: 5,
  41. playing: true
  42. };
  43. queue.set(message.guild.id, queueContruct);
  44. results.data.items.forEach(function(item, index){
  45. url = "https://www.youtube.com/watch?v=" + item.contentDetails.videoId
  46. let song = {
  47. title: item.snippet.title,
  48. url: url
  49. };
  50. queueContruct.songs.push(song);
  51. });
  52. try {
  53. var connection = await voiceChannel.join();
  54. queueContruct.connection = connection;
  55. this.play(message, queueContruct.songs[0]);
  56. } catch (err) {
  57. console.log(err);
  58. queue.delete(message.guild.id);
  59. return message.channel.send(err);
  60. }
  61. return message.channel.send(
  62. `Playing the first song from the playlist!`
  63. );
  64. } else {
  65. results.data.items.forEach(function(item, index){
  66. url = "https://www.youtube.com/watch?v=" + item.contentDetails.videoId
  67. let song = {
  68. title: item.snippet.title,
  69. url: url
  70. };
  71. serverQueue.songs.push(song);
  72. });
  73. return message.channel.send(
  74. `Added songs from the playlist to the queue!`
  75. );
  76. }
  77. } catch (error) {
  78. console.log(error);
  79. message.channel.send("Sorry, that did not work for some mysterious reason :(");
  80. }
  81. });
  82. } catch (error) {
  83. console.log(error);
  84. message.channel.send("Sorry, that did not work for some mysterious reason :(");
  85. }
  86. },
  87. play(message, song) {
  88. const queue = message.client.queue;
  89. const guild = message.guild;
  90. const serverQueue = queue.get(message.guild.id);
  91. if (!song) {
  92. serverQueue.voiceChannel.leave();
  93. queue.delete(guild.id);
  94. return;
  95. }
  96. const dispatcher = serverQueue.connection
  97. .play(ytdl(song.url, { filter: 'audioonly', highWaterMark: 1024 * 1024 * 10 }))
  98. .on("finish", () => {
  99. serverQueue.songs.shift();
  100. this.play(message, serverQueue.songs[0]);
  101. })
  102. .on("error", error => console.error(error));
  103. dispatcher.setVolumeLogarithmic(serverQueue.volume / 5);
  104. serverQueue.textChannel.send(`Start playing: **${song.title}**`);
  105. },
  106. };