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:
+47
-12
@@ -1,7 +1,9 @@
|
||||
const { Command } = require('discord.js-commando');
|
||||
const Youtube = require('simple-youtube-api');
|
||||
const puppeteer = require('puppeteer');
|
||||
// const Youtube = require('simple-youtube-api');
|
||||
const ytdl = require('ytdl-core-discord');
|
||||
const youtube = new Youtube(process.env.YOUTUBE_API_KEY);
|
||||
const { URL } = require('url');
|
||||
// const youtube = new Youtube(process.env.YOUTUBE_API_KEY);
|
||||
|
||||
module.exports = class Play extends Command {
|
||||
constructor(client) {
|
||||
@@ -24,20 +26,53 @@ module.exports = class Play extends Command {
|
||||
}
|
||||
|
||||
async run(message, { query }) {
|
||||
const voiceChannel = message.member.voice.channel;
|
||||
try {
|
||||
const voiceChannel = message.member.voice.channel;
|
||||
|
||||
if (!voiceChannel) return message.reply('you need to join a channel!');
|
||||
if (!voiceChannel) return message.reply('you need to join a channel!');
|
||||
|
||||
const connection = await voiceChannel.join();
|
||||
const connection = await voiceChannel.join();
|
||||
|
||||
if (query.match(/^(http(s)?:\/\/)?((w){3}.)?youtu(be|.be)?(\.com)?\/.+/)) {
|
||||
const dispatcher = connection.play(await ytdl(query), { type: 'opus' });
|
||||
const link = new URL(query);
|
||||
|
||||
dispatcher.on('start', () => {
|
||||
return message.reply('youtube video is playing!');
|
||||
});
|
||||
} else {
|
||||
return message.reply('currently, i only support youtube video links!');
|
||||
if (['youtube.com', 'www.youtube.com'].includes(link.host)) {
|
||||
const dispatcher = connection.play(await ytdl(query), { type: 'opus' });
|
||||
|
||||
dispatcher.on('start', () => {
|
||||
return message.reply('youtube video is playing!');
|
||||
});
|
||||
} else if (['facebook.com', 'www.facebook.com'].includes(link.host)) {
|
||||
(async () => {
|
||||
try {
|
||||
const browser = await puppeteer.launch({
|
||||
timeout: 0
|
||||
});
|
||||
const page = await browser.newPage();
|
||||
page.setDefaultNavigationTimeout(0);
|
||||
page.setDefaultTimeout(0);
|
||||
await page.goto(query, { waitUntil: 'networkidle2' });
|
||||
const metaHandle = await page.$('meta[property="og:video:secure_url"]');
|
||||
const videoLink = await page.evaluate(meta => meta.getAttribute('content'), metaHandle);
|
||||
|
||||
const dispatcher = connection.play(videoLink);
|
||||
|
||||
dispatcher.on('start', () => {
|
||||
return message.reply('facebook video is playing!');
|
||||
});
|
||||
} catch (err) {
|
||||
throw err;
|
||||
}
|
||||
})();
|
||||
} else {
|
||||
const dispatcher = connection.play(query);
|
||||
|
||||
dispatcher.on('start', () => {
|
||||
return message.reply('video is playing!');
|
||||
});
|
||||
}
|
||||
} catch(err) {
|
||||
console.log(err);
|
||||
return message.reply('error occured playing video from link!');
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user