Advertisement
Guest User

Untitled

a guest
Aug 22nd, 2017
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.64 KB | None | 0 0
  1. const Discord = require('discord.js');
  2. const client = new Discord.Client();
  3. const settings = require('./settings.json');
  4. const num = null;
  5. const yt = require('ytdl-core');
  6. const tokens = require('./tokens.json');
  7. const ffmpeg = require('ffmpeg');
  8. const ytdl = require('ytdl-core');
  9.  
  10. client.on('ready',() => {
  11. console.log('I\'m Active');
  12. });
  13.  
  14. client.on("guildMemberAdd", member => {
  15. let guild = member.guild;
  16. member.guild.defaultChannel.send(`Welcome to the Elavation Discord, ${member}!`);
  17. });
  18.  
  19. const prefix = "$";
  20. client.on('message', message => {
  21. if (message.author === client.user) return;
  22. if (message.content.startsWith(prefix + 'ping')) {
  23. message.reply('pong');
  24. }
  25. });
  26.  
  27. client.on('message', message => {
  28. if (message.author === client.user) return;
  29. if (message.content.startsWith(prefix + 'commands')) {
  30. message.reply('```The commands available to you here: https://pastebin.com/7EwuEvCU```');
  31. }
  32. });
  33. client.on('message', message => {
  34. if (message.author === client.user) return;
  35. if (message.content.startsWith(prefix + 'purge')) {
  36. message.channel.bulkDelete(num);
  37. message.reply('Your channel has been cleared!');
  38. }
  39. }
  40.  
  41. );
  42.  
  43. let queue = {};
  44.  
  45. const commands = {
  46. 'play': (msg) => {
  47. if (queue[msg.guild.id] === undefined) return msg.channel.send(`Add some songs to the queue first with ${tokens.prefix}add`);
  48. if (!msg.guild.voiceConnection) return commands.join(msg).then(() => commands.play(msg));
  49. if (queue[msg.guild.id].playing) return msg.channel.send('Already Playing');
  50. let dispatcher;
  51. queue[msg.guild.id].playing = true;
  52.  
  53. console.log(queue);
  54. (function play(song) {
  55. console.log(song);
  56. if (song === undefined) return msg.channel.send('Queue is empty').then(() => {
  57. queue[msg.guild.id].playing = false;
  58. msg.member.voiceChannel.leave();
  59. });
  60. msg.channel.send(`Playing: **${song.title}** as requested by: **${song.requester}**`);
  61. dispatcher = msg.guild.voiceConnection.playStream(yt(song.url, { audioonly: true }), { passes : tokens.passes });
  62. let collector = msg.channel.createCollector(m => m);
  63. collector.on('send', m => {
  64. if (m.content.startsWith(tokens.prefix + 'pause')) {
  65. msg.channel.send('paused').then(() => {dispatcher.pause();});
  66. } else if (m.content.startsWith(tokens.prefix + 'resume')){
  67. msg.channel.send('resumed').then(() => {dispatcher.resume();});
  68. } else if (m.content.startsWith(tokens.prefix + 'skip')){
  69. msg.channel.send('skipped').then(() => {dispatcher.end();});
  70. } else if (m.content.startsWith('volume+')){
  71. if (Math.round(dispatcher.volume*50) >= 100) return msg.channel.send(`Volume: ${Math.round(dispatcher.volume*50)}%`);
  72. dispatcher.setVolume(Math.min((dispatcher.volume*50 + (2*(m.content.split('+').length-1)))/50,2));
  73. msg.channel.send(`Volume: ${Math.round(dispatcher.volume*50)}%`);
  74. } else if (m.content.startsWith('volume-')){
  75. if (Math.round(dispatcher.volume*50) <= 0) return msg.channel.send(`Volume: ${Math.round(dispatcher.volume*50)}%`);
  76. dispatcher.setVolume(Math.max((dispatcher.volume*50 - (2*(m.content.split('-').length-1)))/50,0));
  77. msg.channel.send(`Volume: ${Math.round(dispatcher.volume*50)}%`);
  78. } else if (m.content.startsWith(tokens.prefix + 'time')){
  79. msg.channel.send(`time: ${Math.floor(dispatcher.time / 60000)}:${Math.floor((dispatcher.time % 60000)/1000) <10 ? '0'+Math.floor((dispatcher.time % 60000)/1000) : Math.floor((dispatcher.time % 60000)/1000)}`);
  80. }
  81. });
  82. dispatcher.on('end', () => {
  83. collector.stop();
  84. play(queue[msg.guild.id].songs.shift());
  85. });
  86. dispatcher.on('error', (err) => {
  87. return msg.channel.send('error: ' + err).then(() => {
  88. collector.stop();
  89. play(queue[msg.guild.id].songs.shift());
  90. });
  91. });
  92. })(queue[msg.guild.id].songs.shift());
  93. },
  94. 'join': (msg) => {
  95. return new Promise((resolve, reject) => {
  96. const voiceChannel = msg.member.voiceChannel;
  97. if (!voiceChannel || voiceChannel.type !== 'voice') return msg.reply('I couldn\'t connect to your voice channel...');
  98. voiceChannel.join().then(connection => resolve(connection)).catch(err => reject(err));
  99. });
  100. },
  101. 'add': (msg) => {
  102. let url = msg.content.split(' ')[1];
  103. if (url == '' || url === undefined) return msg.channel.send(`You must add a YouTube video url, or id after ${tokens.prefix}add`);
  104. yt.getInfo(url, (err, info) => {
  105. if(err) return msg.channel.send('Invalid YouTube Link: ' + err);
  106. if (!queue.hasOwnProperty(msg.guild.id)) queue[msg.guild.id] = {}, queue[msg.guild.id].playing = false, queue[msg.guild.id].songs = [];
  107. queue[msg.guild.id].songs.push({url: url, title: info.title, requester: msg.author.username});
  108. msg.channel.send(`added **${info.title}** to the queue`);
  109. });
  110. },
  111. 'queue': (msg) => {
  112. if (queue[msg.guild.id] === undefined) return msg.channel.send(`Add some songs to the queue first with ${tokens.prefix}add`);
  113. let tosend = [];
  114. queue[msg.guild.id].songs.forEach((song, i) => { tosend.push(`${i+1}. ${song.title} - Requested by: ${song.requester}`);});
  115. msg.channel.send(`__**${msg.guild.name}'s Music Queue:**__ Currently **${tosend.length}** songs queued ${(tosend.length > 15 ? '*[Only next 15 shown]*' : '')}\n\`\`\`${tosend.slice(0,15).join('\n')}\`\`\``);
  116. },
  117. 'help': (msg) => {
  118. let tosend = ['```xl', tokens.prefix + 'join : "Join Voice channel of msg sender"', tokens.prefix + 'add : "Add a valid youtube link to the queue"', tokens.prefix + 'queue : "Shows the current queue, up to 15 songs shown."', tokens.prefix + 'play : "Play the music queue if already joined to a voice channel"', '', 'the following commands only function while the play command is running:'.toUpperCase(), tokens.prefix + 'pause : "pauses the music"', tokens.prefix + 'resume : "resumes the music"', tokens.prefix + 'skip : "skips the playing song"', tokens.prefix + 'time : "Shows the playtime of the song."', 'volume+(+++) : "increases volume by 2%/+"', 'volume-(---) : "decreases volume by 2%/-"', '```'];
  119. msg.channel.send(tosend.join('\n'));
  120. },
  121. 'reboot': (msg) => {
  122. if (msg.author.id == tokens.adminID) process.exit(); //Requires a node module like Forever to work.
  123. }
  124. };
  125.  
  126. client.on('ready', () => {
  127. console.log('ready!');
  128. });
  129.  
  130. client.on('message', msg => {
  131. if (!msg.content.startsWith(tokens.prefix)) return;
  132. if (commands.hasOwnProperty(msg.content.toLowerCase().slice(tokens.prefix.length).split(' ')[0])) commands[msg.content.toLowerCase().slice(tokens.prefix.length).split(' ')[0]](msg);
  133. });
  134. client.login(settings.token);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement