Advertisement
Guest User

Untitled

a guest
Jan 10th, 2020
163
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.27 KB | None | 0 0
  1.  
  2. const {
  3. prefix,
  4. token,
  5. } = require('./config.json');
  6. const ytdl = require('ytdl-core');
  7.  
  8. const queue = new Map();
  9.  
  10. client.once('ready', () => {
  11. console.log('Prêt');
  12. });
  13.  
  14. client.once('reconnecting', () => {
  15. console.log('Reconnection!');
  16. });
  17.  
  18. client.once('disconnect', () => {
  19. console.log('Déconnecté');
  20. });
  21.  
  22. client.on('message', async message => {
  23. if (message.author.bot) return;
  24. if (!message.content.startsWith(prefix)) return;
  25.  
  26. const serverQueue = queue.get(message.guild.id);
  27.  
  28. if (message.content.startsWith(`${prefix}play`)) {
  29. execute(message, serverQueue);
  30. return;
  31. } else if (message.content.startsWith(`${prefix}skip`)) {
  32. skip(message, serverQueue);
  33. return;
  34. } else if (message.content.startsWith(`${prefix}stop`)) {
  35. stop(message, serverQueue);
  36. return;
  37. }
  38. });
  39.  
  40. async function execute(message, serverQueue) {
  41. const args = message.content.split(' ');
  42.  
  43. const voiceChannel = message.member.voiceChannel;
  44. if (!voiceChannel) return message.channel.send('**Allez dans un salon vocal !**');
  45. const permissions = voiceChannel.permissionsFor(message.client.user);
  46. if (!permissions.has('CONNECT') || !permissions.has('SPEAK')) {
  47. return message.channel.send('**J\'ai besoin des permissions pour me connecter au salon vocal**');
  48. }
  49.  
  50. const songInfo = await ytdl.getInfo(args[1]);
  51. const song = {
  52. title: songInfo.title,
  53. url: songInfo.video_url,
  54. };
  55.  
  56. if (!serverQueue) {
  57. const queueContruct = {
  58. textChannel: message.channel,
  59. voiceChannel: voiceChannel,
  60. connection: null,
  61. songs: [],
  62. volume: 5,
  63. playing: true,
  64. };
  65.  
  66. queue.set(message.guild.id, queueContruct);
  67.  
  68. queueContruct.songs.push(song);
  69.  
  70. try {
  71. var connection = await voiceChannel.join();
  72. queueContruct.connection = connection;
  73. play(message.guild, queueContruct.songs[0]);
  74. } catch (err) {
  75. console.log(err);
  76. queue.delete(message.guild.id);
  77. return message.channel.send(err);
  78. }
  79. } else {
  80. serverQueue.songs.push(song);
  81. console.log(serverQueue.songs);
  82. return message.channel.send(`**${song.title} a été ajouté à la queue.** :white_check_mark:`);
  83. }
  84.  
  85. }
  86.  
  87. function skip(message, serverQueue) {
  88. if (!message.member.voiceChannel) return message.channel.send('Vous devez être dans le salon vocal pour stopper la musique.');
  89. if (!serverQueue) return message.channel.send('Il n\'y a plus de musique, je ne peux pas `skip`');
  90. serverQueue.connection.dispatcher.end();
  91. }
  92.  
  93. function stop(message, serverQueue) {
  94. if (!message.member.voiceChannel) return message.channel.send('Vous devez être dans le salon vocal pour stopper la musique.');
  95.  
  96. serverQueue.songs = [];
  97. serverQueue.connection.dispatcher.end();
  98. }
  99.  
  100. function play(guild, song) {
  101. const serverQueue = queue.get(guild.id);
  102.  
  103. if (!song) {
  104. serverQueue.voiceChannel.leave();
  105. queue.delete(guild.id);
  106. return;
  107. }
  108.  
  109. const dispatcher = serverQueue.connection.playStream(ytdl(song.url))
  110. .on('end', () => {
  111. serverQueue.songs.shift();
  112. play(guild, serverQueue.songs[0]);
  113. })
  114. .on('error', error => {
  115. console.error(error);
  116. });
  117. dispatcher.setVolumeLogarithmic(serverQueue.volume / 5);
  118. }
  119. client.login(token);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement