Advertisement
Guest User

Untitled

a guest
Feb 27th, 2020
149
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.33 KB | None | 0 0
  1. const { Client, Util } = require('discord.js');
  2. const { TOKEN, GOOGLE_API_KEY } = require('./config');
  3. const YouTube = require('simple-youtube-api');
  4. const ytdl = require('ytdl-core');
  5.  
  6. const client = new Client({ disableEveryone: true });
  7.  
  8. const youtube = new YouTube(GOOGLE_API_KEY);
  9.  
  10. const queue = new Map();
  11.  
  12. client.on('warn', console.warn);
  13.  
  14. client.on('error', console.error);
  15.  
  16. client.on('ready', () => console.log('Yo this ready!'));
  17.  
  18. client.on('disconnect', () => console.log('I just disconnected, making sure you know, I will reconnect now...'));
  19.  
  20. client.on('reconnecting', () => console.log('I am reconnecting now!'));
  21.  
  22. client.on('message', async msg => {
  23. con.query(`SELECT * FROM prefix WHERE id = '${message.guild.id}' `, (err, rows) => {
  24. if(err) throw err;
  25. let PEWFIX = rows[0].prefix;
  26. if (msg.author.bot) return undefined;
  27. if (!msg.content.startsWith(PREFIX)) return undefined;
  28.  
  29. const args = msg.content.split(' ');
  30. const searchString = args.slice(1).join(' ');
  31. const url = args[1] ? args[1].replace(/<(.+)>/g, '$1') : '';
  32. const serverQueue = queue.get(msg.guild.id);
  33.  
  34. let command = msg.content.toLowerCase().split(' ')[0];
  35. command = command.slice(PREFIX.length)
  36.  
  37. if (command === 'play') {
  38. const voiceChannel = msg.member.voiceChannel;
  39. if (!voiceChannel) return msg.channel.send('I\'m sorry but you need to be in a voice channel to play music!');
  40. const permissions = voiceChannel.permissionsFor(msg.client.user);
  41. if (!permissions.has('CONNECT')) {
  42. return msg.channel.send('I cannot connect to your voice channel, make sure I have the proper permissions!');
  43. }
  44. if (!permissions.has('SPEAK')) {
  45. return msg.channel.send('I cannot speak in this voice channel, make sure I have the proper permissions!');
  46. }
  47.  
  48. if (url.match(/^https?:\/\/(www.youtube.com|youtube.com)\/playlist(.*)$/)) {
  49. const playlist = await youtube.getPlaylist(url);
  50. const videos = await playlist.getVideos();
  51. for (const video of Object.values(videos)) {
  52. const video2 = await youtube.getVideoByID(video.id); // eslint-disable-line no-await-in-loop
  53. await handleVideo(video2, msg, voiceChannel, true); // eslint-disable-line no-await-in-loop
  54. }
  55. return msg.channel.send(`✅ Playlist: **${playlist.title}** has been added to the queue!`);
  56. } else {
  57. try {
  58. var video = await youtube.getVideo(url);
  59. } catch (error) {
  60. try {
  61. var videos = await youtube.searchVideos(searchString, 10);
  62. let index = 0;
  63. msg.channel.send(`
  64. __**Song selection:**__
  65. ${videos.map(video2 => `**${++index} -** ${video2.title}`).join('\n')}
  66. Please provide a value to select one of the search results ranging from 1-10.
  67. `);
  68. // eslint-disable-next-line max-depth
  69. try {
  70. var response = await msg.channel.awaitMessages(msg2 => msg2.content > 0 && msg2.content < 11, {
  71. maxMatches: 1,
  72. time: 10000,
  73. errors: ['time']
  74. });
  75. } catch (err) {
  76. console.error(err);
  77. return msg.channel.send('No or invalid value entered, cancelling video selection.');
  78. }
  79. const videoIndex = parseInt(response.first().content);
  80. var video = await youtube.getVideoByID(videos[videoIndex - 1].id);
  81. } catch (err) {
  82. console.error(err);
  83. return msg.channel.send('🆘 I could not obtain any search results.');
  84. }
  85. }
  86. return handleVideo(video, msg, voiceChannel);
  87. }
  88. } else if (command === 'skip') {
  89. if (!msg.member.voiceChannel) return msg.channel.send('You are not in a voice channel!');
  90. if (!serverQueue) return msg.channel.send('There is nothing playing that I could skip for you.');
  91. serverQueue.connection.dispatcher.end('Skip command has been used!');
  92. return undefined;
  93. } else if (command === 'stop') {
  94. if (!msg.member.voiceChannel) return msg.channel.send('You are not in a voice channel!');
  95. if (!serverQueue) return msg.channel.send('There is nothing playing that I could stop for you.');
  96. serverQueue.songs = [];
  97. serverQueue.connection.dispatcher.end('Stop command has been used!');
  98. return undefined;
  99. } else if (command === 'volume') {
  100. if (!msg.member.voiceChannel) return msg.channel.send('You are not in a voice channel!');
  101. if (!serverQueue) return msg.channel.send('There is nothing playing.');
  102. if (!args[1]) return msg.channel.send(`The current volume is: **${serverQueue.volume}**`);
  103. serverQueue.volume = args[1];
  104. serverQueue.connection.dispatcher.setVolumeLogarithmic(args[1] / 100);
  105. return msg.channel.send(`I set the volume to: **${args[1]}**`);
  106. } else if (command === 'np') {
  107. if (!serverQueue) return msg.channel.send('There is nothing playing.');
  108. return msg.channel.send(`🎶 Now playing: **${serverQueue.songs[0].title}**`);
  109. } else if (command === 'queue') {
  110. if (!serverQueue) return msg.channel.send('There is nothing playing.');
  111. return msg.channel.send(`
  112. __**Song queue:**__
  113. ${serverQueue.songs.map(song => `**-** ${song.title}`).join('\n')}
  114. **Now playing:** ${serverQueue.songs[0].title}
  115. `);
  116. } else if (command === 'pause') {
  117. if (serverQueue && serverQueue.playing) {
  118. serverQueue.playing = false;
  119. serverQueue.connection.dispatcher.pause();
  120. return msg.channel.send('⏸ Paused the music for you!');
  121. }
  122. return msg.channel.send('There is nothing playing.');
  123. } else if (command === 'resume') {
  124. if (serverQueue && !serverQueue.playing) {
  125. serverQueue.playing = true;
  126. serverQueue.connection.dispatcher.resume();
  127. return msg.channel.send('▶ Resumed the music for you!');
  128. }
  129. return msg.channel.send('There is nothing playing.');
  130. }
  131.  
  132. return undefined;
  133. });
  134.  
  135. async function handleVideo(video, msg, voiceChannel, playlist = false) {
  136. const serverQueue = queue.get(msg.guild.id);
  137. console.log(video);
  138. const song = {
  139. id: video.id,
  140. title: Util.escapeMarkdown(video.title),
  141. url: `https://www.youtube.com/watch?v=${video.id}`
  142. };
  143. if (!serverQueue) {
  144. const queueConstruct = {
  145. textChannel: msg.channel,
  146. voiceChannel: voiceChannel,
  147. connection: null,
  148. songs: [],
  149. volume: 100,
  150. playing: true
  151. };
  152. queue.set(msg.guild.id, queueConstruct);
  153.  
  154. queueConstruct.songs.push(song);
  155.  
  156. try {
  157. var connection = await voiceChannel.join();
  158. queueConstruct.connection = connection;
  159. play(msg.guild, queueConstruct.songs[0]);
  160. } catch (error) {
  161. console.error(`I could not join the voice channel: ${error}`);
  162. queue.delete(msg.guild.id);
  163. return msg.channel.send(`I could not join the voice channel: ${error}`);
  164. }
  165. } else {
  166. serverQueue.songs.push(song);
  167. console.log(serverQueue.songs);
  168. if (playlist) return undefined;
  169. else return msg.channel.send(`✅ **${song.title}** has been added to the queue!`);
  170. }
  171. return undefined;
  172. }
  173.  
  174. function play(guild, song) {
  175. const serverQueue = queue.get(guild.id);
  176.  
  177. if (!song) {
  178. serverQueue.voiceChannel.leave();
  179. queue.delete(guild.id);
  180. return;
  181. }
  182. console.log(serverQueue.songs);
  183.  
  184. const dispatcher = serverQueue.connection.playStream(ytdl(song.url))
  185. .on('end', reason => {
  186. if (reason === 'Stream is not generating quickly enough.') console.log('Song ended.');
  187. else console.log(reason);
  188. serverQueue.songs.shift();
  189. play(guild, serverQueue.songs[0]);
  190. })
  191. .on('error', error => console.error(error));
  192. dispatcher.setVolumeLogarithmic(serverQueue.volume / 100);
  193.  
  194. serverQueue.textChannel.send(`🎶 Start playing: **${song.title}**`);
  195. }
  196. })
  197.  
  198. client.login(TOKEN);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement