Advertisement
Guest User

Untitled

a guest
Feb 20th, 2020
211
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 12.44 KB | None | 0 0
  1. const { Client, Util } = require(`discord.js`);
  2. const { TOKEN, PREFIX, GOOGLE_API_KEY} = require(`./config`);
  3. const ytdl = require(`ytdl-core`);
  4. const YouTube = require(`simple-youtube-api`)
  5. var loop = false;
  6.  
  7. const client = new Client({ disableEveryone: true });
  8.  
  9. const youtube = new YouTube(GOOGLE_API_KEY);
  10.  
  11. const queue = new Map();
  12.  
  13. client.on(`warn`, console.warn);
  14.  
  15. client.on(`error`, console.error);
  16.  
  17. client.on(`ready`, () => {
  18. console.log("Active!");
  19. client.user.setActivity(`.help`);
  20. })
  21.  
  22. client.on(`disconnect`, () => console.log("I just disconnected, reconnecting now!"))
  23.  
  24. client.on(`reconnecting`, () => console.log("I am reconnecting now!"));
  25.  
  26. client.on(`message`, async msg => { // eslist disable line
  27. if (msg.author.bot) return undefined;
  28. if (!msg.content.startsWith(PREFIX)) return undefined;
  29. const args = msg.content.split(" ");
  30. const searchString = args.slice(1).join(" ");
  31. const url = args[1] ? args[1].replace(/<(.+)>/g, `$1`) : ``;
  32. const serverQueue = queue.get(msg.guild.id);
  33.  
  34. if (msg.content.startsWith(`${PREFIX}play`)) {
  35. const voiceChannel = msg.member.voiceChannel;
  36. if (!voiceChannel) return msg.channel.send(`**You need to be in a voice channel to play music!**`);
  37. const permissions = voiceChannel.permissionsFor(msg.client.user);
  38. if(!permissions.has(`CONNECT`)) {
  39. return msg.channel.send(`**I cant connect to the voice channel, make sure I have permissions!**`);
  40. }
  41. if (!permissions.has(`SPEAK`)) {
  42. return msg.channel.send(`**I cant speak in the voice channel, make sure I have permissions!**`);
  43. }
  44.  
  45. if (url.match(/^https?:\/\/(www.youtube.com|youtube.com)\/playlist(.*)$/)) {
  46. const playlist = await youtube.getPlaylist(url);
  47. const videos = await playlist.getVideos();
  48. for (const video of Object.values(videos)) {
  49. const video2 = await youtube.getVideoByID(video.id); // eslint disable line no await in loop
  50. await handleVideo(video2, msg, voiceChannel, true); // eslist disable line no await in loop
  51. }
  52. return msg.channel.send(`Playlist: **${playlist.title}** has been added to the queue!`);
  53. } else {
  54. try {
  55. var video = await youtube.getVideo(url);
  56. } catch (error) {
  57. try {
  58. var videos = await youtube.searchVideos(searchString, 1);
  59. var video = await youtube.getVideoByID(videos[0].id);
  60. } catch (err) {
  61. console.error(err);
  62. return msg.channel.send("**I could not find any search results!**");
  63. }
  64. }
  65.  
  66. return handleVideo(video, msg, voiceChannel);
  67. }
  68. } else if (msg.content.startsWith(`${PREFIX}skip`)) {
  69. if (!msg.member.voiceChannel) return msg.channel.send("**Your not in a voice channel!**");
  70. if(!serverQueue) return msg.channel.send("**There is nothing playing that I can skip!**");
  71. msg.channel.send("**Skipping song for you now!**");
  72. serverQueue.connection.dispatcher.end("Skip command has been used!");
  73. return undefined;
  74. } else if (msg.content.startsWith(`${PREFIX}stop`)) {
  75. if (!msg.member.voiceChannel) return msg.channel.send("**Your not in a voice channel!**");
  76. if(!serverQueue) return msg.channel.send("**There is nothing playing right now, I can not stop!**");
  77. serverQueue.songs = [];
  78. serverQueue.connection.dispatcher.end("Stop command has been used!");
  79. return msg.channel.send("**Stopping music for you now!**");
  80. } else if (msg.content.startsWith(`${PREFIX}volume`)) {
  81. if(!serverQueue) return msg.channel.send("**There is nothing playing!**");
  82. if (!msg.member.voiceChannel) return msg.channel.send("**Your not in a voice channel!**");
  83. if(!args[1]) return msg.channel.send(`**The current volume is: ${serverQueue.volume}**`);
  84. serverQueue.volume = args[1];
  85. serverQueue.connection.dispatcher.setVolumeLogarithmic(args[1] / 5);
  86. return msg.channel.send(`**I set the volume to: ${args[1]}**`);
  87. } else if (msg.content.startsWith(`${PREFIX}np`)) {
  88. if(!serverQueue) return msg.channel.send("**There is nothing playing!**");
  89. return msg.channel.send(`**Now playing: ${serverQueue.songs[0].title}**`);
  90. } else if (msg.content.startsWith(`${PREFIX}queue`)) {
  91. if(!serverQueue) return msg.channel.send("**There is nothing playing!**");
  92. return msg.channel.send(`
  93. __**Song Queue:**__
  94. ${serverQueue.songs.map(song => `**-** ${song.title}`).join(`\n`)}
  95.  
  96. **Now playing:** ${serverQueue.songs[0].title}
  97. `);
  98. } else if (msg.content.startsWith(`${PREFIX}pause`)) {
  99. if (serverQueue && serverQueue.playing) {
  100. serverQueue.playing = false;
  101. serverQueue.connection.dispatcher.pause();
  102. return msg.channel.send("**Paused the music for you!**");
  103. }
  104. return msg.channel.send("**There is nothing playing!**");
  105. } else if (msg.content.startsWith(`${PREFIX}resume`)) {
  106. if(serverQueue && !serverQueue.playing) {
  107. serverQueue.playing = true;
  108. serverQueue.connection.dispatcher.resume();
  109. return msg.channel.send("**Resumed the music for you!**");
  110. }
  111. return msg.channel.send("**There is nothing playing!**");
  112. } else if(msg.content.startsWith(`${PREFIX}help`)) {
  113. msg.channel.send(`These are the commands for the music bot!
  114. **.skip** will skip the current song and play the next one in the queue
  115. **.stop** will make the bot stop playing music and leave the voice channel
  116. **.np** will show the title of the song currently playing
  117. **.volume** will show the current volume the music it at
  118. **.volume *(insert number)*** will change that to the volume level
  119. **.pause** will pause the music bot where the song is
  120. **.resume** will play the song where it was last paused
  121. **.queue** will show you the queue list of songs that are going to be played
  122. **.play *(yotube url)*** this will play that song that pasted
  123. **.play *(song name)*** will play the song that you put the name of
  124. **.loop** will play the song on repeat until .loop is sent again
  125. `);
  126. }else if (msg.content.startsWith(`${PREFIX}loop`)) {
  127. loop = !loop;
  128. if (loop === false) {
  129. msg.channel.send("**The music has now disabled loop!**");
  130. } if (loop === true) {
  131. msg.channel.send("**The music is now enabled to loop!**");
  132. }}
  133. return undefined;
  134. });
  135.  
  136. async function handleVideo(video, msg, voiceChannel, playlist = false) {
  137. const serverQueue = queue.get(msg.guild.id);
  138. console.log(video);
  139. const song = {
  140. id: video.id,
  141. title: Util.escapeMarkdown(video.title),
  142. url: `https://www.youtube.com/watch?v=${video.id}`
  143. };
  144. if(!serverQueue) {
  145. const queueConstruct = {
  146. textChannel: msg.channel,
  147. voiceChannel: voiceChannel,
  148. connection: null,
  149. songs: [],
  150. volume: 5,
  151. playing: true,
  152. };
  153. queue.set(msg.guild.id, queueConstruct);
  154.  
  155. queueConstruct.songs.push(song);
  156.  
  157. try {
  158. var connection = await voiceChannel.join();
  159. queueConstruct.connection = connection;
  160. play(msg.guild, queueConstruct.songs[0]);
  161. } catch (error) {
  162. console.error(`I could not join the voice channel: ${error}`);
  163. queue.delete(msg.guild.id);
  164. return msg.channel.send(`I could not join the voice channel: ${error}`);
  165. }
  166. } else {
  167. serverQueue.songs.push(song);
  168. console.log(serverQueue.songs);
  169. if (playlist) return undefined;
  170. else return msg.channel.send(`**${song.title}** has been added to the queue!`);
  171. }
  172. return undefined;
  173. }
  174.  
  175. function play(guild, song) {
  176. const serverQueue = queue.get(guild.id);
  177.  
  178. if(!song) {
  179. serverQueue.voiceChannel.leave();
  180. queue.delete(guild.id);
  181. }
  182. client.on(`message`, msg => {
  183. if (msg.content.startsWith(`${PREFIX}clearqueue`)) {
  184. serverQueue.voiceChannel.leave();
  185. queue.delete(guild.id);
  186. return;
  187. }
  188. })
  189. console.log(serverQueue.songs);
  190. const dispatcher = serverQueue.connection.playStream(ytdl(song.url))
  191. .on(`end`, reason => {
  192. if(reason === "Stream is not generating quickly enough!") console.log("Song ended!");
  193. if (loop === false) {
  194. serverQueue.songs.shift();
  195. }
  196. play(guild, serverQueue.songs[0]);
  197. })
  198. .on(`error`, error => console.error(error));
  199. dispatcher.setVolumeLogarithmic(serverQueue.volume / 5);
  200.  
  201. if (loop === false) {
  202. serverQueue.textChannel.send(`**Now playing: ${song.title}**`);
  203. }
  204. }
  205.  
  206. client.login(TOKEN)
  207.  
  208.  
  209.  
  210.  
  211.  
  212.  
  213.  
  214. ERROR CODE
  215. node .
  216. I just disconnected, reconnecting now!
  217. (node:15392) UnhandledPromiseRejectionWarning: Error: Incorrect login details were provided.
  218. at WebSocketConnection.<anonymous> (C:\Users\maxhu\Desktop\Discord Bot\node_modules\discord.js\src\client\ClientManager.js:48:41)
  219. at Object.onceWrapper (events.js:313:26)
  220. at WebSocketConnection.emit (events.js:223:5)
  221. at WebSocketConnection.onClose (C:\Users\maxhu\Desktop\Discord Bot\node_modules\discord.js\src\client\websocket\WebSocketConnection.js:390:10)
  222. at WebSocket.onClose (C:\Users\maxhu\Desktop\Discord Bot\node_modules\ws\lib\event-target.js:124:16)
  223. at WebSocket.emit (events.js:223:5)
  224. at WebSocket.emitClose (C:\Users\maxhu\Desktop\Discord Bot\node_modules\ws\lib\websocket.js:191:10)
  225. at TLSSocket.socketOnClose (C:\Users\maxhu\Desktop\Discord Bot\node_modules\ws\lib\websocket.js:850:15)
  226. at TLSSocket.emit (events.js:228:7)
  227. at net.js:664:12
  228. (node:15392) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was
  229. not handled with .catch(). (rejection id: 2)
  230. (node:15392) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.PS C:\Users\maxhu\Desktop\Discord Bot> ^C
  231. PS C:\Users\maxhu\Desktop\Discord Bot> node .
  232. I just disconnected, reconnecting now!
  233. (node:17272) UnhandledPromiseRejectionWarning: Error: Incorrect login details were provided.
  234. at WebSocketConnection.<anonymous> (C:\Users\maxhu\Desktop\Discord Bot\node_modules\discord.js\src\client\ClientManager.js:48:41)
  235. at Object.onceWrapper (events.js:313:26)
  236. at WebSocketConnection.emit (events.js:223:5)
  237. PS C:\Users\maxhu\Desktop\Discord Bot> node .
  238. I just disconnected, reconnecting now!
  239. (node:3508) UnhandledPromiseRejectionWarning: Error: Incorrect login details were provided.
  240. at WebSocketConnection.<anonymous> (C:\Users\maxhu\Desktop\Discord Bot\node_modules\discord.js\src\client\ClientManager.js:48:41)
  241. at Object.onceWrapper (events.js:313:26)
  242. at WebSocketConnection.emit (events.js:223:5)
  243. at WebSocketConnection.onClose (C:\Users\maxhu\Desktop\Discord Bot\node_modules\discord.js\src\client\websocket\WebSocketConnection.js:390:10)
  244. at WebSocket.onClose (C:\Users\maxhu\Desktop\Discord Bot\node_modules\ws\lib\event-target.js:124:16)
  245. at WebSocket.emit (events.js:223:5)
  246. at WebSocket.emitClose (C:\Users\maxhu\Desktop\Discord Bot\node_modules\ws\lib\websocket.js:191:10)
  247. at TLSSocket.socketOnClose (C:\Users\maxhu\Desktop\Discord Bot\node_modules\ws\lib\websocket.js:850:15)
  248. at TLSSocket.emit (events.js:228:7)
  249. at net.js:664:12
  250. (node:3508) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 2)
  251. (node:3508) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
  252. PS C:\Users\maxhu\Desktop\Discord Bot>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement