Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- const { Client, Util } = require('discord.js');
- const { TOKEN, PREFIX, GOOGLE_API_KEY } = require('./config');
- const YouTube = require('simple-youtube-api');
- const ytdl = require('ytdl-core');
- const youtube = new YouTube(GOOGLE_API_KEY);
- const queue = new Map();
- bot.on('warn', console.warn);
- bot.on('error', console.error);
- bot.on('ready', () => console.log('Musicbot ready'));
- bot.on('disconnect', () => console.log('Csak leválasztottam, meggyőződve róla, hogy tudod, most újra csatlakozom...'));
- bot.on('reconnecting', () => console.log('Most újra csatlakozom!'));
- bot.on('message', async msg => { // eslint-disable-line
- let setprefix = JSON.parse(fs.readFileSync("./setprefix.json", "utf8"));
- if(!setprefix[msg.guild.id]) {
- setprefix[msg.guild.id] = {
- prefix: botconfig.prefix,
- };
- }
- let prefix = setprefix[msg.guild.id].prefix
- if (msg.author.bot) return undefined;
- if (!msg.content.startsWith(prefix)) return undefined;
- const args = msg.content.split(' ');
- const searchString = args.slice(1).join(' ');
- const url = args[1] ? args[1].replace(/<(.+)>/g, '$1') : '';
- const serverQueue = queue.get(msg.guild.id);
- let command = msg.content.toLowerCase().split(' ')[0];
- command = command.slice(prefix.length)
- if (command === 'play') {
- const voiceChannel = msg.member.voiceChannel;
- let botusername = bot.user.username
- let joinEmbed = new Discord.RichEmbed()
- .setColor('#ff5930')
- .setAuthor(`${botusername} - ERROR`, bot.user.displayAvatarURL)
- .setThumbnail(bot.user.displayAvatarURL)
- .setDescription(":no_entry: kérlek csatlakozz egy hangcsatornához! :no_entry: ");
- if (!voiceChannel) return msg.channel.send({embed: joinEmbed});
- const permissions = voiceChannel.permissionsFor(msg.client.user);
- if (!permissions.has('CONNECT')) {
- let botusername = bot.user.username
- let nyetEmbed = new Discord.RichEmbed()
- .setColor('#ff5930')
- .setAuthor(`${botusername} - ERROR`, bot.user.displayAvatarURL)
- .setThumbnail(bot.user.displayAvatarURL)
- .setDescription(":no_entry: Nem tudok csatlakozni az audio csatornához, ellenőrizze, hogy van megfelelő engedélyem! :no_entry: ");
- return msg.channel.send({embed: nyetEmbed});
- }
- if (!permissions.has('SPEAK')) {
- return msg.channel.send({embed: nyetEmbed});
- }
- if (url.match(/^https?:\/\/(www.youtube.com|youtube.com)\/playlist(.*)$/)) {
- const playlist = await youtube.getPlaylist(url);
- const videos = await playlist.getVideos();
- for (const video of Object.values(videos)) {
- const video2 = await youtube.getVideoByID(video.id); // eslint-disable-line no-await-in-loop
- await handleVideo(video2, msg, voiceChannel, true); // eslint-disable-line no-await-in-loop
- }
- return msg.channel.send(`✅ lejátszási lista: **${playlist.title}** felkerült a listára!`);
- } else {
- try {
- var video = await youtube.getVideo(url);
- } catch (error) {
- try {
- var videos = await youtube.searchVideos(searchString, 10);
- let index = 0;
- let botusername = bot.user.username
- let hEmbed = new Discord.RichEmbed()
- .setColor('#30acff')
- .setAuthor('Zene választás')
- .setThumbnail(bot.user.displayAvatarURL)
- .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.`)
- msg.channel.send({embed: hEmbed});
- // eslint-disable-next-line max-depth
- try {
- var response = await msg.channel.awaitMessages(msg2 => msg2.content > 0 && msg2.content < 11, {
- maxMatches: 1,
- time: 10000,
- errors: ['time']
- });
- } catch (err) {
- console.error(err);
- let ertekstopEmbed = new Discord.RichEmbed()
- .setColor('#ff5930')
- .setAuthor(`${botusername} - ERROR`, bot.user.displayAvatarURL)
- .setThumbnail(bot.user.displayAvatarURL)
- .setDescription(":no_entry: Nem adot meg értéket :no_entry: ");
- return msg.channel.send({embed: ertekstopEmbed});
- }
- const videoIndex = parseInt(response.first().content);
- var video = await youtube.getVideoByID(videos[videoIndex - 1].id);
- } catch (err) {
- console.error(err);
- return msg.channel.send('🆘 Nem kaptam keresési eredményeket.');
- }
- }
- return handleVideo(video, msg, voiceChannel);
- }
- } else if (command === 'skip') {
- let botusername = bot.user.username
- let notvoiceEmbed = new Discord.RichEmbed()
- .setColor('#f76252')
- .setAuthor(`${botusername} - ERROR`, bot.user.displayAvatarURL)
- .setThumbnail(bot.user.displayAvatarURL)
- .setDescription(":no_entry: Nem egy hangcsatornán vagy! :no_entry: ");
- let skipEmbed = new Discord.RichEmbed()
- .setColor('#f76252')
- .setAuthor(`${botusername} - ERROR`, bot.user.displayAvatarURL)
- .setThumbnail(bot.user.displayAvatarURL)
- .setDescription(":no_entry: Mivel nincs zene, nincs mit kihagyni! :no_entry: ");
- if (!msg.member.voiceChannel) return msg.channel.send({embed: notvoiceEmbed});
- if (!serverQueue) return msg.channel.send({embed: skipEmbed});
- serverQueue.connection.dispatcher.end('Az átugrás parancsot használták!');
- return undefined;
- } else if (command === 'stop') {
- let botusername = bot.user.username
- let stopEmbed = new Discord.RichEmbed()
- .setColor('#f76252')
- .setAuthor(`${botusername} - ERROR`, bot.user.displayAvatarURL)
- .setThumbnail(bot.user.displayAvatarURL)
- .setDescription(":no_entry: Mivel nincs zene, így semmi sem állhat meg. :no_entry: ");
- if (!msg.member.voiceChannel) return msg.channel.send({embed: skipEmbed});
- if (!serverQueue) return msg.channel.send({embed: stopEmbed});
- serverQueue.songs = [];
- serverQueue.connection.dispatcher.end('A Stop parancsot használták!');
- return undefined;
- } else if (command === 'np') {
- let botusername = bot.user.username
- let notplayEmbed = new Discord.RichEmbed()
- .setColor('#f76252')
- .setAuthor(`${botusername} - ERROR`, bot.user.displayAvatarURL)
- .setThumbnail(bot.user.displayAvatarURL)
- .setDescription(":no_entry: Nem játszik zenét. :no_entry: ");
- if (!serverQueue) return msg.channel.send({embed: notplayEmbed});
- return msg.channel.send(`🎶 Most játszik: **${serverQueue.songs[0].title}**`);
- } else if (command === 'list') {
- if (!serverQueue) return msg.channel.send({embed: notplayEmbed});
- let playlistEmbed = new Discord.RichEmbed()
- .setColor('#30acff')
- .setAuthor('lejátszási lista')
- .setThumbnail(bot.user.displayAvatarURL)
- .setDescription(`${serverQueue.songs.map(song => `**-** ${song.title}`).join('\n')}\n **🎶 Most játszik:** ${serverQueue.songs[0].title}`)
- return msg.channel.send({embed: playlistEmbed});
- } else if (command === 'pause') {
- if (serverQueue && serverQueue.playing) {
- serverQueue.playing = false;
- serverQueue.connection.dispatcher.pause();
- return msg.channel.send('⏸ Zene szüneteltetve!');
- }
- return msg.channel.send({embed: notplayEmbed});
- } else if (command === 'resume') {
- if (serverQueue && !serverQueue.playing) {
- serverQueue.playing = true;
- serverQueue.connection.dispatcher.resume();
- return msg.channel.send('▶ Zene folytatódik!');
- }
- return msg.channel.send({embed: notplayEmbed});
- }
- return undefined;
- });
- async function handleVideo(video, msg, voiceChannel, playlist = false) {
- const serverQueue = queue.get(msg.guild.id);
- //console.log(video);
- const song = {
- id: video.id,
- title: Util.escapeMarkdown(video.title),
- url: `https://www.youtube.com/watch?v=${video.id}`
- };
- if (!serverQueue) {
- const queueConstruct = {
- textChannel: msg.channel,
- voiceChannel: voiceChannel,
- connection: null,
- songs: [],
- volume: 5,
- playing: true
- };
- queue.set(msg.guild.id, queueConstruct);
- queueConstruct.songs.push(song);
- try {
- var connection = await voiceChannel.join();
- queueConstruct.connection = connection;
- play(msg.guild, queueConstruct.songs[0]);
- } catch (error) {
- console.error(`Nem tudtok csatlakozni a hangcsatornahoz: ${error}`);
- queue.delete(msg.guild.id);
- return msg.channel.send(`Nem tudok csatlakozni az audio csatornához: ${error}`);
- }
- } else {
- serverQueue.songs.push(song);
- console.log(serverQueue.songs);
- if (playlist) return undefined;
- else return msg.channel.send(`✅ **${song.title}** felkerült a listára!`);
- }
- return undefined;
- }
- function play(guild, song) {
- const serverQueue = queue.get(guild.id);
- if (!song) {
- serverQueue.voiceChannel.leave();
- queue.delete(guild.id);
- return;
- }
- console.log(serverQueue.songs);
- const dispatcher = serverQueue.connection.playStream(ytdl(song.url))
- .on('end', reason => {
- if (reason === 'A stream nem generál elég gyorsan.') console.log('A dal véget ért.');
- else console.log(reason);
- serverQueue.songs.shift();
- play(guild, serverQueue.songs[0]);
- })
- .on('error', error => console.error(error));
- dispatcher.setVolumeLogarithmic(serverQueue.volume / 5);
- serverQueue.textChannel.send(`🎶 Music Title: **${song.title}**`);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement