Advertisement
Guest User

Untitled

a guest
May 26th, 2019
162
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.11 KB | None | 0 0
  1. const Discord = require('discord.js')
  2. const bot = new Discord.Client({ disableEveryone: true })
  3. const queue = new Map()
  4. const previous = new Map()
  5. const ytdl = require('ytdl-core')
  6. bot.login('NTU0NjkzODA2MjU2MDI5NzA3.D2ga1w.g6uMisx31L_qqk8yD3gYbO-B-VY')
  7. bot.on('ready', () => {
  8. console.log('BOT ONLINE')
  9. bot.user.setActivity('ANK | Musique')
  10. })
  11.  
  12. prefix = "!"
  13.  
  14. bot.on('message', async function (msg) {
  15. if (!msg.content.startsWith(prefix)) return undefined
  16. let args = msg.content.split(' ')
  17. const serverQueue = queue.get(msg.guild.id)
  18. const serverPrevious = previous.get(msg.guild.id)
  19. if (args[0].toLowerCase() === prefix + 'ping') {
  20. msg.channel.send("Pong!")
  21. }
  22. if (args[0].toLowerCase() === prefix + 'play') {
  23. const channel = msg.member.voiceChannel
  24. if (!channel) {
  25. msg.channel.send(":x: | Tu dois rejoindre un canal vocal pour exécuter cette commande !")
  26. return undefined
  27. }
  28. const permissions = channel.permissionsFor(msg.client.user)
  29. if (!args[1]) {
  30. msg.channel.send(":x: | Merci de saisir un URL YouTube !")
  31. return undefined
  32. }
  33. if (!permissions.has('CONNECT')) {
  34. return msg.channel.send(":information_source: | Je n'ai pas les permissions de me connecter à ce canal")
  35. }
  36. if (!permissions.has('SPEAK')) {
  37. return msg.channel.send(":information_source: | Je ne peux pas parler dans ce canal")
  38. }
  39. try {
  40. await ytdl(args[1])
  41. } catch(error) {
  42. return msg.channel.send(":x: | Je ne peux pas lire cette vidéo")
  43. }
  44. const songInfo = await ytdl.getInfo(args[1])
  45. const song = {
  46. title: songInfo.title,
  47. url: songInfo.video_url
  48. }
  49. if (!serverPrevious) {
  50. const previousContruct = {
  51. songs: [],
  52. status: false
  53. }
  54. previous.set(msg.guild.id, previousContruct)
  55. }
  56. if (!serverQueue) {
  57. const queueConstruct = {
  58. textChannel: msg.channel,
  59. voiceChannel: channel,
  60. connection: null,
  61. songs: [],
  62. volume: 5,
  63. playing: true
  64. }
  65. queue.set(msg.guild.id, queueConstruct)
  66. queueConstruct.songs.push(song)
  67. try {
  68. var connection = await channel.join()
  69. queueConstruct.connection = connection
  70. play(msg.guild, queueConstruct.songs[0])
  71. } catch(error) {
  72. return msg.channel.send(":x: | Je ne peux pas me connecter à ce canal")
  73. }
  74. } else {
  75. serverQueue.songs.push(song)
  76. return msg.channel.send(`:white_check_mark: | **${song.title}** a été ajouté à la liste d'attente`)
  77. }
  78. }
  79. if (args[0].toLowerCase() === prefix + 'stop') {
  80. const client = msg.guild.voiceConnection
  81. const channel = msg.member.voiceChannel
  82. if (!client) {
  83. msg.channel.send("Je ne suis pas dans un canal vocal")
  84. } else {
  85. channel.leave()
  86. msg.react('👋')
  87. return undefined
  88. }
  89. }
  90. if (args[0].toLowerCase() === prefix + 'skip') {
  91. if (!msg.member.voiceChannel) return msg.channel.send(":x: | Tu dois rejoindre un canal vocal pour exécuter cette commande !")
  92. skip(msg.guild)
  93. }
  94. if (args[0].toLowerCase() === prefix + 'volume') {
  95. if (!msg.member.voiceChannel) return msg.channel.send(":x: | Tu dois rejoindre un canal vocal pour exécuter cette commande !")
  96. if (!serverQueue) return msg.channel.send(":x: | Aucune musique en cours de lecture")
  97. if (!args[1]) return msg.channel.send(`🔊 | Le volume actuel est: **${serverQueue.volume}**`)
  98. serverQueue.volume = args[1]
  99. serverQueue.connection.dispatcher.setVolumeLogarithmic(args[1] / 5)
  100. return msg.channel.send(`🔊 | Volume réglé à: **${args[1]}**`)
  101. }
  102. return undefined
  103. })
  104.  
  105.  
  106. function play(guild, song) {
  107. const serverQueue = queue.get(guild.id)
  108. const serverPrevious = previous.get(guild.id)
  109. serverPrevious.songs.push(song)
  110. if (!song) {
  111. serverQueue.textChannel.send(":information_source: | Plus aucune musique en liste d'attente, déconnexion !")
  112. serverQueue.voiceChannel.leave()
  113. queue.delete(guild.id)
  114. previous.delete(guild.id)
  115. return
  116. }
  117. console.log(serverQueue.songs)
  118. console.log(serverPrevious.songs)
  119.  
  120. const dispatcher = serverQueue.connection.playStream(ytdl(song.url))
  121. .on('end', reason => {
  122. if (reason === 'Stream is not generating quickly enough.') console.log('Fin chanson')
  123. else console.log(reason)
  124. if (serverPrevious.status === true) {
  125. play(guild, serverPrevious.songs[0])
  126. serverPrevious.songs.shift()
  127. serverPrevious.status = false
  128. } else {
  129. serverQueue.songs.shift()
  130. play(guild, serverQueue.songs[0])
  131. }
  132. })
  133. .on('error', error => console.error(error))
  134. dispatcher.setVolumeLogarithmic(serverQueue.volume / 5)
  135.  
  136. serverQueue.textChannel.send(`:musical_note: | Joue: **${song.title}**`).then(async function(message) {
  137. playpause(message,guild)
  138. })
  139. }
  140.  
  141. function skip(guild) {
  142. const serverQueue = queue.get(guild.id)
  143. serverQueue.connection.dispatcher.end()
  144. }
  145.  
  146. async function playpause(message, guild) {
  147. const serverQueue = queue.get(guild.id)
  148. const serverPrevious = previous.get(guild.id)
  149. await message.react('⏮')
  150. await message.react('⏯')
  151. await message.react('⏭')
  152. const filter = function(reaction, user) {
  153. return ['⏮','⏯','⏭'].includes(reaction.emoji.name) && user.id != 554693806256029707
  154. }
  155. await message.awaitReactions(filter, { max: 1, time: 6000000, errors: ['time'] }).then(async function(collected) {
  156. const reaction = collected.first()
  157. if (reaction.emoji.name === '⏮') {
  158. serverPrevious.status = true
  159. skip(guild)
  160. return undefined
  161. }
  162. if (reaction.emoji.name === '⏯') {
  163. if (serverQueue.playing === true) {
  164. serverQueue.playing = false
  165. serverQueue.connection.dispatcher.pause()
  166. message.channel.send(":pause_button: | Musique en pause ! ").then(async function(message) {
  167. playpause(message,guild)
  168. })
  169. }
  170. else if (serverQueue.playing ===false) {
  171. serverQueue.playing = true
  172. serverQueue.connection.dispatcher.resume()
  173. message.channel.send(':arrow_forward: | Play!').then(async function(message) {
  174. playpause(message,guild)
  175. })
  176. }
  177. return undefined
  178. }
  179. if (reaction.emoji.name === '⏭') {
  180. message.channel.send(":fast_forward: | Musique suivante")
  181. skip(guild)
  182. return undefined
  183. }
  184. })
  185. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement