Advertisement
Guest User

Untitled

a guest
Apr 22nd, 2019
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 11.44 KB | None | 0 0
  1. const queue = new Map()
  2. const { GOOGLE_API_KEY } = "AIzaSyC_aYZh3asa3I-Pwi4PLhn8y9V6rXIwmJs"
  3. const YouTube = require('simple-youtube-api')
  4. const ytdl = require('ytdl-core')
  5.  
  6. const youtube = new YouTube("AIzaSyC_aYZh3asa3I-Pwi4PLhn8y9V6rXIwmJs")
  7. let client = Koala;
  8. client.on('warn', console.warn)
  9.  
  10. client.on('error', console.error)
  11.  
  12. client.on('ready', () => console.log('Yo this ready!'))
  13.  
  14. client.on('disconnect', () => console.log('I just disconnected, making sure you know, I will reconnect now...'))
  15.  
  16. client.on('reconnecting', () => console.log('I am reconnecting now!'))
  17.  
  18. client.on('message', async msg => { // eslint-disable-line
  19. if (msg.author.bot) return undefined
  20.  
  21. let Prefix = await Quick.fetch(`prefix_${msg.guild.id}`);
  22. if (!Prefix){ Prefix = "k!"; };
  23. if (!msg.content.startsWith(Prefix)) return undefined
  24.  
  25. const args = msg.content.split(' ')
  26. let eColor = msg.guild.me.displayHexColor!=='#000000' ? msg.guild.me.displayHexColor : 0xffffff
  27. const searchString = args.slice(1).join(' ')
  28. const url = args[1] ? args[1].replace(/<(.+)>/g, '$1') : ''
  29. const serverQueue = queue.get(msg.guild.id)
  30.  
  31. let command = msg.content.toLowerCase().split(' ')[0]
  32. command = command.slice(Prefix.length)
  33.  
  34. if (command === 'play') {
  35. const voiceChannel = msg.member.voiceChannel;
  36. if (!voiceChannel) return msg.channel.send('I\'m sorry but you need to be in a voice channel to play music!');
  37. const permissions = voiceChannel.permissionsFor(msg.client.user);
  38. if (!permissions.has('CONNECT')) {
  39. return msg.channel.send('I cannot connect to your voice channel, make sure I have the proper permissions!');
  40. }
  41. if (!permissions.has('SPEAK')) {
  42. return msg.channel.send('I cannot speak in this voice channel, make sure I have the proper permissions!');
  43. }
  44.  
  45. if (url.match(/^https?:\/\/(www.youtube.com|youtube.com)\/playlist(.*)$/)) {
  46. const playlist = await youtube.getPlaylist(url);
  47. const videos = await playlist.getVideos();
  48. for (const video of Object.values(videos)) {
  49. const video2 = await youtube.getVideoByID(video.id); // eslint-disable-line no-await-in-loop
  50. await handleVideo(video2, msg, voiceChannel, true); // eslint-disable-line no-await-in-loop
  51. }
  52. return msg.channel.send({embed: new Discord.RichEmbed()
  53. .setAuthor(`You Requested for Music, ` + msg.author.tag,msg.author.avatarURL)
  54. .setDescription(`:notes: **PlayList Added:**
  55. **»** ${playlist.title}`)
  56. .setColor(eColor)
  57.  
  58. });
  59. } else {
  60. if(!searchString) {
  61. msg.channel.send({embed: new Discord.RichEmbed()
  62. .setAuthor(`You Requested for Music, ` + msg.author.tag,msg.author.avatarURL)
  63. .setDescription(`**Usage:** ${Prefix}play <search>
  64. If you want to listen to music, you have
  65. to put a search string ahead! That's all.`)
  66. .setColor(eColor)
  67.  
  68. });
  69. } else {
  70. try {
  71. var video = await youtube.getVideo(url);
  72. } catch (error) {
  73. try {
  74. var videos = await youtube.searchVideos(searchString, 5);
  75. let index = 0;
  76. msg.channel.send({embed: new Discord.RichEmbed()
  77. .setAuthor(`You Requested for Music, ` + msg.author.tag,msg.author.avatarURL)
  78. .setDescription(`__**Youtube Search Result**__
  79. ${videos.map(video2 => `**${++index}.** ${video2.title}`).join(`\n`)}
  80. To select a song, type any number from \`1 - 5\` to choose song!
  81. The search is cancelled in \`10 seconds\` if no number provided.`)
  82. .setColor(eColor)
  83.  
  84. });
  85. try {
  86. var response = await msg.channel.awaitMessages(msg2 => msg2.content > 0 && msg2.content < 6, {
  87. maxMatches: 1,
  88. time: 10000,
  89. errors: ['time']
  90. });
  91. } catch (err) {
  92. console.error(err);
  93. return msg.channel.send('Invalid numbers inserted or no received numbers. I\'m Cancelling your Search.');
  94. }
  95. // var response = 1;
  96. // const videoIndex = parseInt(response.first().content);
  97. var video = await youtube.getVideoByID(videos[0].id);
  98. } catch (err) {
  99. console.error(err);
  100. return msg.channel.send('Yo! I could not find any results!');
  101. }
  102. }
  103. return handleVideo(video, msg, voiceChannel)
  104. }
  105. }
  106. } else if (command === 'skip') {
  107. if (!msg.member.voiceChannel) return msg.channel.send(':red_circle: **Not in a voice channel, I am talking to you**');
  108. if (!serverQueue) return msg.channel.send(':mailbox_with_no_mail: **I can\'t skip an empty queue**');
  109. serverQueue.connection.dispatcher.end('Skip command has been used!');
  110. return undefined;
  111. } else if (command === 'stop') {
  112. if (!msg.member.voiceChannel) return msg.channel.send(':red_circle: **Not in a voice channel, I am talking to you**');
  113. if (!serverQueue) return msg.channel.send(':mailbox_with_no_mail: **Nothing to stop, because there is no music!**');
  114. serverQueue.songs = [];
  115. serverQueue.connection.dispatcher.end('Stop command has been used!');
  116. msg.channel.send({embed: new Discord.RichEmbed()
  117. .setAuthor(msg.author.tag,msg.author.avatarURL)
  118. .setDescription(`The player has been stopped.`)
  119. .setColor(eColor)
  120. })
  121. return undefined;
  122. } else if (command === 'volume') {
  123. if (!msg.member.voiceChannel) return msg.channel.send('You are not in a voice channel!');
  124. if (!serverQueue) return msg.channel.send('There is nothing playing.');
  125. if (!args[1]) return msg.channel.send(`The current volume is: **${serverQueue.volume}**`);
  126. serverQueue.volume = args[1];
  127. serverQueue.connection.dispatcher.setVolumeLogarithmic(args[1] / 5);
  128. return msg.channel.send(`I set the volume to: **${args[1]}**`);
  129.  
  130. } else if (command === 'np' || command === 'nowplaying') {
  131. if (!serverQueue) return msg.channel.send(':mailbox_with_no_mail: **Wait, There is no music playing!**');
  132. return msg.channel.send({embed: new Discord.RichEmbed()
  133. .setAuthor(msg.author.tag,msg.author.avatarURL)
  134. .setDescription(`:notes: **Currently Playing:**\n${serverQueue.songs[0].title}`)
  135. .setColor(eColor)
  136. .setThumbnail(`https://img.youtube.com/vi/${serverQueue.songs[0].id}/mqdefault.jpg`)
  137.  
  138. .setTimestamp(new Date())
  139. })
  140. //msg.channel.send(`Yo yo! I'm playing :notes: ,**${serverQueue.songs[0].title}**, :notes: currently!`);
  141. } else if (command === 'queue' || command === `q`) {
  142. if (!serverQueue) return msg.channel.send(':mailbox_with_no_mail: **What? Nothing is playing at all?**');
  143. return msg.channel.send({embed: new Discord.RichEmbed()
  144. .setAuthor(msg.author.tag,msg.author.avatarURL)
  145. .setDescription(`:notes: **Song Current Queue:**\n${serverQueue.songs.map(song => `**»** ${song.title}`).join('\n')}`)
  146. .setColor(eColor)
  147.  
  148. .setTimestamp(new Date())
  149. })
  150.  
  151. msg.channel.send(`
  152. __**Song queue:**__
  153. ${serverQueue.songs.map(song => `**-** ${song.title}`).join('\n')}
  154. **Now playing:**
  155. :notes: ${serverQueue.songs[0].title} :notes:
  156. `);
  157.  
  158.  
  159.  
  160.  
  161. } else if (command === 'pause') {
  162. if (serverQueue && serverQueue.playing) {
  163. serverQueue.playing = false;
  164. serverQueue.connection.dispatcher.pause();
  165. return msg.channel.send(':pause_button: **The player has been Paused**');
  166. }
  167. return msg.channel.send(':mailbox_with_no_mail: **This DJ don\`t know how to pause empty song!**');
  168. } else if (command === 'resume') {
  169. if (serverQueue && !serverQueue.playing) {
  170. serverQueue.playing = true;
  171. serverQueue.connection.dispatcher.resume();
  172. return msg.channel.send(':play_pause: **The player has been resumed**');
  173. }
  174. return msg.channel.send(':mailbox_with_no_mail: **This DJ don\'t know how to resume an empty list!**');
  175.  
  176. }
  177.  
  178. return undefined;
  179. });
  180.  
  181. async function handleVideo(video, msg, voiceChannel, playlist = false) {
  182. let eColor = msg.guild.me.displayHexColor!=='#000000' ? msg.guild.me.displayHexColor : 0xffffff
  183. const serverQueue = queue.get(msg.guild.id);
  184. console.log(video);
  185. const song = {
  186. id: video.id,
  187. title: Discord.escapeMarkdown(video.title),
  188. url: `https://www.youtube.com/watch?v=${video.id}`
  189. };
  190. if (!serverQueue) {
  191. const queueConstruct = {
  192. textChannel: msg.channel,
  193. voiceChannel: voiceChannel,
  194. connection: null,
  195. songs: [],
  196. volume: 5,
  197. playing: true
  198. };
  199. queue.set(msg.guild.id, queueConstruct);
  200.  
  201. queueConstruct.songs.push(song)
  202. msg.channel.send({embed: new Discord.RichEmbed()
  203. .setAuthor(msg.author.tag,msg.author.avatarURL)
  204. .setDescription(`:notes: **Now Playing:**\n${video.title}`)
  205. .setTimestamp(new Date())
  206.  
  207. .setThumbnail(`https://img.youtube.com/vi/${video.id}/mqdefault.jpg`)
  208. .setColor(eColor)
  209. });
  210.  
  211. try {
  212. var connection = await voiceChannel.join();
  213. queueConstruct.connection = connection;
  214. play(msg.guild, queueConstruct.songs[0]);
  215. } catch (error) {
  216. console.error(`I could not join the voice channel: ${error}`);
  217. queue.delete(msg.guild.id);
  218. return msg.channel.send(`I could not join the voice channel: ${error}`);
  219. }
  220. } else {
  221. serverQueue.songs.push(song);
  222. console.log(serverQueue.songs);
  223. if (playlist) return undefined;
  224. else return msg.channel.send({embed: new Discord.RichEmbed()
  225. .setAuthor(msg.author.tag,msg.author.avatarURL)
  226. .setDescription(`:notes: **Added Song:**\n${video.title}`)
  227. .setTimestamp(new Date())
  228. .setThumbnail(`https://img.youtube.com/vi/${video.id}/mqdefault.jpg`)
  229.  
  230. .setColor(eColor)
  231. })
  232. }
  233. return undefined;
  234. }
  235.  
  236. function play(guild, song) {
  237. const serverQueue = queue.get(guild.id);
  238.  
  239. if (!song) {
  240. serverQueue.voiceChannel.leave();
  241. queue.delete(guild.id);
  242. return;
  243. }
  244. console.log(serverQueue.songs);
  245.  
  246. const dispatcher = serverQueue.connection.playStream(ytdl(song.url))
  247. .on('end', reason => {
  248. if (reason === 'Stream is not generating quickly enough.') console.log('Song ended.');
  249. else console.log(reason);
  250. serverQueue.songs.shift();
  251. setTimeout(() => {
  252. play(guild, serverQueue.songs[0]);
  253. }, 250);
  254. })
  255. .on('error', error => console.error(error));
  256. dispatcher.setVolumeLogarithmic(serverQueue.volume / 5);
  257. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement