forked from rnd42/odinbot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplayer.js
359 lines (294 loc) · 10.2 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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
// -- VOICE PLAYER AND SHIT, GOOD FOR PLAYING AND OTHER JUNK --
//--====================================================================--
// const YoutubeDL = require('youtube-dl');
const ytdl = require('ytdl-core');
const YoutubeParser = require('youtube-parser');
const Request = require('request');
const cheerio = require('cheerio');
const playlists = require('./playlists.js');
const radio = require("radio-stream");
const botParent = require('./odin.js');
const util = require('util')
// const NPemote = {text:":cd:",lookup:false}
const NPemote = {text:"wl4disc",lookup:true}
class ThePlayer {
constructor()
{
this.defaultVolume = 0.5;
this.playVolume = this.defaultVolume;
this.playlist = [];
this.isPlaying = false;
this.isStreaming = false;
this.targetChannel = undefined;
this.retryCounter = 0;
// -- Variables for voice connection
this.voiceConnection = undefined;
this.voiceStream = undefined;
}
inPlaylist() {return this.isPlaying;}
findEmoji(theguild, eid) {return theguild.emojis.find("name", eid.toString());}
// -- Initializes the bot and sets the channel to send shit to
initialize(guild,connection)
{
this.targetChannel = guild.defaultChannel;
this.voiceConnection = connection;
this.voiceConnection.on("error", function (err) {console.log("ERROR WITH STREAM: "+err);});
console.log("**VOICE CONNECTION INITIALIZED**");
}
// -- PLAY A YOUTUBE VIDEO --
playYoutube(TS)
{
this.voiceStream = this.voiceConnection.playStream( TS, {seek:0,volume:this.playVolume} );
// -- FIND THE EMOTE
var emt = NPemote.text;
if (NPemote.lookup){emt = this.findEmoji(this.targetChannel.guild,NPemote.text);}
if (botParent.getMC() != undefined){botParent.getMC().sendMessage(emt+" **Now playing:** *"+this.playlist[0].title+"*. `"+this.playlist[0].url+"`");}
this.voiceStream.on("end",(function(){
console.log("Stream ended!");
this.playlist.shift();
if (this.playlist.length > 0){this.playlistPlay();}
else{this.targetChannel.sendMessage("The playlist has ended."); this.isPlaying = false; botParent.swapStatus();}
}).bind(this));
}
// -- FORCEFULLY PLAY A LOCAL SOUND
playLocalForced(sound,vol)
{
if (this.playlist.length <= 0){this.voiceStream = this.voiceConnection.playFile( "./sounds/"+sound, {seek:0,volume:vol} );}
}
// -- PLAY A LOCAL TRACK --
playLocal()
{
this.voiceStream = this.voiceConnection.playFile( "./sounds/music/"+this.playlist[0].url, {seek:0,volume:this.playVolume} );
// -- FIND THE EMOTE
var emt = NPemote.text;
if (NPemote.lookup){emt = this.findEmoji(this.targetChannel.guild,NPemote.text);}
if (botParent.getMC() != undefined){botParent.getMC().sendMessage(emt+" **Now playing:** *"+this.playlist[0].title+"*. `"+this.playlist[0].url+"`");}
this.voiceStream.on("end",(function(){
console.log("Stream ended!");
this.playlist.shift();
if (this.playlist.length > 0){this.playlistPlay();}
else{this.targetChannel.sendMessage("The playlist has ended."); this.isPlaying = false; botParent.swapStatus();}
}).bind(this));
}
// -- PLAYS THE APPROPRIATE PLAYLIST TRACK --
playlistPlay()
{
if (this.playlist[0] == undefined){console.log("Playlist 0 was undefined!"); return;}
console.log("PlaylistPlay() "+this.playlist[0].url+", length is "+this.playlist.length.toString()+".");
if (this.playlist[0].title != undefined){botParent.setStatus(this.playlist[0].title);}
else {botParent.swapStatus();}
if (this.playlist[0].url != undefined)
{
var parpar = this;
var par = this;
// -- NOT LOCAL, PLAY A YOUTUBE VIDEO --
if (!this.playlist[0].local)
{
var thestream = ytdl(this.playlist[0].url, {filter: 'audioonly'}, this.targetChannel);
var TS = thestream;
thestream.on("error",function(error){
if (error.toString().indexOf("Code 150") != -1){parpar.targetChannel.sendMessage(":x: **parpar video is fucking censored, wow:** `"+parpar.playlist[0].title+"`");}
else {parpar.targetChannel.sendMessage("There was an error with the video, oh no\n```"+error+"```"); console.log(error);}
parpar.playlist.shift();
if (parpar.playlist.length > 0){parpar.playlistPlay();}
else{parpar.targetChannel.sendMessage("The playlist has ended."); parpar.isPlaying = false; botParent.swapStatus();}
}.bind(par));
thestream.on("response",(function(){
if (TS != undefined)
{
console.log("RESPONSE YO!");
parpar.playYoutube(TS);
}
}).bind(par));
}
// -- LOCAL, PLAY A SOUND CLIP --
else
parpar.playLocal();
}
else{this.targetChannel.sendMessage("URL was undefined."); this.isPlaying = false; this.playlist.length = 0;}
}
// -- DO WE WANT TO PLAY A PLAYLIST?
checkForPlaylist(message)
{
for (var l=0; l<playlists.length; l++)
{
if (message.content.toLowerCase().indexOf("!"+playlists[l].id) != -1)
{
message.reply("*Now playing the playlist* `"+playlists[l].id+"`*, one moment...*");
this.addPlaylist(playlists[l].id,message);
}
}
}
// -- RETURNS A STRING WITH ALL OF THE AVAILABLE PLAYLISTS
printPlaylists()
{
var PLS = "";
for (var l=0; l<playlists.length; l++) { PLS += "`"+l.toString()+"` **"+playlists[l].id+"** - *"+playlists[l].desc+"*\n"; }
return PLS;
}
// -- SHUFFLE FUNCTIONS --
shuffle(inn)
{
var out = [];
while (inn.length > 0) {out.push(inn.splice(Math.floor(Math.random() * inn.length), 1)[0]);}
return out;
}
// -- PAUSE THE SONG --
pauseSong(message)
{
if (this.voiceStream != undefined)
{
message.reply("**Paused.** - Use `!resume` to resume.");
this.voiceStream.pause();
}
else
message.reply("**You can't pause without anything playing!**");
}
// -- RESUME SONG --
resumeSong(message)
{
if (this.voiceStream != undefined)
{
message.reply("**Resumed.**");
this.voiceStream.resume();
}
else
message.reply("**You can't resume without anything playing!**");
}
safeShuffle(inn) {return this.shuffle(inn.slice(0));}
shuffleWithoutFirst(inn)
{
var safe = inn.slice(0);
var firsty = safe[0];
var spliced = this.safeShuffle( safe.splice(1,safe.length-1) );
spliced.unshift(firsty);
return spliced;
}
// -- SHUFFLES THE PLAYLIST WHILE IT'S PLAYING
shufflePlaylist(message)
{
// Make sure we're in a playlist
if (!this.isPlaying){message.reply("You have to be in a playlist to shuffle it."); return;}
this.playlist = this.shuffleWithoutFirst(this.playlist);
message.reply("**Playlist has been shuffled.**");
}
// -- TERMINATES THE WHOLE PLAYLIST --
terminatePlaylist(message)
{
// Make sure we're in a playlist
if (!this.isPlaying){message.reply("You have to be in a playlist to shuffle it."); return;}
this.playlist.length = 0;
this.isPlaying = false;
if (this.voiceStream != undefined){this.voiceStream.end();}
message.reply("**Terminated the playlist.**");
botParent.swapStatus();
}
// -- SKIPS A TRACK IN THE PLAYLIST --
skipTrack()
{
if (this.voiceStream != undefined)
{
if (this.isStreaming)
{
this.isStreaming = false;
this.voiceStream.end();
if (this.targetChannel != undefined){this.targetChannel.sendMessage("Ended the stream.");}
return;
}
if (this.playlist.length > 1){this.targetChannel.sendMessage("Playing next video.");}
this.voiceStream.end();
}
}
// -- CHANGES THE VOLUME OF THE MUSIC
setVolume(vol,message)
{
if (this.voiceStream == undefined){message.reply("Be patient, wait for something to start playing"); return;}
if (!this.isPlaying && !this.isStreaming){message.reply("No music is playing right now."); return;}
this.targetChannel = message.channel;
this.voiceStream.setVolume(vol);
this.playVolume = vol;
message.reply("Volume set to **"+(vol*100).toString()+"%**.");
}
// -- STARTS STREAMING FROM A CERTAIN URL
startStreaming(streamurl,message)
{
this.isStreaming = true;
this.targetChannel = message.channel;
this.voiceStream = this.voiceConnection.playStream( streamurl, {seek:0,volume:this.playVolume} );
}
// -- ADDS A PREMADE ARRAY OF TRACKS TO THE PLAYLIST
addPlaylist(id,message)
{
this.targetChannel = message.channel;
if (this.isStreaming)
{
message.reply("You can't add playlists while a stream is playing!");
return;
}
// -- FIND THE ACTUAL PLAYLIST WE'RE GOING TO USE --
var PLN = -1;
for (var l=0; l<playlists.length; l++){if (playlists[l].id == id){PLN = l; break;}}
if (PLN == -1){message.reply("That playlist didn't exist."); return;}
var TA = this.safeShuffle(playlists[PLN].tracks);
if (this.isPlaying){message.reply("A playlist is already going, hold up");}
else
{
// Push all of the tracks
for (var l=0; l<TA.length; l++){this.playlist.push(TA[l]);}
this.playlistPlay();
this.isPlaying = true;
}
}
// -- ADD A TRACK TO THE PLAYLIST --
addTrack(URL,channel,resetit)
{
var that = this;
this.targetChannel = channel;
if (this.isStreaming)
{
channel.sendMessage("You can't add tracks when you're streaming!");
return;
}
if (URL.indexOf("://") == -1) {URL = "http://www.youtube.com/watch?v=" + URL;}
Request(URL, function (error, response, body)
{
var tit = "";
if (!error && response.statusCode == 200)
{
var $ = cheerio.load(body);
tit = $("title").text();
console.log("TIT IS "+tit);
tit = tit.replace(" - YouTube","");
}
if (!resetit)
{
if (that.playlist.length > 0){that.targetChannel.sendMessage("Added video **"+tit+"**.");}
that.playlist.push( {url:URL,title:tit} );
}
else
{
that.playlist.length = 0;
that.playlist.push( {url:URL,title:tit} );
that.isPlaying = false;
if (this.voiceStream != undefined){this.voiceStream.end();}
}
console.log("Playing the actual vid");
if (!that.isPlaying)
{
that.playlistPlay();
that.isPlaying = true;
}
})
}
// -- RECEIVE A LIST OF ALL THE TRACKS
getTrackList()
{
var TL = ":cd: ";
var clampString = "";
var LN = this.playlist.length;
if (this.playlist.length >= 15){LN = 15; clampString="\n*(Showing next 15 songs only)*"}
for (var l=0; l<LN; l++){TL += "• `"+(l+1).toString()+"` "+this.playlist[l].title+"\n";}
return "\n**Here is the current video playlist:**"+clampString+"\n\n"+TL;
}
}
module.exports = ThePlayer;