mirror of
https://github.com/hazemKrimi/discord-bot.git
synced 2026-05-01 18:30:25 +00:00
improved the play command
This commit is contained in:
+56
-15
@@ -1,3 +1,4 @@
|
||||
const { spawn } = require('child_process');
|
||||
const { Command } = require('discord.js-commando');
|
||||
const puppeteer = require('puppeteer');
|
||||
const ytdl = require('ytdl-core-discord');
|
||||
@@ -40,6 +41,7 @@ module.exports = class Play extends Command {
|
||||
|
||||
const video = await youtube.getVideoByID(id);
|
||||
const title = video.title;
|
||||
const by = video.channel.title;
|
||||
const duration = this.formatDuration(video.duration);
|
||||
const thumbnail = video.thumbnails.high.url;
|
||||
|
||||
@@ -47,7 +49,8 @@ module.exports = class Play extends Command {
|
||||
type: 'youtube',
|
||||
link,
|
||||
title,
|
||||
duration: duration !== '00:00:00' ? duration : 'Live Stream',
|
||||
by,
|
||||
duration: duration !== '00:00:00' ? { hours: video.duration.hours, minutes: video.duration.minutes, seconds: video.duration.seconds, string: duration } : 'Live Stream',
|
||||
thumbnail,
|
||||
voiceChannel
|
||||
};
|
||||
@@ -67,21 +70,66 @@ module.exports = class Play extends Command {
|
||||
timeout: 0
|
||||
});
|
||||
const page = await browser.newPage();
|
||||
|
||||
page.setDefaultNavigationTimeout(0);
|
||||
page.setDefaultTimeout(0);
|
||||
await page.goto(link, { waitUntil: 'networkidle2' });
|
||||
|
||||
const titleHandle = await page.$('title');
|
||||
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';
|
||||
let durationCode = (await page.content()).match(/mediaPresentationDuration=\\"\S+\\"/) || (await page.content()).match(/"duration":"\S+"/) || false;
|
||||
let durationArr = [0, 0, 0];
|
||||
|
||||
if (durationCode && durationCode.toString().match(/mediaPresentationDuration=\\"\S+\\"/)) {
|
||||
durationArr = durationCode.toString().replace(/mediaPresentationDuration=\\"/, '').replace(/\\"/, '').trim().split(/\D/).slice(2, 5).map(time => parseInt(time));
|
||||
} else if (durationCode && durationCode.toString().match(/"duration":"\S+"/)) {
|
||||
durationCode.toString().match(/"duration":"T(\d+H)?(\d+M)?(\d+S")?/)[0].replace(/"duration":"/, '').replace(/"/, '').trim().match(/(\d+H)|(\d+M)|(\d+S)/g).forEach(time => {
|
||||
if (time.toString().match(/\d+H/)) durationArr[0] = parseInt(time.match(/\d+/));
|
||||
if (time.toString().match(/\d+M/)) durationArr[1] = parseInt(time.match(/\d+/));
|
||||
if (time.toString().match(/\d+S/)) durationArr[2] = parseInt(time.match(/\d+/));
|
||||
});
|
||||
}
|
||||
|
||||
const durationString = 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 data = {
|
||||
type: 'facebook',
|
||||
link: videoLink,
|
||||
title: title.split(' - ')[1],
|
||||
by: title.split(' - ')[0],
|
||||
duration: durationString !== 'Live Stream' ? { hours: durationArr[0], minutes: durationArr[1], seconds: durationArr[2] + 1, string: durationString } : durationString,
|
||||
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 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 ffmpeg = spawn('ffmpeg', ['-i', link]);
|
||||
|
||||
const title = link.split('/')[link.split('/').length - 1].split('.')[0];
|
||||
let duration;
|
||||
|
||||
ffmpeg.stderr.on('data', 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/, '');
|
||||
if (duration) {
|
||||
const durationArr = duration.toString().split(':').map(time => parseInt(time));
|
||||
duration = { hours: durationArr[0], minutes: durationArr[1], seconds: durationArr[2] + 1, string: duration };
|
||||
ffmpeg.kill('SIGHUP');
|
||||
}
|
||||
});
|
||||
|
||||
const data = {
|
||||
type: 'other',
|
||||
link,
|
||||
title,
|
||||
duration,
|
||||
voiceChannel
|
||||
@@ -95,14 +143,6 @@ module.exports = class Play extends Command {
|
||||
} else {
|
||||
return message.reply(`${data.title} added to queue`);
|
||||
}
|
||||
} 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 dispatcher = connection.play(link);
|
||||
|
||||
// dispatcher.on('start', () => {
|
||||
// return message.reply('playing!');
|
||||
// });
|
||||
} else {
|
||||
const videos = await youtube.searchVideos(query, 1);
|
||||
if (!videos.length === 1) return message.reply('nothing found!');
|
||||
@@ -114,7 +154,7 @@ module.exports = class Play extends Command {
|
||||
const thumbnail = video.thumbnails.high.url;
|
||||
|
||||
const data = {
|
||||
type: 'youtube-search',
|
||||
type: 'search',
|
||||
link: `https://www.youtube.com/watch?v=${video.id}`,
|
||||
title,
|
||||
duration: duration !== '00:00:00' ? duration : 'Live Stream',
|
||||
@@ -146,7 +186,8 @@ module.exports = class Play extends Command {
|
||||
switch (queue[0].type) {
|
||||
case 'youtube': { dispatcher = connection.play(await ytdl(queue[0].link, { quality: 'highestaudio' }), { type: 'opus' }); break; }
|
||||
case 'facebook': { dispatcher = connection.play(queue[0].link); break; }
|
||||
case 'youtube-search': { dispatcher = connection.play(await ytdl(queue[0].link), { type: 'opus' }); break; }
|
||||
case 'search': { dispatcher = connection.play(await ytdl(queue[0].link), { type: 'opus' }); break; }
|
||||
case 'other': { dispatcher = connection.play(queue[0].link); break; }
|
||||
}
|
||||
|
||||
dispatcher.on('start', () => {
|
||||
|
||||
Reference in New Issue
Block a user