improved the queue system

This commit is contained in:
Hazem Krimi
2020-05-06 11:59:30 +01:00
parent 866403f80a
commit d0b635ade3
+51 -19
View File
@@ -34,8 +34,6 @@ module.exports = class Play extends Command {
if (!voiceChannel) return message.reply('you need to join a channel!'); if (!voiceChannel) return message.reply('you need to join a channel!');
// TODO change if to switch
if (query.match(/^(http(s)?:\/\/)?((w){3}.)?youtu(be|.be)?(\.com)?\/\S+/)) { if (query.match(/^(http(s)?:\/\/)?((w){3}.)?youtu(be|.be)?(\.com)?\/\S+/)) {
const link = query.match(/^(http(s)?:\/\/)?((w){3}.)?youtu(be|.be)?(\.com)?\/\S+/)[0]; const link = query.match(/^(http(s)?:\/\/)?((w){3}.)?youtu(be|.be)?(\.com)?\/\S+/)[0];
const id = link.replace(/(>|<)/gi, '').split(/(vi\/|v=|\/v\/|youtu\.be\/|\/embed\/)/)[2].split(/[^0-9a-z_\-]/i)[0]; const id = link.replace(/(>|<)/gi, '').split(/(vi\/|v=|\/v\/|youtu\.be\/|\/embed\/)/)[2].split(/[^0-9a-z_\-]/i)[0];
@@ -72,16 +70,33 @@ module.exports = class Play extends Command {
page.setDefaultNavigationTimeout(0); page.setDefaultNavigationTimeout(0);
page.setDefaultTimeout(0); page.setDefaultTimeout(0);
await page.goto(link, { waitUntil: 'networkidle2' }); await page.goto(link, { waitUntil: 'networkidle2' });
const titleHandle = await page.$('title');
const metaHandle = await page.$('meta[property="og:video:url"]'); const metaHandle = await page.$('meta[property="og:video:url"]');
const durationString = (await page.content()).match(/mediaPresentationDuration=\\"\S+\\"/) || false;
const durationArr = durationString ? durationString.toString().replace(/mediaPresentationDuration=\\"/, '').replace(/\\"/, '').trim().split(/\D/).slice(2, 5).map(time => parseInt(time)) : false;
const duration = durationArr ? this.formatDuration({ hours: durationArr[0], minutes: durationArr[1], seconds: durationArr[2] + 1 }) : 'Live Stream';
const title = await page.evaluate(title => title.innerText.replace(/\s\|\sfacebook/i, ''), titleHandle);
const videoLink = await page.evaluate(meta => meta.getAttribute('content'), metaHandle); const videoLink = await page.evaluate(meta => meta.getAttribute('content'), metaHandle);
// const dispatcher = connection.play(videoLink); const data = {
type: 'facebook',
link: videoLink,
title,
duration,
voiceChannel
};
// dispatcher.on('start', () => { message.guild.music.queue.push(data);
// return message.reply('facebook video is playing!');
// }); if (message.guild.music.isPlaying === false || message.guild.music.isPlaying === undefined) {
message.guild.music.isPlaying = true;
return this.play(message.guild.music.queue, message);
} else {
return message.reply(`${data.title} added to queue`);
}
} else if (query.match(/^(http(s)?:\/\/)?((w){3}\S)?\S+(\.)\S+\/\S+\.(\S){3}/)) { } else if (query.match(/^(http(s)?:\/\/)?((w){3}\S)?\S+(\.)\S+\/\S+\.(\S){3}/)) {
const link = query.match(/^(http(s)?:\/\/)?((w){3}\S)?\S+(\.)\S+\/\S+\.(\S){3}/)[0]; // const link = query.match(/^(http(s)?:\/\/)?((w){3}\S)?\S+(\.)\S+\/\S+\.(\S){3}/)[0];
// const dispatcher = connection.play(link); // const dispatcher = connection.play(link);
@@ -92,11 +107,29 @@ module.exports = class Play extends Command {
const videos = await youtube.searchVideos(query, 1); const videos = await youtube.searchVideos(query, 1);
if (!videos.length === 1) return message.reply('nothing found!'); if (!videos.length === 1) return message.reply('nothing found!');
// const dispatcher = connection.play(await ytdl(`https://www.youtube.com/watch?v=${videos[0].raw.id.videoId}`), { type: 'opus' }); const video = await youtube.getVideoByID(videos[0].raw.id.videoId);
// dispatcher.on('start', () => { const title = video.title;
// return message.reply('youtube video is playing!'); const duration = this.formatDuration(video.duration);
// }); const thumbnail = video.thumbnails.high.url;
const data = {
type: 'youtube-search',
link: `https://www.youtube.com/watch?v=${video.id}`,
title,
duration: duration !== '00:00:00' ? duration : 'Live Stream',
thumbnail,
voiceChannel
};
message.guild.music.queue.push(data);
if (message.guild.music.isPlaying === false || message.guild.music.isPlaying === undefined) {
message.guild.music.isPlaying = true;
return this.play(message.guild.music.queue, message);
} else {
return message.reply(`${data.title} added to queue`);
}
} }
} catch(err) { } catch(err) {
console.error(err); console.error(err);
@@ -108,11 +141,13 @@ module.exports = class Play extends Command {
try { try {
const voiceChannel = queue[0].voiceChannel; const voiceChannel = queue[0].voiceChannel;
const connection = await voiceChannel.join(); const connection = await voiceChannel.join();
let dispatcher;
// TODO change if to switch switch (queue[0].type) {
case 'youtube': { dispatcher = connection.play(await ytdl(queue[0].link, { quality: 'highestaudio' }), { type: 'opus' }); break; }
if (queue[0].type === 'youtube') { case 'facebook': { dispatcher = connection.play(queue[0].link); break; }
const dispatcher = connection.play(await ytdl(queue[0].link, { quality: 'highestaudio' }), { type: 'opus' }); case 'youtube-search': { dispatcher = connection.play(await ytdl(queue[0].link), { type: 'opus' }); break; }
}
dispatcher.on('start', () => { dispatcher.on('start', () => {
messsage.guild.music.dispatcher = dispatcher; messsage.guild.music.dispatcher = dispatcher;
@@ -139,7 +174,6 @@ module.exports = class Play extends Command {
voiceChannel.leave(); voiceChannel.leave();
throw err; throw err;
}); });
}
} catch(err) { } catch(err) {
console.error(err); console.error(err);
return message.reply('cannot play what you requested!'); return message.reply('cannot play what you requested!');
@@ -147,8 +181,6 @@ module.exports = class Play extends Command {
} }
formatDuration = durationObject => { formatDuration = durationObject => {
const duration = `${durationObject.hours < 10 ? '0' + durationObject.hours : durationObject.hours ? durationObject.hours : '00'}:${durationObject.minutes < 10 ? '0' + durationObject.minutes : durationObject.minutes ? durationObject.minutes : '00'}:${durationObject.seconds < 10 ? '0' + durationObject.seconds : durationObject.seconds ? durationObject.seconds : '00'}`; return `${durationObject.hours < 10 ? '0' + durationObject.hours : durationObject.hours ? durationObject.hours : '00'}:${durationObject.minutes < 10 ? '0' + durationObject.minutes : durationObject.minutes ? durationObject.minutes : '00'}:${durationObject.seconds < 10 ? '0' + durationObject.seconds : durationObject.seconds ? durationObject.seconds : '00'}`;
return duration;
} }
} }