Advertisement
PizzaClappsU

Untitled

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