mirror of
https://github.com/hazemKrimi/discord-bot.git
synced 2026-05-01 18:30:25 +00:00
moved play logic to the main guild class
This commit is contained in:
@@ -1,6 +1,8 @@
|
||||
const { CommandoClient } = require('discord.js-commando');
|
||||
const { Structures } = require('discord.js');
|
||||
const { MessageEmbed } = require('discord.js');
|
||||
const path = require('path');
|
||||
const ytdl = require('ytdl-core-discord');
|
||||
|
||||
Structures.extend('Guild', Guild => {
|
||||
class MusicGuild extends Guild {
|
||||
@@ -18,6 +20,64 @@ Structures.extend('Guild', Guild => {
|
||||
};
|
||||
}
|
||||
|
||||
play = async (queue, message) => {
|
||||
try {
|
||||
if (queue.length === 0) {
|
||||
const embed = new MessageEmbed().setColor('#ff0000').setTitle(`:x: Error occured: ${err.message}`);
|
||||
return await message.say({ embed });
|
||||
}
|
||||
|
||||
const voiceChannel = queue[0].voiceChannel;
|
||||
const connection = await voiceChannel.join();
|
||||
let dispatcher;
|
||||
|
||||
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 'search': { dispatcher = connection.play(await ytdl(queue[0].link), { type: 'opus' }); break; }
|
||||
case 'other': { dispatcher = connection.play(queue[0].link); break; }
|
||||
}
|
||||
|
||||
dispatcher.on('start', async () => {
|
||||
message.guild.music.nowPlaying = queue[0];
|
||||
message.guild.startCounter(message);
|
||||
message.guild.music.dispatcher = dispatcher;
|
||||
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();
|
||||
});
|
||||
|
||||
dispatcher.on('finish', async () => {
|
||||
if (queue.length >= 1) return this.play(queue, message);
|
||||
else {
|
||||
message.guild.music.isPlaying = false;
|
||||
message.guild.music.nowPlaying = null;
|
||||
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 => {
|
||||
message.guild.music.queue = [];
|
||||
message.guild.music.isPlaying = false;
|
||||
message.guild.music.nowPlaying = false;
|
||||
message.guild.music.dispatcher = null;
|
||||
voiceChannel.leave();
|
||||
throw err;
|
||||
});
|
||||
} catch (err) {
|
||||
console.error(err);
|
||||
const embed = new MessageEmbed().setColor('#ff0000').setTitle(`:x: Error occured: ${err.message}`);
|
||||
return message.say({ embed });
|
||||
}
|
||||
}
|
||||
|
||||
startCounter = message => {
|
||||
if (!message.guild.music.nowPlaying.playingFor) message.guild.music.nowPlaying.playingFor = { hours: 0, minutes: 0, seconds: 0, string: '00:00:00' };
|
||||
|
||||
|
||||
Reference in New Issue
Block a user