Advertisement
YoJxsn

Untitled

Nov 5th, 2020
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.03 KB | None | 0 0
  1. const Discord = require("discord.js");
  2. const { prefix, token } = require("./config.json");
  3. const ytdl = require("ytdl-core");
  4.  
  5. const client = new Discord.Client();
  6.  
  7. const queue = new Map();
  8.  
  9. client.once("ready", () => {
  10. console.log("Ready!");
  11. });
  12.  
  13. client.once("reconnecting", () => {
  14. console.log("Reconnecting!");
  15. });
  16.  
  17. client.once("disconnect", () => {
  18. console.log("Disconnect!");
  19. });
  20.  
  21. client.on("message", async message => {
  22. if (message.author.bot) return;
  23. if (!message.content.startsWith(prefix)) return;
  24.  
  25. const serverQueue = queue.get(message.guild.id);
  26.  
  27. if (message.content.startsWith(`${prefix}play`)) {
  28. execute(message, serverQueue);
  29. return;
  30. } else if (message.content.startsWith(`${prefix}skip`)) {
  31. skip(message, serverQueue);
  32. return;
  33. } else if (message.content.startsWith(`${prefix}stop`)) {
  34. stop(message, serverQueue);
  35. return;
  36. } else {
  37. message.channel.send("You need to enter a valid command!");
  38. }
  39. });
  40.  
  41. async function execute(message, serverQueue) {
  42. const args = message.content.split(" ");
  43.  
  44. const voiceChannel = message.member.voice.channel;
  45. if (!voiceChannel)
  46. return message.channel.send(
  47. "You need to be in a voice channel to play music!"
  48. );
  49. const permissions = voiceChannel.permissionsFor(message.client.user);
  50. if (!permissions.has("CONNECT") || !permissions.has("SPEAK")) {
  51. return message.channel.send(
  52. "I need the permissions to join and speak in your voice channel!"
  53. );
  54. }
  55.  
  56. const songInfo = await ytdl.getInfo(args[1]);
  57. const song = {
  58. title: songInfo.title,
  59. url: songInfo.video_url
  60. };
  61.  
  62. if (!serverQueue) {
  63. const queueContruct = {
  64. textChannel: message.channel,
  65. voiceChannel: voiceChannel,
  66. connection: null,
  67. songs: [],
  68. volume: 100,
  69. playing: true
  70. };
  71.  
  72. queue.set(message.guild.id, queueContruct);
  73.  
  74. queueContruct.songs.push(song);
  75.  
  76. try {
  77. var connection = await voiceChannel.join();
  78. queueContruct.connection = connection;
  79. play(message.guild, queueContruct.songs[0]);
  80. } catch (err) {
  81. console.log(err);
  82. queue.delete(message.guild.id);
  83. return message.channel.send(err);
  84. }
  85. } else {
  86. serverQueue.songs.push(song);
  87. return message.channel.send(`${song.title} has been added to the queue!`);
  88. }
  89. }
  90.  
  91. function skip(message, serverQueue) {
  92. if (!message.member.voice.channel)
  93. return message.channel.send(
  94. "You have to be in a voice channel to stop the music!"
  95. );
  96. if (!serverQueue)
  97. return message.channel.send("There is no song that I could skip!");
  98. serverQueue.connection.dispatcher.end();
  99. }
  100.  
  101. function stop(message, serverQueue) {
  102. if (!message.member.voice.channel)
  103. return message.channel.send(
  104. "You have to be in a voice channel to stop the music!"
  105. );
  106. serverQueue.songs = [];
  107. serverQueue.connection.dispatcher.end();
  108. }
  109.  
  110. function play(guild, song) {
  111. const serverQueue = queue.get(guild.id);
  112. if (!song) {
  113. serverQueue.voiceChannel.leave();
  114. queue.delete(guild.id);
  115. return;
  116. }
  117.  
  118. const dispatcher = serverQueue.connection
  119. .play(ytdl(song.url))
  120. .on("finish", () => {
  121. serverQueue.songs.shift();
  122. play(guild, serverQueue.songs[0]);
  123. })
  124. .on("error", error => console.error(error));
  125. dispatcher.setVolumeLogarithmic(serverQueue.volume / 100);
  126. serverQueue.textChannel.send(`Start playing: **${song.title}**`);
  127. }
  128. const dispatcher = connection.play('/home/discord/audio.mp3', {
  129. volume: 0.5,
  130. });
  131. dispatcher.pause();
  132. dispatcher.resume();
  133.  
  134. dispatcher.setVolume(0.5); // half the volume
  135.  
  136. dispatcher.on('finish', () => {
  137. console.log('Finished playing!');
  138. });
  139. const broadcast = client.voice.createBroadcast();
  140.  
  141. broadcast.on('subscribe', dispatcher => {
  142. console.log('New broadcast subscriber!');
  143. });
  144.  
  145. broadcast.on('unsubscribe', dispatcher => {
  146. console.log('Channel unsubscribed from broadcast :(');
  147. });
  148. dispatcher.destroy(); // end the stream
  149. connection.play(fs.createReadStream('./media.webm'), {
  150. type: 'webm/opus',
  151. });
  152.  
  153. connection.play(fs.createReadStream('./media.ogg'), {
  154. type: 'ogg/opus',
  155. });
  156.  
  157.  
  158. client.login('not showing');
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement