-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplayer.js
185 lines (160 loc) · 4.48 KB
/
player.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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
const ytdl = require("ytdl-core");
const { EventEmitter } = require("events");
const SpeakerPipeline = require("./speakerPipeline.js");
class Player extends EventEmitter {
/**
* A callback handler only accepting an error
* @callback emptyCallback
* @param {*} [error]
*/
/**
* A simple music player, playing YouTube videos to speakers
*/
constructor() {
super();
/**
* Music queue of the bot
* @type {string[]}
*/
this.queue = [];
/**
* Current play status of the player
* @type {boolean}
* @readonly
*/
this.playing = false;
/**
* Current pause status
* @type {boolean}
* @readonly
*/
this.paused = false;
/**
* The currently played song
* @type {object}
* @private
*/
this._currentSong = null;
}
/**
* Current song
* @returns {object} Object containing url and name
*/
get currentSong() {
return this._currentSong;
}
/**
* @private
*/
set currentSong(value) {
this._currentSong = value;
this.emit("songChanged", value);
}
/**
* Current volume
*/
get volume() {
return this._speakerPipeline ? this._speakerPipeline.volume : null;
}
/**
* Current volume
*/
set volume(value) {
if (this._speakerPipeline)
this._speakerPipeline.volume = value;
}
_free() {
if (this._speakerPipeline)
this._speakerPipeline.destroy();
if (this._ytdl)
this._ytdl.destroy();
this._speakerPipeline = undefined;
this._ytdl = undefined;
}
/**
* Add song to queue, currently only accepts YouTube URLs
* @param {string} url URL to song
* @param {emptyCallback} callback Called after song got added to the queue callback
*/
addSong(url, callback) {
if (true || ytdl.validateLink(url)) { // Worst fix in history
this.once("queueChanged", callback);
ytdl.getInfo(url).then(info => {
this.queue.push({ name: info.title, url: url, pictureUrl: info.iurlmaxres, description: info.description });
this.emit("queueChanged", this.queue);
});
}
}
/**
* Start the player
*/
start() {
if (this._speakerPipeline)
this._speakerPipeline.resume();
else if (this.queue.length > 0) {
let song = this.queue.shift();
this.emit("queueChanged", this.queue);
this._ytdl = ytdl(song.url, { quality: "lowest" });
this._speakerPipeline = new SpeakerPipeline(this._ytdl);
this._speakerPipeline.on("volumeChanged", volume => this.emit("volumeChanged", volume));
this._speakerPipeline.on("pause", () => this.emit("pause"));
this._speakerPipeline.on("resume", () => this.emit("resume"));
this.emit("volumeChanged", this._speakerPipeline.volume);
this._speakerPipeline.once("finish", () => this.next());
if (!this.playing) {
this.emit("start", song);
this.playing = true;
}
this.currentSong = song;
} else if (this.playing)
this.stop();
}
/**
* Pause the playback
*/
pause() {
this._speakerPipeline.pause();
}
/**
* Stop playback
*/
stop() {
if (this.playing) {
this._free();
this.currentSong = null;
this.playing = false;
this.emit("stop");
}
}
/**
* Play next song
*/
next() {
if (this.playing) {
this._free();
this.start();
} else {
if (this.queue.shift())
this.emit("queueChanged", this.queue);
}
}
/**
* Get various information for debugging purposes
* @returns {Object}
*/
getInfo() {
return {
ytdlBuffer: this._ytdl ? this._ytdl._readableState.buffer : null
};
}
/**
* Remove an item from the playback queue
* @param {number} index Index of item which is to be removed
*/
removeFromQueue(index) {
let elem = this.queue.splice(index, 1)[0];
this.emit("queueChanged", this.queue);
console.log("Song '" + elem.name + "' removed from queue");
}
}
module.exports = Player;