-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmusic.js
98 lines (89 loc) · 2.93 KB
/
music.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
const Discord = require("discord.js");
const ytdl = require("ytdl-core");
class MusicPlayer {
/*
textChannel: message.channel,
// voiceChannel: voiceChannel,
// connection: null,
// songs: [],
// volume: 5,
// playing: true,
*/
constructor(guild, message, bot){
if (!message.member.voiceChannel) return message.channel.send(':x: Join a voice channel first!');
this.guild = guild
this.queue = [];
this.voiceChannel;
this.textChannel = message.channel;
this.volume = 2;
this.playing = false;
this.message = message;
this.bot = bot;
return this;
}
async play(message, args){
if (!args[0]) return message.channel.send(':x: Use a YouTube URL');
let validate = await ytdl.validateURL(args[0]);
if (!validate) return message.channel.send(':x: Use a valid YouTube URL');
console.log("validated");
this.options = {
seek: 0,
volume: this.volume / 10
};
if(!this.voiceConnection){
this.voiceConnection = await message.member.voiceChannel.join()
}
await this.addToQueue(args[0]);
this.message = message;
await this.playNextInQueue();
}
async playNextInQueue(){
if(this.queue[0]){
if(this.playing) return;
this.playing = true;
var stream = ytdl(this.queue[0].url, {
filter: "audioonly",
quality: "highest"
});
let musicEmbed = new Discord.RichEmbed()
.setTitle("Now playing")
.setDescription(`**${this.queue[0].title}**`)
.setColor("#FFFFFF")
.setTimestamp(new Date())
this.message.channel.send(musicEmbed);
if(this.stream){
if(!this.stream.destroyed){
await this.stream.destroy();
}
}
this.stream = await this.voiceConnection.playStream(stream, this.options)
.on('end', () => {
this.queue.shift();
this.playing = false;
this.playNextInQueue(this.options);
});
} else {
await this.stream.destroy();
this.voiceConnection.disconnect();
this.voiceConnection = undefined;
this.queue = [];
}
}
async addToQueue(url){
const songInfo = await ytdl.getInfo(url);
const song = {
title: songInfo.title,
url: songInfo.video_url,
};
this.queue.push(song);
let queueEmbed = new Discord.RichEmbed()
.setTitle("Added to queue")
.setColor("#FFFFFF")
.setDescription(`${song.title}`)
this.message.channel.send(queueEmbed);
}
async skip(){
this.stream.end();
}
}
module.exports = MusicPlayer;