Advertisement
Guest User

Untitled

a guest
Mar 22nd, 2018
579
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.25 KB | None | 0 0
  1. const { Client, Util } = require('discord.js');
  2. const Discord = require("discord.js");
  3. const YouTube = require('simple-youtube-api');
  4. const ytdl = require('ytdl-core');
  5. const FFMPEG = require('ffmpeg');
  6. const client = new Client({ disableEveryone: true });
  7. const youtube = new YouTube("AIzaSyAdORXg7UZUo7sePv97JyoDqtQVi3Ll0b8");
  8. const queue = new Map();
  9. const PREFIX = '$'
  10.  
  11. client.on('ready', () => {
  12. console.log('Logging into Discord..');
  13. console.log(`I'm Ready Now`);
  14. });
  15.  
  16. client.on('warn', console.warn);
  17. client.on('error', console.error);
  18. client.on('disconnect', () => console.log('I just disconnected, making sure you know, I will reconnect now...'));
  19. client.on('reconnecting', () => console.log('I am reconnecting now!'));
  20.  
  21. /*
  22. ////////////////////////\\\\\\\\\\\\\\\\\\\\\\\\\
  23. ////////////////////////\\\\\\\\\\\\\\\\\\\\\\\\\
  24. ////////////////////////\\\\\\\\\\\\\\\\\\\\\\\\\
  25. ////////////////////////\\\\\\\\\\\\\\\\\\\\\\\\\
  26. */
  27. var servers = [];
  28. var guilds = [];
  29. var queueNames = [];
  30. var isPlaying = false;
  31. var dispatcher = null;
  32. var voiceChannel = null;
  33. var skipReq = 0;
  34. var skippers = [];
  35. var now_playing = [];
  36. /*
  37. \\\\\\\\\\\\\\\\\\\\\\\\/////////////////////////
  38. \\\\\\\\\\\\\\\\\\\\\\\\/////////////////////////
  39. \\\\\\\\\\\\\\\\\\\\\\\\/////////////////////////
  40. \\\\\\\\\\\\\\\\\\\\\\\\/////////////////////////
  41. */
  42. client.on('message', async msg => { // eslint-disable-line
  43. if (msg.author.bot) return undefined;
  44. if (!msg.content.startsWith(PREFIX)) return undefined;
  45.  
  46. const args = msg.content.split(' ');
  47. const searchString = args.slice(1).join(' ');
  48. const url = args[1] ? args[1].replace(/<(.+)>/g, '$1') : '';
  49. const serverQueue = queue.get(msg.guild.id);
  50. let command = msg.content.toLowerCase().split(" ")[0];
  51. command = command.slice(PREFIX.length)
  52.  
  53. if (command === `play`) {
  54. const voiceChannel = msg.member.voiceChannel;
  55. if (!voiceChannel) return msg.channel.send('You must join a voice channel first.');
  56.  
  57. if (url.match(/^https?:\/\/(www.youtube.com|youtube.com)\/playlist(.*)$/)) {
  58. const playlist = await youtube.getPlaylist(url);
  59. const videos = await playlist.getVideos();
  60. for (const video of Object.values(videos)) {
  61. const video2 = await youtube.getVideoByID(video.id); // eslint-disable-line no-await-in-loop
  62. await handleVideo(video2, msg, voiceChannel, true); // eslint-disable-line no-await-in-loop
  63. }
  64. return msg.channel.send(` **${playlist.title}** تم اضافة القائمه!`);
  65. } else {
  66. try {
  67. var video = await youtube.getVideo(url);
  68. } catch (error) {
  69. try {
  70. var videos = await youtube.searchVideos(searchString, 5);
  71. let index = 0;
  72. const embed1 = new Discord.RichEmbed()
  73. .setAuthor(`Music`, `https://cdn.discordapp.com/attachments/404998213347704852/413782918368985109/53ba3a8530c9f645c09e1a49eb3421b2--s-star-design-branding.jpg`)
  74. .setDescription(`**Please select a track with the 1-5 **
  75.  
  76. ${videos.map(video2 => `[**${++index} **] \`${video2.title}\``).join('\n')}`)
  77. .setFooter(`Music`,`https://cdn.discordapp.com/attachments/404998213347704852/413782918368985109/53ba3a8530c9f645c09e1a49eb3421b2--s-star-design-branding.jpg`)
  78. msg.channel.sendEmbed(embed1).then(message =>{message.delete(10000)})
  79. // eslint-disable-next-line max-depth
  80. try {
  81. var response = await msg.channel.awaitMessages(msg2 => msg2.content > 0 && msg2.content < 11, {
  82. maxMatches: 1,
  83. time: 10000,
  84. errors: ['time']
  85. });
  86. } catch (err) {
  87. console.error(err);
  88. return msg.channel.send('Time Out Please try again.');
  89. }
  90. const videoIndex = parseInt(response.first().content);
  91. var video = await youtube.getVideoByID(videos[videoIndex - 1].id);
  92. } catch (err) {
  93. console.error(err);
  94. return msg.channel.send('I could not get any search results.');
  95. }
  96. }
  97. return handleVideo(video, msg, voiceChannel);
  98. }
  99. } else if (command === `skip`) {
  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 that I could skip for you.');
  102. serverQueue.connection.dispatcher.connectoin('Skip command has been used!');
  103. return undefined;
  104. } else if (command === `stop`) {
  105. if (!msg.member.voiceChannel) return msg.channel.send('You are not in a voice channel!');
  106. if (!serverQueue) return msg.channel.send('There is nothing playing that I could stop for you.');
  107. serverQueue.songs = [];
  108. serverQueue.connection.dispatcher.end('Stop command has been used!');
  109. return undefined;
  110. } else if (command === `vol`) {
  111. if (!msg.member.voiceChannel) return msg.channel.send('You are not in a voice channel!');
  112. if (!serverQueue) return msg.channel.send('There is nothing playing.');
  113. if (!args[1]) return msg.channel.send(`:loud_sound: Current volume is **${serverQueue.volume}**`);
  114. serverQueue.volume = args[1];
  115. serverQueue.connection.dispatcher.setVolumeLogarithmic(args[1] / 5);
  116. return msg.channel.send(`:speaker: تم تغير الصوت الي **${args[1]}**`);
  117. } else if (command === `np`) {
  118. if (!serverQueue) return msg.channel.send('nothing playing now.');
  119. const embedNP = new Discord.RichEmbed()
  120. .setAuthor(`Music`, `https://cdn.discordapp.com/attachments/404998213347704852/413782918368985109/53ba3a8530c9f645c09e1a49eb3421b2--s-star-design-branding.jpg`)
  121. .setDescription(`**Now Playing :** **${serverQueue.songs[0].title}**`)
  122. .setFooter(`Music`,`https://cdn.discordapp.com/attachments/404998213347704852/413782918368985109/53ba3a8530c9f645c09e1a49eb3421b2--s-star-design-branding.jpg`)
  123. return msg.channel.sendEmbed(embedNP);
  124. } else if (command === `list`) {
  125. if (!serverQueue) return msg.channel.send('There is nothing playing.');
  126. let index = 0;
  127. const embedqu = new Discord.RichEmbed()
  128. .setAuthor(`Music`, `https://cdn.discordapp.com/attachments/404998213347704852/413782918368985109/53ba3a8530c9f645c09e1a49eb3421b2--s-star-design-branding.jpg`)
  129. .setDescription(`**List Music**
  130.  
  131. ${serverQueue.songs.map(song => `**${++index} -** ${song.title}`).join('\n')}
  132.  
  133. **Now Playing :** ${serverQueue.songs[0].title}`)
  134. .setFooter(`Music`,`https://cdn.discordapp.com/attachments/404998213347704852/413782918368985109/53ba3a8530c9f645c09e1a49eb3421b2--s-star-design-branding.jpg`)
  135. return msg.channel.sendEmbed(embedqu);
  136. } else if (command === `pause`) {
  137. if (serverQueue && serverQueue.playing) {
  138. serverQueue.playing = false;
  139. serverQueue.connection.dispatcher.pause();
  140. return msg.channel.send('Music paused!');
  141. }
  142. return msg.channel.send('There is nothing playing.');
  143. } else if (command === `resume`) {
  144. if (serverQueue && !serverQueue.playing) {
  145. serverQueue.playing = true;
  146. serverQueue.connection.dispatcher.resume();
  147. return msg.channel.send('Music resumed.');
  148. }
  149. return msg.channel.send('nothing playing now.');
  150. }
  151. return undefined;
  152. });
  153. async function handleVideo(video, msg, voiceChannel, playlist = false) {
  154. const serverQueue = queue.get(msg.guild.id);
  155. console.log(video);
  156. const song = {
  157. id: video.id,
  158. title: Util.escapeMarkdown(video.title),
  159. url: `https://www.youtube.com/watch?v=${video.id}`
  160. };
  161. if (!serverQueue) {
  162. const queueConstruct = {
  163. textChannel: msg.channel,
  164. voiceChannel: voiceChannel,
  165. connection: null,
  166. songs: [],
  167. volume: 5,
  168. playing: true
  169. };
  170. queue.set(msg.guild.id, queueConstruct);
  171. queueConstruct.songs.push(song);
  172. try {
  173. var connection = await voiceChannel.join();
  174. queueConstruct.connection = connection;
  175. play(msg.guild, queueConstruct.songs[0]);
  176. } catch (error) {
  177. console.error(`I could not join the voice channel: ${error}`);
  178. queue.delete(msg.guild.id);
  179. return msg.channel.send(`I could not join the voice channel: ${error}`);
  180. }
  181. } else {
  182. serverQueue.songs.push(song);
  183. console.log(serverQueue.songs);
  184. if (playlist) return undefined;
  185. else return msg.channel.send(`Queued : **${song.title}**`);
  186. }
  187. return undefined;
  188. }
  189. function play(guild, song) {
  190. const serverQueue = queue.get(guild.id);
  191. if (!song) {
  192. serverQueue.voiceChannel.leave();
  193. queue.delete(guild.id);
  194. return;
  195. }
  196. console.log(serverQueue.songs);
  197. const dispatcher = serverQueue.connection.playStream(ytdl(song.url))
  198. .on('end', reason => {
  199. if (reason === 'Stream is not generating quickly enough.') console.log('Song ended.');
  200. else console.log(reason);
  201. serverQueue.songs.shift();
  202. play(guild, serverQueue.songs[0]);
  203. })
  204. .on('error', error => console.error(error));
  205. dispatcher.setVolumeLogarithmic(serverQueue.volume / 5);
  206. serverQueue.textChannel.send(`Now playing : **${song.title}**`);
  207. }
  208. client.login("Token");
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement