added queue system

This commit is contained in:
Hazem Krimi
2020-05-05 09:19:02 +01:00
parent ff0b556347
commit 866403f80a
5 changed files with 138 additions and 43 deletions
+6 -2
View File
@@ -8,11 +8,15 @@ module.exports = class JoinCommand extends Command {
group: 'music', group: 'music',
description: 'joins a voice channel', description: 'joins a voice channel',
aliases: ['summon'], aliases: ['summon'],
guildOnly: true guildOnly: true,
throttling: {
usages: 1,
duration: 5
}
}); });
} }
async run(message) { run = async message => {
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) return message.reply('you need to join a channel!');
+6 -2
View File
@@ -7,11 +7,15 @@ module.exports = class Leave extends Command {
memberName: 'leave', memberName: 'leave',
group: 'music', group: 'music',
description: 'leaves a voice channel', description: 'leaves a voice channel',
guildOnly: true guildOnly: true,
throttling: {
usages: 1,
duration: 5
}
}); });
} }
async run(message) { run = message => {
if (!message.member.voice.channel) return message.reply('you need to join a channel!'); if (!message.member.voice.channel) return message.reply('you need to join a channel!');
const voiceChannel = message.member.voice.channel; const voiceChannel = message.member.voice.channel;
+100 -36
View File
@@ -20,71 +20,135 @@ module.exports = class Play extends Command {
type: 'string', type: 'string',
validate: query => query.length > 0 validate: query => query.length > 0
} }
] ],
throttling: {
usages: 1,
duration: 5
}
}); });
} }
async run(message, { query }) { run = async(message, { query }) => {
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) return message.reply('you need to join a channel!');
const connection = await voiceChannel.join(); // 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 dispatcher = connection.play(await ytdl(link), { type: 'opus' }); const video = await youtube.getVideoByID(id);
const title = video.title;
const duration = this.formatDuration(video.duration);
const thumbnail = video.thumbnails.high.url;
dispatcher.on('start', () => { const data = {
return message.reply('youtube video is playing!'); type: 'youtube',
}); link,
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`);
}
} 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];
(async () => { const browser = await puppeteer.launch({
try { timeout: 0
const browser = await puppeteer.launch({ });
timeout: 0 const page = await browser.newPage();
}); page.setDefaultNavigationTimeout(0);
const page = await browser.newPage(); page.setDefaultTimeout(0);
page.setDefaultNavigationTimeout(0); await page.goto(link, { waitUntil: 'networkidle2' });
page.setDefaultTimeout(0); const metaHandle = await page.$('meta[property="og:video:url"]');
await page.goto(link, { waitUntil: 'networkidle2' }); const videoLink = await page.evaluate(meta => meta.getAttribute('content'), metaHandle);
const metaHandle = await page.$('meta[property="og:video:url"]');
const videoLink = await page.evaluate(meta => meta.getAttribute('content'), metaHandle);
const dispatcher = connection.play(videoLink); // const dispatcher = connection.play(videoLink);
dispatcher.on('start', () => { // dispatcher.on('start', () => {
return message.reply('facebook video is playing!'); // return message.reply('facebook video is playing!');
}); // });
} catch (err) {
message.reply('cannot play what you requested!');
throw err;
}
})();
} 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);
dispatcher.on('start', () => { // dispatcher.on('start', () => {
return message.reply('playing!'); // return message.reply('playing!');
}); // });
} 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) return message.reply('nothing found!');
const dispatcher = connection.play(await ytdl(`https://www.youtube.com/watch?v=${videos[0].raw.id.videoId}`), { type: 'opus' });
dispatcher.on('start', () => { // const dispatcher = connection.play(await ytdl(`https://www.youtube.com/watch?v=${videos[0].raw.id.videoId}`), { type: 'opus' });
return message.reply('youtube video is playing!');
}); // dispatcher.on('start', () => {
// return message.reply('youtube video is playing!');
// });
} }
} catch(err) { } catch(err) {
console.log(err); console.error(err);
return message.reply('cannot play what you requested!'); return message.reply('cannot play what you requested!');
} }
} }
play = async(queue, messsage) => {
try {
const voiceChannel = queue[0].voiceChannel;
const connection = await voiceChannel.join();
// TODO change if to switch
if (queue[0].type === 'youtube') {
const dispatcher = connection.play(await ytdl(queue[0].link, { quality: 'highestaudio' }), { type: 'opus' });
dispatcher.on('start', () => {
messsage.guild.music.dispatcher = dispatcher;
messsage.guild.music.nowPlaying = queue[0];
dispatcher.setVolume(messsage.guild.music.volume);
messsage.reply(`${queue[0].title} is playing`);
return queue.shift();
});
dispatcher.on('finish', () => {
if (queue.length >= 1) return this.play(queue, messsage);
else {
messsage.guild.music.isPlaying = false;
messsage.say('queue ended');
return voiceChannel.leave();
}
});
dispatcher.on('error', err => {
messsage.guild.music.queue = [];
messsage.guild.music.isPlaying = false;
messsage.guild.music.nowPlaying = false;
messsage.say('error occured');
voiceChannel.leave();
throw err;
});
}
} catch(err) {
console.error(err);
return message.reply('cannot play what you requested!');
}
}
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 duration;
}
} }
+6 -2
View File
@@ -7,11 +7,15 @@ module.exports = class Play extends Command {
memberName: 'stop', memberName: 'stop',
group: 'music', group: 'music',
description: 'stops the player and leaves the channel', description: 'stops the player and leaves the channel',
guildOnly: true guildOnly: true,
throttling: {
usages: 1,
duration: 5
}
}); });
} }
async run(message) { run = message => {
if (!message.member.voice.channel) return message.reply('you need to join a channel!'); if (!message.member.voice.channel) return message.reply('you need to join a channel!');
const voiceChannel = message.member.voice.channel; const voiceChannel = message.member.voice.channel;
+20 -1
View File
@@ -1,10 +1,29 @@
require('dotenv').config(); require('dotenv').config();
const { CommandoClient } = require('discord.js-commando'); const { CommandoClient } = require('discord.js-commando');
const { Structures } = require('discord.js');
const path = require('path'); const path = require('path');
Structures.extend('Guild', Guild => {
class MusicGuild extends Guild {
constructor(client, data) {
super(client, data);
this.music = {
queue: [],
isPlaying: false,
nowPlaying: null,
volume: 1,
dispatcher: null
};
}
}
return MusicGuild;
});
const client = new CommandoClient({ const client = new CommandoClient({
commandPrefix: 'b.' commandPrefix: 'b.',
owner: '321673699436527617'
}); });
client.registry client.registry