added changes to the play command

This commit is contained in:
Hazem Krimi
2020-05-08 15:50:44 +01:00
parent c53a6ab38c
commit 987fa57a0f
+93 -62
View File
@@ -1,5 +1,7 @@
const { spawn } = require('child_process'); // const { spawn } = require('child_process');
const ffmpeg = require('fluent-ffmpeg');
const { Command } = require('discord.js-commando'); const { Command } = require('discord.js-commando');
const { MessageEmbed } = require('discord.js');
const puppeteer = require('puppeteer'); const puppeteer = require('puppeteer');
const ytdl = require('ytdl-core-discord'); const ytdl = require('ytdl-core-discord');
const Youtube = require('simple-youtube-api'); const Youtube = require('simple-youtube-api');
@@ -33,7 +35,13 @@ module.exports = class Play extends Command {
try { try {
const voiceChannel = message.member.voice.channel; const voiceChannel = message.member.voice.channel;
if (!voiceChannel) return message.reply('you need to join a channel!'); if (!voiceChannel) {
const embed = new MessageEmbed().setColor('#ff0000').setTitle(':x: You need to join a voice channel first');
return await message.say({ embed });
}
const embed = new MessageEmbed().setColor('#000099').setTitle(':arrows_counterclockwise: Loading');
await message.say({ embed });
if (query.match(/^(?!.*\?.*\bv=)https:\/\/www\.youtube\.com\/.*\?.*\blist=.*$/)) { if (query.match(/^(?!.*\?.*\bv=)https:\/\/www\.youtube\.com\/.*\?.*\blist=.*$/)) {
const link = query.match(/^(?!.*\?.*\bv=)https:\/\/www\.youtube\.com\/.*\?.*\blist=.*$/)[0]; const link = query.match(/^(?!.*\?.*\bv=)https:\/\/www\.youtube\.com\/.*\?.*\blist=.*$/)[0];
@@ -45,7 +53,7 @@ module.exports = class Play extends Command {
const title = video.title; const title = video.title;
const by = video.channel.title; const by = video.channel.title;
const durationString = this.formatDuration(video.duration); const durationString = message.guild.formatDuration(video.duration);
const thumbnail = video.thumbnails.high.url; const thumbnail = video.thumbnails.high.url;
const data = { const data = {
type: 'youtube', type: 'youtube',
@@ -62,10 +70,12 @@ module.exports = class Play extends Command {
if (message.guild.music.isPlaying === false || message.guild.music.isPlaying === undefined) { if (message.guild.music.isPlaying === false || message.guild.music.isPlaying === undefined) {
message.guild.music.isPlaying = true; message.guild.music.isPlaying = true;
return this.play(message.guild.music.queue, message); return this.play(message.guild.music.queue, message);
} else {
return message.reply(`${video.title} added to queue`);
} }
}); });
if (message.guild.music.isPlaying) {
const embed = new MessageEmbed().setColor('#000099').setTitle(`:arrow_forward: ${playlist.title} (${playlistVideos.length} tracks) added to queue`);
return await message.say({ embed });
}
} else if (query.match(/^(http(s)?:\/\/)?((w){3}.)?youtu(be|.be)?(\.com)?\/\S+/)) { } else 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];
@@ -73,7 +83,7 @@ module.exports = class Play extends Command {
const video = await youtube.getVideoByID(id); const video = await youtube.getVideoByID(id);
const title = video.title; const title = video.title;
const by = video.channel.title; const by = video.channel.title;
const durationString = this.formatDuration(video.duration); const durationString = message.guild.formatDuration(video.duration);
const thumbnail = video.thumbnails.high.url; const thumbnail = video.thumbnails.high.url;
const data = { const data = {
@@ -92,7 +102,8 @@ module.exports = class Play extends Command {
message.guild.music.isPlaying = true; message.guild.music.isPlaying = true;
return this.play(message.guild.music.queue, message); return this.play(message.guild.music.queue, message);
} else { } else {
return message.reply(`${data.title} added to queue`); const embed = new MessageEmbed().setColor('#000099').setTitle(`:arrow_forward: ${data.title} added to queue`);
return await message.say({ embed });
} }
} else if (query.match(/^(http(s)?:\/\/)?((w){3}.)?facebook?(\.com)?\/\S+\/videos\/\S+/)) { } else if (query.match(/^(http(s)?:\/\/)?((w){3}.)?facebook?(\.com)?\/\S+\/videos\/\S+/)) {
const link = query.match(/^(http(s)?:\/\/)?((w){3}.)?facebook?(\.com)?\/\S+\/videos\/\S+/)[0]; const link = query.match(/^(http(s)?:\/\/)?((w){3}.)?facebook?(\.com)?\/\S+\/videos\/\S+/)[0];
@@ -122,7 +133,7 @@ module.exports = class Play extends Command {
}); });
} }
const durationString = durationArr ? this.formatDuration({ hours: durationArr[0], minutes: durationArr[1], seconds: durationArr[2] + 1 }) : 'Live Stream'; const durationString = durationArr ? message.guild.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 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 data = { const data = {
@@ -140,48 +151,58 @@ module.exports = class Play extends Command {
message.guild.music.isPlaying = true; message.guild.music.isPlaying = true;
return this.play(message.guild.music.queue, message); return this.play(message.guild.music.queue, message);
} else { } else {
return message.reply(`${data.title} added to queue`); const embed = new MessageEmbed().setColor('#000099').setTitle(`:arrow_forward: ${data.title} added to queue`);
return await message.say({ embed });
} }
} 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 ffmpeg = spawn('ffmpeg', ['-i', link]);
const title = link.split('/')[link.split('/').length - 1].split('.')[0]; const title = link.split('/')[link.split('/').length - 1].split('.')[0];
let duration;
ffmpeg.ffprobe(link, async(err, metaData) => {
ffmpeg.stderr.on('data', err => { if (err) throw err;
if (err.toString().match(/Duration:\s(\d){2}:(\d){2}:(\d){2}/)) duration = err.toString().match(/Duration:\s(\d){2}:(\d){2}:(\d){2}/).toString().replace(/Duration:\s/, ''); else {
if (duration) { const duration = {
const durationArr = duration.toString().split(':').map(time => parseInt(time)); hours: new Date(Math.ceil(metaData.format.duration) * 1000).getUTCHours(),
duration = { hours: durationArr[0], minutes: durationArr[1], seconds: durationArr[2] + 1, string: duration }; minutes: new Date(Math.ceil(metaData.format.duration) * 1000).getUTCMinutes(),
ffmpeg.kill('SIGHUP'); seconds: new Date(Math.ceil(metaData.format.duration) * 1000).getSeconds(),
string: message.guild.formatDuration({
hours: new Date(Math.ceil(metaData.format.duration) * 1000).getUTCHours(),
minutes: new Date(Math.ceil(metaData.format.duration) * 1000).getUTCMinutes(),
seconds: new Date(Math.ceil(metaData.format.duration) * 1000).getSeconds()
})
};
const data = {
type: 'other',
link,
title,
duration,
voiceChannel
};
if (data) {
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 {
const embed = new MessageEmbed().setColor('#000099').setTitle(`:arrow_forward: ${data.title} added to queue`);
return await message.say({ embed });
}
}
} }
}); });
const data = {
type: 'other',
link,
title,
duration,
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`);
}
} else { } else {
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) {
const embed = new MessageEmbed().setColor('#ff0000').setTitle(':x: Nothing found');
return await message.say({ embed });
}
const video = await youtube.getVideoByID(videos[0].raw.id.videoId); const video = await youtube.getVideoByID(videos[0].raw.id.videoId);
const title = video.title; const title = video.title;
const by = video.channel.title; const by = video.channel.title;
const durationString = this.formatDuration(video.duration); const durationString = message.guild.formatDuration(video.duration);
const thumbnail = video.thumbnails.high.url; const thumbnail = video.thumbnails.high.url;
const data = { const data = {
type: 'search', type: 'search',
@@ -199,18 +220,23 @@ module.exports = class Play extends Command {
message.guild.music.isPlaying = true; message.guild.music.isPlaying = true;
return this.play(message.guild.music.queue, message); return this.play(message.guild.music.queue, message);
} else { } else {
return message.reply(`${data.title} added to queue`); const embed = new MessageEmbed().setColor('#000099').setTitle(`:arrow_forward: ${data.title} added to queue`);
return await message.say({ embed });
} }
} }
} catch(err) { } catch(err) {
console.error(err); console.error(err);
return message.reply('cannot play what you requested!'); const embed = new MessageEmbed().setColor('#ff0000').setTitle(':x: Track is private or I just can\'t play this for some other reason');
return message.say({ embed });
} }
} }
play = async(queue, messsage) => { play = async(queue, message) => {
try { try {
if (queue.length === 0) return messsage.reply('queue is empty'); if (queue.length === 0) {
const embed = new MessageEmbed().setColor('#ff0000').setTitle(':x: Error occured, if you are my creator please fix me soon');
return await message.say({ embed });
}
const voiceChannel = queue[0].voiceChannel; const voiceChannel = queue[0].voiceChannel;
const connection = await voiceChannel.join(); const connection = await voiceChannel.join();
@@ -223,38 +249,43 @@ module.exports = class Play extends Command {
case 'other': { dispatcher = connection.play(queue[0].link); break; } case 'other': { dispatcher = connection.play(queue[0].link); break; }
} }
dispatcher.on('start', () => { dispatcher.on('start', async() => {
messsage.guild.music.dispatcher = dispatcher; message.guild.music.nowPlaying = queue[0];
messsage.guild.music.nowPlaying = queue[0]; message.guild.startCounter(message);
dispatcher.setVolume(messsage.guild.music.volume); message.guild.music.dispatcher = dispatcher;
messsage.reply(`${queue[0].title} is playing`); dispatcher.setVolume(message.guild.music.volume);
const embed = new MessageEmbed().setColor('#000099').setTitle(`:arrow_forward: Play`).addField('Now playing', queue[0].title);
if (queue[0].type === 'youtube' || queue[0].type === 'search') embed.setThumbnail(queue[0].thumbnail);
if (queue[0].type === 'youtube' || queue[0].type === 'search' || queue[0].type === 'facebook') embed.addField('By', queue[0].by);
embed.addField('Duration', queue[0].duration.string);
await message.say({ embed });
return queue.shift(); return queue.shift();
}); });
dispatcher.on('finish', () => { dispatcher.on('finish', async() => {
if (queue.length >= 1) return this.play(queue, messsage); if (queue.length >= 1) return this.play(queue, message);
else { else {
messsage.guild.music.isPlaying = false; message.guild.music.isPlaying = false;
messsage.say('queue ended'); message.guild.music.nowPlaying = null;
return voiceChannel.leave(); message.guild.music.dispatcher = null;
voiceChannel.leave();
const embed = new MessageEmbed().setColor('#000099').setTitle(':musical_note: Queue ended');
return await message.say({ embed });
} }
}); });
dispatcher.on('error', err => { dispatcher.on('error', err => {
messsage.guild.music.queue = []; message.guild.music.queue = [];
messsage.guild.music.isPlaying = false; message.guild.music.isPlaying = false;
messsage.guild.music.nowPlaying = false; message.guild.music.nowPlaying = false;
messsage.say('error occured'); message.guild.music.dispatcher = null;
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!'); const embed = new MessageEmbed().setColor('#ff0000').setTitle(':x: Error occured, if you are my creator please fix me soon');
return message.say({ embed });
} }
} }
formatDuration = durationObject => {
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'}`;
}
} }