Advertisement
PizzaClappsU

Untitled

Nov 28th, 2019
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.01 KB | None | 0 0
  1. const Discord = require('discord.js');
  2. const client = new Discord.Client();
  3. const token = 'YOUR TOKEN GORES HERE';
  4. const ytdl = require('ytdl-core');
  5. const streamOptions = {
  6. seek: 0,
  7. volume: 1
  8. }
  9. client.login(token);
  10. client.on('ready', () => {
  11. console.log('I am ready!');
  12. });
  13. client.on('message', async message =>{
  14. if(message.author.bot)
  15. return;
  16. if(message.content.toLowerCase().startsWith('!play'))
  17. {
  18. let args = message.content.split(" ");
  19. let url = args[1];
  20. let voiceChannel = message.guild.channels.find(channel => channel.id === '649641310986436608');
  21.  
  22. if(ytdl.validateURL(url))
  23. {
  24. console.log("Valid URL!");
  25. var flag = musicUrls.some(element => element === url);
  26. if(!flag)
  27. {
  28. musicUrls.push(url);
  29. if(voiceChannel != null)
  30. {
  31. if(voiceChannel.connection)
  32. {
  33. console.log("Connection exists.");
  34. const embed = new Discord.RichEmbed();
  35. embed.setAuthor(client.user.username, client.user.displayAvatarURL);
  36. embed.setDescription("You've successfully added to the queue!");
  37. message.channel.send(embed);
  38. }
  39. else {
  40. try {
  41. const voiceConnection = await voiceChannel.join();
  42. await playSong(message.channel, voiceConnection, voiceChannel);
  43. }
  44. catch(ex)
  45. {
  46. console.log(ex)
  47. }
  48. }
  49. }
  50. }
  51. }
  52. }
  53. });
  54.  
  55. async function playSong(messageChannel, voiceConnection, voiceChannel)
  56. {
  57. const stream = ytdl(musicUrls[0],{ filter : 'audioonly'});
  58. const dispatcher = voiceConnection.playStream(stream, streamOptions);
  59.  
  60. dispatcher.on('end', () =>{
  61. musicUrls.shift();
  62. if(musicUrls.length == 0)
  63. voiceChannel.leave();
  64. else
  65. {
  66. setTimeout(() => {
  67. playSong(messageChannel, voiceConnection, voiceChannel);
  68. }, 1000);
  69. }
  70. });
  71. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement