Advertisement
Guest User

Untitled

a guest
Jan 29th, 2020
135
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.17 KB | None | 0 0
  1. const { Client, Util } = require('discord.js');
  2. const { TOKEN, PREFIX, GOOGLE_API_KEY } = require('./config');
  3. const YouTube = require('simple-youtube-api');
  4. const ytdl = require('ytdl-core');
  5.  
  6. const youtube = new YouTube(GOOGLE_API_KEY);
  7.  
  8. const queue = new Map();
  9.  
  10. bot.on('warn', console.warn);
  11.  
  12. bot.on('error', console.error);
  13.  
  14. bot.on('ready', () => console.log('Musicbot ready'));
  15.  
  16. bot.on('disconnect', () => console.log('Csak leválasztottam, meggyőződve róla, hogy tudod, most újra csatlakozom...'));
  17.  
  18. bot.on('reconnecting', () => console.log('Most újra csatlakozom!'));
  19.  
  20. bot.on('message', async msg => { // eslint-disable-line
  21.  
  22.  
  23. let setprefix = JSON.parse(fs.readFileSync("./setprefix.json", "utf8"));
  24. if(!setprefix[msg.guild.id]) {
  25. setprefix[msg.guild.id] = {
  26. prefix: botconfig.prefix,
  27. };
  28. }
  29. let prefix = setprefix[msg.guild.id].prefix
  30. if (msg.author.bot) return undefined;
  31. if (!msg.content.startsWith(prefix)) return undefined;
  32.  
  33. const args = msg.content.split(' ');
  34. const searchString = args.slice(1).join(' ');
  35. const url = args[1] ? args[1].replace(/<(.+)>/g, '$1') : '';
  36. const serverQueue = queue.get(msg.guild.id);
  37.  
  38. let command = msg.content.toLowerCase().split(' ')[0];
  39. command = command.slice(prefix.length)
  40.  
  41. if (command === 'play') {
  42. const voiceChannel = msg.member.voiceChannel;
  43. let botusername = bot.user.username
  44. let joinEmbed = new Discord.RichEmbed()
  45. .setColor('#ff5930')
  46. .setAuthor(`${botusername} - ERROR`, bot.user.displayAvatarURL)
  47. .setThumbnail(bot.user.displayAvatarURL)
  48. .setDescription(":no_entry: kérlek csatlakozz egy hangcsatornához! :no_entry: ");
  49. if (!voiceChannel) return msg.channel.send({embed: joinEmbed});
  50. const permissions = voiceChannel.permissionsFor(msg.client.user);
  51. if (!permissions.has('CONNECT')) {
  52. let botusername = bot.user.username
  53. let nyetEmbed = new Discord.RichEmbed()
  54. .setColor('#ff5930')
  55. .setAuthor(`${botusername} - ERROR`, bot.user.displayAvatarURL)
  56. .setThumbnail(bot.user.displayAvatarURL)
  57. .setDescription(":no_entry: Nem tudok csatlakozni az audio csatornához, ellenőrizze, hogy van megfelelő engedélyem! :no_entry: ");
  58. return msg.channel.send({embed: nyetEmbed});
  59. }
  60. if (!permissions.has('SPEAK')) {
  61. return msg.channel.send({embed: nyetEmbed});
  62. }
  63.  
  64. if (url.match(/^https?:\/\/(www.youtube.com|youtube.com)\/playlist(.*)$/)) {
  65. const playlist = await youtube.getPlaylist(url);
  66. const videos = await playlist.getVideos();
  67. for (const video of Object.values(videos)) {
  68. const video2 = await youtube.getVideoByID(video.id); // eslint-disable-line no-await-in-loop
  69. await handleVideo(video2, msg, voiceChannel, true); // eslint-disable-line no-await-in-loop
  70. }
  71. return msg.channel.send(`✅ lejátszási lista: **${playlist.title}** felkerült a listára!`);
  72. } else {
  73. try {
  74. var video = await youtube.getVideo(url);
  75. } catch (error) {
  76. try {
  77. var videos = await youtube.searchVideos(searchString, 10);
  78. let index = 0;
  79. let botusername = bot.user.username
  80. let hEmbed = new Discord.RichEmbed()
  81. .setColor('#30acff')
  82. .setAuthor('Zene választás')
  83. .setThumbnail(bot.user.displayAvatarURL)
  84. .setDescription(`${videos.map(video2 => `**${++index} -** ${video2.title}`).join('\n')}\n\n\nKérjük, adjon meg egy értéket, hogy 1-10 keresési eredményt válasszon.`)
  85. msg.channel.send({embed: hEmbed});
  86. // eslint-disable-next-line max-depth
  87. try {
  88. var response = await msg.channel.awaitMessages(msg2 => msg2.content > 0 && msg2.content < 11, {
  89. maxMatches: 1,
  90. time: 10000,
  91. errors: ['time']
  92. });
  93. } catch (err) {
  94. console.error(err);
  95. let ertekstopEmbed = new Discord.RichEmbed()
  96. .setColor('#ff5930')
  97. .setAuthor(`${botusername} - ERROR`, bot.user.displayAvatarURL)
  98. .setThumbnail(bot.user.displayAvatarURL)
  99. .setDescription(":no_entry: Nem adot meg értéket :no_entry: ");
  100. return msg.channel.send({embed: ertekstopEmbed});
  101. }
  102. const videoIndex = parseInt(response.first().content);
  103. var video = await youtube.getVideoByID(videos[videoIndex - 1].id);
  104. } catch (err) {
  105. console.error(err);
  106. return msg.channel.send('🆘 Nem kaptam keresési eredményeket.');
  107. }
  108. }
  109. return handleVideo(video, msg, voiceChannel);
  110. }
  111. } else if (command === 'skip') {
  112. let botusername = bot.user.username
  113. let notvoiceEmbed = new Discord.RichEmbed()
  114. .setColor('#f76252')
  115. .setAuthor(`${botusername} - ERROR`, bot.user.displayAvatarURL)
  116. .setThumbnail(bot.user.displayAvatarURL)
  117. .setDescription(":no_entry: Nem egy hangcsatornán vagy! :no_entry: ");
  118. let skipEmbed = new Discord.RichEmbed()
  119. .setColor('#f76252')
  120. .setAuthor(`${botusername} - ERROR`, bot.user.displayAvatarURL)
  121. .setThumbnail(bot.user.displayAvatarURL)
  122. .setDescription(":no_entry: Mivel nincs zene, nincs mit kihagyni! :no_entry: ");
  123. if (!msg.member.voiceChannel) return msg.channel.send({embed: notvoiceEmbed});
  124. if (!serverQueue) return msg.channel.send({embed: skipEmbed});
  125. serverQueue.connection.dispatcher.end('Az átugrás parancsot használták!');
  126. return undefined;
  127. } else if (command === 'stop') {
  128. let botusername = bot.user.username
  129. let stopEmbed = new Discord.RichEmbed()
  130. .setColor('#f76252')
  131. .setAuthor(`${botusername} - ERROR`, bot.user.displayAvatarURL)
  132. .setThumbnail(bot.user.displayAvatarURL)
  133. .setDescription(":no_entry: Mivel nincs zene, így semmi sem állhat meg. :no_entry: ");
  134. if (!msg.member.voiceChannel) return msg.channel.send({embed: skipEmbed});
  135. if (!serverQueue) return msg.channel.send({embed: stopEmbed});
  136. serverQueue.songs = [];
  137. serverQueue.connection.dispatcher.end('A Stop parancsot használták!');
  138. return undefined;
  139. } else if (command === 'np') {
  140. let botusername = bot.user.username
  141. let notplayEmbed = new Discord.RichEmbed()
  142. .setColor('#f76252')
  143. .setAuthor(`${botusername} - ERROR`, bot.user.displayAvatarURL)
  144. .setThumbnail(bot.user.displayAvatarURL)
  145. .setDescription(":no_entry: Nem játszik zenét. :no_entry: ");
  146. if (!serverQueue) return msg.channel.send({embed: notplayEmbed});
  147. return msg.channel.send(`🎶 Most játszik: **${serverQueue.songs[0].title}**`);
  148. } else if (command === 'list') {
  149. if (!serverQueue) return msg.channel.send({embed: notplayEmbed});
  150. let playlistEmbed = new Discord.RichEmbed()
  151. .setColor('#30acff')
  152. .setAuthor('lejátszási lista')
  153. .setThumbnail(bot.user.displayAvatarURL)
  154. .setDescription(`${serverQueue.songs.map(song => `**-** ${song.title}`).join('\n')}\n **🎶 Most játszik:** ${serverQueue.songs[0].title}`)
  155. return msg.channel.send({embed: playlistEmbed});
  156. } else if (command === 'pause') {
  157. if (serverQueue && serverQueue.playing) {
  158. serverQueue.playing = false;
  159. serverQueue.connection.dispatcher.pause();
  160. return msg.channel.send('⏸ Zene szüneteltetve!');
  161. }
  162. return msg.channel.send({embed: notplayEmbed});
  163. } else if (command === 'resume') {
  164. if (serverQueue && !serverQueue.playing) {
  165. serverQueue.playing = true;
  166. serverQueue.connection.dispatcher.resume();
  167. return msg.channel.send('▶ Zene folytatódik!');
  168. }
  169. return msg.channel.send({embed: notplayEmbed});
  170. }
  171.  
  172. return undefined;
  173. });
  174.  
  175. async function handleVideo(video, msg, voiceChannel, playlist = false) {
  176. const serverQueue = queue.get(msg.guild.id);
  177. //console.log(video);
  178. const song = {
  179. id: video.id,
  180. title: Util.escapeMarkdown(video.title),
  181. url: `https://www.youtube.com/watch?v=${video.id}`
  182. };
  183. if (!serverQueue) {
  184. const queueConstruct = {
  185. textChannel: msg.channel,
  186. voiceChannel: voiceChannel,
  187. connection: null,
  188. songs: [],
  189. volume: 5,
  190. playing: true
  191. };
  192. queue.set(msg.guild.id, queueConstruct);
  193.  
  194. queueConstruct.songs.push(song);
  195.  
  196. try {
  197. var connection = await voiceChannel.join();
  198. queueConstruct.connection = connection;
  199. play(msg.guild, queueConstruct.songs[0]);
  200. } catch (error) {
  201. console.error(`Nem tudtok csatlakozni a hangcsatornahoz: ${error}`);
  202. queue.delete(msg.guild.id);
  203. return msg.channel.send(`Nem tudok csatlakozni az audio csatornához: ${error}`);
  204. }
  205. } else {
  206. serverQueue.songs.push(song);
  207. console.log(serverQueue.songs);
  208. if (playlist) return undefined;
  209. else return msg.channel.send(`✅ **${song.title}** felkerült a listára!`);
  210. }
  211. return undefined;
  212. }
  213.  
  214. function play(guild, song) {
  215. const serverQueue = queue.get(guild.id);
  216.  
  217. if (!song) {
  218. serverQueue.voiceChannel.leave();
  219. queue.delete(guild.id);
  220. return;
  221. }
  222. console.log(serverQueue.songs);
  223.  
  224. const dispatcher = serverQueue.connection.playStream(ytdl(song.url))
  225. .on('end', reason => {
  226. if (reason === 'A stream nem generál elég gyorsan.') console.log('A dal véget ért.');
  227. else console.log(reason);
  228. serverQueue.songs.shift();
  229. play(guild, serverQueue.songs[0]);
  230. })
  231. .on('error', error => console.error(error));
  232. dispatcher.setVolumeLogarithmic(serverQueue.volume / 5);
  233.  
  234. serverQueue.textChannel.send(`🎶 Music Title: **${song.title}**`);
  235. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement