Advertisement
X_KillerYT

music

Dec 24th, 2018
252
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 12.39 KB | None | 0 0
  1. const Discord = require('discord.js');
  2.  
  3. const Util = require('discord.js');
  4.  
  5. const getYoutubeID = require('get-youtube-id');
  6.  
  7. const fetchVideoInfo = require('youtube-info');
  8.  
  9. const YouTube = require('simple-youtube-api');
  10.  
  11. const youtube = new YouTube("AIzaSyAdORXg7UZUo7sePv97JyoDqtQVi3Ll0b8");
  12.  
  13. const queue = new Map();
  14.  
  15. const ytdl = require('ytdl-core');
  16.  
  17. const fs = require('fs');
  18.  
  19. const gif = require("gif-search");
  20.  
  21. const client = new Discord.Client({disableEveryone: true});
  22.  
  23. const prefix = "#";
  24. /////////////////////////
  25. ////////////////////////
  26.  
  27. client.on('message', async msg =>{
  28. if (msg.author.bot) return undefined;
  29. if (!msg.content.startsWith(prefix)) return undefined;
  30.  
  31. let args = msg.content.split(' ');
  32.  
  33. let command = msg.content.toLowerCase().split(" ")[0];
  34. command = command.slice(prefix.length)
  35.  
  36. if(command === `ping`) {
  37. let embed = new Discord.RichEmbed()
  38. .setColor(3447003)
  39. .setTitle("Pong!!")
  40. .setDescription(`${client.ping} ms,`)
  41. .setFooter(`Requested by | ${msg.author.tag}`);
  42. msg.delete().catch(O_o=>{})
  43. msg.channel.send(embed);
  44. }
  45. });
  46. /////////////////////////
  47. ////////////////////////
  48. //////////////////////
  49. client.on('message', async msg =>{
  50. if (msg.author.bot) return undefined;
  51. if (!msg.content.startsWith(prefix)) return undefined;
  52.  
  53. let args = msg.content.split(' ');
  54.  
  55. let command = msg.content.toLowerCase().split(" ")[0];
  56. command = command.slice(prefix.length)
  57.  
  58. if(command === `avatar`){
  59. if(msg.channel.type === 'dm') return msg.channel.send("Nope Nope!! u can't use avatar command in DMs (:")
  60. let mentions = msg.mentions.members.first()
  61. if(!mentions) {
  62. let sicon = msg.author.avatarURL
  63. let embed = new Discord.RichEmbed()
  64. .setImage(msg.author.avatarURL)
  65. .setColor("#5074b3")
  66. msg.channel.send({embed})
  67. } else {
  68. let sicon = mentions.user.avatarURL
  69. let embed = new Discord.RichEmbed()
  70. .setColor("#5074b3")
  71. .setImage(sicon)
  72. msg.channel.send({embed})
  73. }
  74. };
  75. });
  76. /////////////////////////
  77. ////////////////////////
  78. //////////////////////
  79. /////////////////////////
  80. ////////////////////////
  81. //////////////////////
  82.  
  83. /////////////////////////
  84. ////////////////////////
  85. //////////////////////
  86. /////////////////////////
  87. ////////////////////////
  88. //////////////////////
  89. client.on('message', async msg => {
  90. if (msg.author.bot) return undefined;
  91. if (!msg.content.startsWith(prefix)) return undefined;
  92.  
  93. const args = msg.content.split(' ');
  94. const searchString = args.slice(1).join(' ');
  95.  
  96. const url = args[1] ? args[1].replace(/<(.+)>/g, '$1') : '';
  97. const serverQueue = queue.get(msg.guild.id);
  98.  
  99. let command = msg.content.toLowerCase().split(" ")[0];
  100. command = command.slice(prefix.length)
  101.  
  102. if (command === `play`) {
  103. const voiceChannel = msg.member.voiceChannel;
  104.  
  105. if (!voiceChannel) return msg.channel.send("I can't find you in any voice channel!");
  106.  
  107. const permissions = voiceChannel.permissionsFor(msg.client.user);
  108.  
  109. if (!permissions.has('CONNECT')) {
  110.  
  111. return msg.channel.send("I don't have enough permissions to join your voice channel!");
  112. }
  113.  
  114. if (!permissions.has('SPEAK')) {
  115.  
  116. return msg.channel.send("I don't have enough permissions to speak in your voice channel!");
  117. }
  118.  
  119. if (!permissions.has('EMBED_LINKS')) {
  120.  
  121. return msg.channel.sendMessage("I don't have enough permissions to insert a URLs!")
  122. }
  123.  
  124. if (url.match(/^https?:\/\/(www.youtube.com|youtube.com)\/playlist(.*)$/)) {
  125.  
  126. const playlist = await youtube.getPlaylist(url);
  127. const videos = await playlist.getVideos();
  128.  
  129.  
  130. for (const video of Object.values(videos)) {
  131.  
  132. const video2 = await youtube.getVideoByID(video.id);
  133. await handleVideo(video2, msg, voiceChannel, true);
  134. }
  135. return msg.channel.send(`**${playlist.title}**, Just added to the queue!`);
  136. } else {
  137.  
  138. try {
  139.  
  140. var video = await youtube.getVideo(url);
  141.  
  142. } catch (error) {
  143. try {
  144.  
  145. var videos = await youtube.searchVideos(searchString, 5);
  146. let index = 0;
  147. const embed1 = new Discord.RichEmbed()
  148. .setTitle(":mag_right: YouTube Search Results :")
  149. .setDescription(`
  150. ${videos.map(video2 => `${++index}. **${video2.title}**`).join('\n')}`)
  151.  
  152. .setColor("#f7abab")
  153. msg.channel.sendEmbed(embed1).then(message =>{message.delete(20000)})
  154.  
  155. /////////////////
  156. try {
  157.  
  158. var response = await msg.channel.awaitMessages(msg2 => msg2.content > 0 && msg2.content < 11, {
  159. maxMatches: 1,
  160. time: 15000,
  161. errors: ['time']
  162. });
  163. } catch (err) {
  164. console.error(err);
  165. return msg.channel.send('No one respone a number!!');
  166. }
  167.  
  168. const videoIndex = parseInt(response.first().content);
  169. var video = await youtube.getVideoByID(videos[videoIndex - 1].id);
  170.  
  171. } catch (err) {
  172.  
  173. console.error(err);
  174. return msg.channel.send("I didn't find any results!");
  175. }
  176. }
  177.  
  178. return handleVideo(video, msg, voiceChannel);
  179.  
  180. }
  181.  
  182. } else if (command === `skip`) {
  183.  
  184. if (!msg.member.voiceChannel) return msg.channel.send("You Must be in a Voice channel to Run the Music commands!");
  185. if (!serverQueue) return msg.channel.send("There is no Queue to skip!!");
  186.  
  187. serverQueue.connection.dispatcher.end('Ok, skipped!');
  188. return undefined;
  189.  
  190. } else if (command === `stop`) {
  191.  
  192. if (!msg.member.voiceChannel) return msg.channel.send("You Must be in a Voice channel to Run the Music commands!");
  193. if (!serverQueue) return msg.channel.send("There is no Queue to stop!!");
  194.  
  195. serverQueue.songs = [];
  196. serverQueue.connection.dispatcher.end('Ok, stopped & disconnected from your Voice channel');
  197. return undefined;
  198.  
  199. } else if (command === `vol`) {
  200.  
  201. if (!msg.member.voiceChannel) return msg.channel.send("You Must be in a Voice channel to Run the Music commands!");
  202. if (!serverQueue) return msg.channel.send('You only can use this command while music is playing!');
  203. if (!args[1]) return msg.channel.send(`The bot volume is **${serverQueue.volume}**`);
  204.  
  205. serverQueue.volume = args[1];
  206. serverQueue.connection.dispatcher.setVolumeLogarithmic(args[1] / 50);
  207.  
  208. return msg.channel.send(`Volume Now is **${args[1]}**`);
  209.  
  210. } else if (command === `np`) {
  211.  
  212. if (!serverQueue) return msg.channel.send('There is no Queue!');
  213. const embedNP = new Discord.RichEmbed()
  214. .setDescription(`Now playing **${serverQueue.songs[0].title}**`)
  215. return msg.channel.sendEmbed(embedNP);
  216.  
  217. } else if (command === `queue`) {
  218.  
  219. if (!serverQueue) return msg.channel.send('There is no Queue!!');
  220. let index = 0;
  221. // // //
  222. const embedqu = new Discord.RichEmbed()
  223. .setTitle("The Queue Songs :")
  224. .setDescription(`
  225. ${serverQueue.songs.map(song => `${++index}. **${song.title}**`).join('\n')}
  226. **Now playing :** **${serverQueue.songs[0].title}**`)
  227. .setColor("#f7abab")
  228. return msg.channel.sendEmbed(embedqu);
  229. } else if (command === `pause`) {
  230. if (serverQueue && serverQueue.playing) {
  231. serverQueue.playing = false;
  232. serverQueue.connection.dispatcher.pause();
  233. return msg.channel.send('Ok, paused');
  234. }
  235. return msg.channel.send('There is no Queue to Pause!');
  236. } else if (command === "resume") {
  237.  
  238. if (serverQueue && !serverQueue.playing) {
  239. serverQueue.playing = true;
  240. serverQueue.connection.dispatcher.resume();
  241. return msg.channel.send('Ok, resumed!');
  242.  
  243. }
  244. return msg.channel.send('Queue is empty!');
  245. }
  246.  
  247. return undefined;
  248. });
  249.  
  250. async function handleVideo(video, msg, voiceChannel, playlist = false) {
  251. const serverQueue = queue.get(msg.guild.id);
  252. console.log(video);
  253.  
  254.  
  255. const song = {
  256. id: video.id,
  257. title: Util.escapeMarkdown(video.title),
  258. url: `https://www.youtube.com/watch?v=${video.id}`
  259. };
  260. if (!serverQueue) {
  261. const queueConstruct = {
  262. textChannel: msg.channel,
  263. voiceChannel: voiceChannel,
  264. connection: null,
  265. songs: [],
  266. volume: 5,
  267. playing: true
  268. };
  269. queue.set(msg.guild.id, queueConstruct);
  270.  
  271. queueConstruct.songs.push(song);
  272.  
  273. try {
  274. var connection = await voiceChannel.join();
  275. queueConstruct.connection = connection;
  276. play(msg.guild, queueConstruct.songs[0]);
  277. } catch (error) {
  278. console.error(`I could not join the voice channel: ${error}!`);
  279. queue.delete(msg.guild.id);
  280. return msg.channel.send(`Can't join this channel: ${error}!`);
  281. }
  282. } else {
  283. serverQueue.songs.push(song);
  284. console.log(serverQueue.songs);
  285. if (playlist) return undefined;
  286. else return msg.channel.send(`**${song.title}**, just added to the queue! `);
  287. }
  288. return undefined;
  289. }
  290.  
  291. function play(guild, song) {
  292. const serverQueue = queue.get(guild.id);
  293.  
  294. if (!song) {
  295. serverQueue.voiceChannel.leave();
  296. queue.delete(guild.id);
  297. return;
  298. }
  299. console.log(serverQueue.songs);
  300.  
  301. const dispatcher = serverQueue.connection.playStream(ytdl(song.url))
  302. .on('end', reason => {
  303. if (reason === 'Stream is not generating quickly enough.') console.log('Song ended.');
  304. else console.log(reason);
  305. serverQueue.songs.shift();
  306. play(guild, serverQueue.songs[0]);
  307. })
  308. .on('error', error => console.error(error));
  309. dispatcher.setVolumeLogarithmic(serverQueue.volume / 5);
  310.  
  311. serverQueue.textChannel.send(`**${song.title}**, is now playing!`);
  312. }
  313.  
  314.  
  315. client.on('message', message => {
  316. if (message.content === '#help') {
  317. let helpEmbed = new Discord.RichEmbed()
  318. .setTitle('**أوامر الميوزك...**')
  319. .setDescription('**برفكس البوت (!)**')
  320. .addField('play', 'لتشغيل اغنية')
  321. .addField('join', 'دخول رومك الصوتي')
  322. .addField('disconnect', 'الخروج من رومك الصوتي')
  323. .addField('skip', 'تخطي الأغنية')
  324. .addField('pause', 'ايقاف الاغنية مؤقتا')
  325. .addField('resume', 'تكملة الاغنية')
  326. .addField('queue', 'اظهار قائمة التشغيل')
  327. .addField('np', 'اظهار الاغنية اللي انت مشغلها حاليا')
  328. .setFooter('(general_commands) لاظهار الاوامر العامة')
  329. message.channel.send(helpEmbed);
  330. }
  331. });
  332.  
  333. client.on('message', message => {
  334. if (message.content === 'general_commands') {
  335. let helpEmbed = new Discord.RichEmbed()
  336. .setTitle('**أوامر عامة...**')
  337. .addField('avatar', "افاتار الشخص المطلوب")
  338. .addField('gif', 'البحث عن جيف انت تطلبه')
  339. .addField('ping', 'معرفة ping البوت')
  340. .setFooter('المزيد قريبا ان شاء الله!')
  341. message.channel.send(helpEmbed);
  342. }
  343. });
  344.  
  345. const adminprefix = "#";
  346. const devs = ['339023914833281025','339023914833281025'];
  347. client.on('message', message => {
  348. var argresult = message.content.split(` `).slice(1).join(' ');
  349. if (!devs.includes(message.author.id)) return;
  350.  
  351. if (message.content.startsWith(adminprefix + 'setgame')) {
  352. client.user.setGame(argresult);
  353. message.channel.sendMessage(`**${argresult} تم تغيير بلاينق البوت إلى **`)
  354. } else
  355. if (message.content.startsWith(adminprefix + 'setname')) {
  356. client.user.setUsername(argresult).then
  357. message.channel.sendMessage(`**${argresult}** : تم تغيير أسم البوت إلى`)
  358. return message.reply("**لا يمكنك تغيير الاسم يجب عليك الانتظآر لمدة ساعتين . **");
  359. } else
  360. if (message.content.startsWith(adminprefix + 'setavatar')) {
  361. client.user.setAvatar(argresult);
  362. message.channel.sendMessage(`**${argresult}** : تم تغير صورة البوت`);
  363. } else
  364. if (message.content.startsWith(adminprefix + 'sett')) {
  365. client.user.setGame(argresult, "https://www.twitch.tv/idk");//حقوق دايموند كودز
  366. message.channel.sendMessage(`**تم تغيير تويتش البوت إلى ${argresult}**`)//حقوق دايموند كودز
  367. }
  368. });
  369.  
  370. client.login("token");
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement