This repository has been archived by the owner on Jan 16, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapp.coffee
339 lines (321 loc) · 13.8 KB
/
app.coffee
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
DiscordClient = require './discordClient/discordClient'
WebServer = require './webserver'
motorbotEventHandler = require './motorbotEventHandler'
keys = require './keys.json'
websocketServer = require("ws").Server
#SocketCluster = require('socketcluster');
stream = require 'stream'
http2 = require 'http2'
MongoClient = require('mongodb').MongoClient
youtubeStream = require 'ytdl-core'
request = require 'request'
fs = require 'fs'
path = require 'path'
uid = require('rand-token').uid;
class App
constructor: () ->
self = @
@musicPlayers = {}
@soundboard = {}
@say = {}
@yStream = {}
###if cluster.isMaster
cluster.on('online', (worker) ->
console.log('Worker ' + worker.process.pid + ' is online')
)
cluster.on('exit', (worker, code, signal) ->
console.log('Worker ' + worker.process.pid + ' died with code: ' + code + ', and signal: ' + signal)
console.log('Starting a new worker')
cluster.fork()
)
for i in [0..2]
cluster.fork()
else###
@init()
debug: (msg,level = "debug") ->
if (process.env.NODE_ENV != 'test')
if level == "info"
level = "\x1b[34m[INFO ]\x1b[0m"
else if level == "error"
level = "\x1b[31m[ERROR]\x1b[0m"
else if level == "warn"
level = "\x1b[5m\x1b[33m[WARN ]\x1b[0m"
else if level == "notification"
level = "\x1b[5m\x1b[35m[NOTIF]\x1b[0m"
else if level == "debug"
level = "\x1b[2m[DEBUG]"
d = new Date()
time = "["+d.getDate()+"/"+(parseInt(d.getMonth())+1)+"/"+d.getFullYear()+" "+d.toLocaleTimeString()+"] "
console.log(level+time+msg+"\x1b[0m")
init: () ->
@debug("Initialising")
@client = new DiscordClient({token: keys.token})
new motorbotEventHandler(@, @client)
@client.connect()
self = @
@initDatabase().then(() ->
self.initWebServer()
self.initWebSocket()
self.initPlaylist()
)
initDatabase: () ->
self = @
new Promise((resolve, reject) ->
MongoClient.connect('mongodb://localhost:27017/motorbot', (err, db) ->
if err
throw new Error("Failed to connect to database, exiting")
self.database = db
self.debug("Connected to Database")
resolve()
)
)
initWebSocket: () ->
self = @
@websocket = websocketServer({port: 3006}) #public port is 443 (wss://wss.lolstat.net) and local 3006 via nginx proxy
@websocket.connectedClients = []
@websocket.on('connection', (ws) ->
session = Buffer(new Date().getTime() + uid(32)).toString("base64")
ws.on("close", (e) ->
console.log "SOCKET CLOSED"
self.websocket.connectedClients.forEach((client) ->
if client
if client.session == session
self.websocket.connectedClients.splice(self.websocket.connectedClients.indexOf(client),1)
)
)
ws.on('message', (message) ->
#recieved message
msg = JSON.parse(message);
switch msg.op
when 0
ws.send(JSON.stringify({op: 1, type:"HEARTBEAT_ACK", d:{}}), (err) ->
if err then console.log err
)
when 2
###
loadPlaying();
#loadRandom(); Depreciated currently
getCurrentChannel();
loadGuilds();
###
self.websocket.connectedClients.push({
id: new Date().getTime() + msg.d.user_id,
user_id: msg.d.user_id,
ws: ws,
session: session
})
welcome_obj = {
playing: {},
channel: undefined,
guilds: {},
session: session
}
songQueueCollection = self.database.collection("songQueue")
songQueueCollection.find({status:'playing'}).toArray((err, results) ->
if err then console.log err
if results[0]
results[0]["currently_playing"] = false
if self.musicPlayers #weird
if self.musicPlayers["130734377066954752"]
results[0]["currently_playing"] = self.musicPlayers["130734377066954752"].playing
results[0]["start_time"] = self.musicPlayers["130734377066954752"].start_time
results[0]["position"] = self.musicPlayers["130734377066954752"].seekPosition
results[0]["playlist_id"] = self.musicPlayers["130734377066954752"].playlist_id
welcome_obj.playing = results[0]
else
welcome_obj.playing = {currently_playing: false}
if self.client.voiceConnections["130734377066954752"] then welcome_obj.channel = self.client.voiceConnections["130734377066954752"].channel_name
if self.client.guilds then welcome_obj.guilds = self.client.guilds #this should be changed to be user specific and only show the ones motorbot is part of
ws.send(JSON.stringify({op: 3, type:"WELCOME", d:welcome_obj}, (key, value) ->
if key == "client" then return undefined else return value
), (err) ->
if err then console.log err
)
)
when 8
if self.musicPlayers["130734377066954752"]
playlistId = self.musicPlayers["130734377066954752"].playlist_id
songId = self.musicPlayers["130734377066954752"].song_id
player_state = self.musicPlayers["130734377066954752"].player_state
if self.musicPlayers["130734377066954752"].playing
self.websocket.broadcast(JSON.stringify({type: 'PLAYER_UPDATE', op: 7, d: {event_type: 'PLAY', player_state: player_state, playlist_id: playlistId, song_id: songId}}))
else
self.websocket.broadcast(JSON.stringify({type: 'PLAYER_UPDATE', op: 7, d: {event_type: 'PAUSE', player_state: player_state, playlist_id: playlistId, song_id: songId}}))
else
self.websocket.broadcast(JSON.stringify({type: 'PLAYER_UPDATE', op: 7, d: {event_type: 'STOP', player_state: player_state, playlist_id: undefined, song_id: undefined}}))
)
)
@websocket.broadcast = (data, user_id) ->
if user_id
self.websocket.connectedClients.forEach((client) ->
if client
if client.user_id = user_id
client.ws.send(data, (err) ->
if err then console.log err
)
)
else
self.websocket.clients.forEach((client) ->
if client
client.send(data, (err) ->
if err then console.log err
)
)
initWebServer: () ->
self = @
@debug("Starting Web Server")
@webserver = new WebServer(@)
@webserver.start()
@webserver.site.listen(3210, () ->
self.debug("Web Server Started and Listening on 3210","info")
).on("error", (err) ->
console.log err
)
initPlaylist: () ->
self = @
songQueueCollection = @database.collection("songQueue")
songQueueCollection.find({status: "playing"}).toArray((err, results) ->
if err then console.log err
for r in results
trackId = r._id
songQueueCollection.updateOne({'_id': trackId},{'$set':{'status':'played'}},() ->
self.debug("Track Status Changed")
)
)
streamNewTrack: (results) ->
self = @
guild_id = "130734377066954752"
songQueueCollection = @database.collection("songQueue")
tracksCollection = @database.collection("tracks")
playlistsCollection = @database.collection("playlists")
if results[0]
videoId = results[0].video_id
title = results[0].title
trackId = results[0]._id
song_id = results[0].id
playlistId = results[0].playlistId
song = results[0]
if videoId
songQueueCollection.updateOne({'_id': trackId, 'playlistId': playlistId},{'$set':{'status':'playing'}},(err, result) ->
if !err then self.debug("Track Status Changed")
)
tracksCollection.updateOne({'id': song_id},{'$inc':{'play_count':1}},(err, result) ->
if !err then self.debug("Tracks Play Count increased")
)
playlistsCollection.updateOne({'id': playlistId, "songs.id": song_id},{'$inc':{'songs.$.play_count':1}, '$set':{'songs.$.last_played': new Date().getTime()}},(err, result) ->
if !err then self.debug("Playlist Play Count increased")
)
requestUrl = 'https://www.youtube.com/watch?v=' + videoId
youtubeStream.getInfo(requestUrl, (err, info) ->
volume = 0.5 #set default, as some videos (recently uploaded maybe?) don't have loudness value
#stabilise volume to avoid really loud or really quiet playback
if info
if info.loudness
volume = (parseFloat(info.loudness)/-40)
self.debug "Setting Volume Based on Video Loudness ("+info.loudness+"): "+volume
self.yStream[guild_id] = youtubeStream.downloadFromInfo(info,{quality: 'highest', filter: 'audioonly'})
thisystream = self.yStream[guild_id]
thisystream.on("error", (e) ->
console.log "e: "+e.toString()
self.debug("Error Occurred Loading Youtube Video")
self.websocket.broadcast(JSON.stringify({type: 'YOUTUBE_ERROR', op:6, d: {error: e.toString()}}))
self.nextSong()
)
if self.client.voiceConnections[guild_id]
self.client.voiceConnections[guild_id].playFromStream(thisystream).then((audioPlayer) ->
self.musicPlayers[guild_id] = audioPlayer
self.musicPlayers[guild_id].on('ready', () ->
self.musicPlayers[guild_id].setVolume(volume)
self.musicPlayers[guild_id].play()
self.musicPlayers[guild_id].playing = true
self.musicPlayers[guild_id].start_time = new Date().getTime()
self.musicPlayers[guild_id].playlist_id = playlistId
self.musicPlayers[guild_id].song_id = song_id
results.shift()
playerState = {
isPaused: false,
isPlaying: true,
isStopped: false,
restrictions: {},
next_tracks: results,
previous_tracks: [],
current_song: song,
seekPosition: 0
}
if !results[1]
playerState.restrictions["skip"] = true
songQueueCollection.find({status: "played"}).sort({sortId: -1}).toArray((err, results) ->
if err then console.log err
if !results[0]
playerState.restrictions["back"] = true
else
playerState.previous_tracks = results
self.musicPlayers[guild_id].player_state = playerState
self.websocket.broadcast(JSON.stringify({type: 'PLAYER_UPDATE', op: 7, d: {event_type: 'PLAY', player_state: playerState, playlist_id: playlistId, song_id: song_id}}))
)
self.websocket.broadcast(JSON.stringify({type: 'TRACK_UPDATE', op: 5, d: {event_type: "CHANGE", event_data: song, start_time: new Date().getTime()}}))
self.client.setStatus(title)
self.debug("Now Playing: "+title)
)
self.musicPlayers[guild_id].on("progress", (seconds) ->
self.websocket.broadcast(JSON.stringify({type: 'TRACK_DOWNLOAD', op: 10, d: {event_type: "UPDATE", event_data: {download_position: seconds}}}))
)
self.musicPlayers[guild_id].on("streamDone", () ->
delete self.musicPlayers[guild_id]
self.client.setStatus("") # reset to blank
self.nextSong()
)
).catch((err) ->
console.log "ERROR OCCURRED CREATING AUDIO PLAYER"
console.log err
)
else
console.log "Somin aint right here, no voice connection exists for this guild"
else
self.debug("Error Occurred Loading Youtube Video")
self.websocket.broadcast(JSON.stringify({type: 'YOUTUBE_ERROR', op:6, d: {error: "We couldn't retrieve information for this youtube video"}}))
self.nextSong()
)
goThroughSongQueue: () ->
self = @
songQueueCollection = @database.collection("songQueue")
songQueueCollection.find({status: "queued"}).sort({sortId: 1}).toArray((err, results) ->
if err then console.log err
if results[0]
self.streamNewTrack(results)
else
#no songs in queue, go to nextSong
###if globals.randomPlayback
songQueueCollection.find({status: "added"}).sort({randId: 1}).toArray((err, results) ->
if err then console.log err
streamNewTrack(results)
)
else###
songQueueCollection.find({status: "added"}).sort({sortId: 1}).toArray((err, results) ->
if err then console.log err
self.streamNewTrack(results)
)
)
nextSong: () ->
self = @
songQueueCollection = @database.collection("songQueue")
songQueueCollection.find({status: "playing"}).sort({sortId: 1}).toArray((err, results) ->
if err then console.log err
if results[0]
trackId = results[0]._id
playlistId = results[0].playlistId
songQueueCollection.updateOne({'_id': trackId, 'playlistId': playlistId},{'$set':{'status':'played'}},() ->
self.debug("Track Status Changed")
setTimeout(() ->
self.goThroughSongQueue()
,1000)
)
else
self.goThroughSongQueue()
)
skipSong: () ->
if @musicPlayers["130734377066954752"]
@musicPlayers["130734377066954752"].stop()
else
@nextSong()
app = new App()