-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbot.js
More file actions
317 lines (272 loc) · 7.03 KB
/
bot.js
File metadata and controls
317 lines (272 loc) · 7.03 KB
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
/*
This bot is written by MikeB and updated by Mike Wills
*/
var IDLELIMIT = (60 * (60*1000)); // 60 minutes
var SONGLIMIT = 480; // 8 minutes
var usercache;
var songtimer;
var hungSongCheck;
function checkIdle()
{
var date = new Date();
var waitlist = API.getWaitList();
for ( var i in waitlist )
{
for ( var j in usercache )
{
if ( waitlist[i].id == usercache[j].id )
{
var timedelta = date.getTime() - usercache[j].lastactive;
if ( (timedelta > IDLELIMIT) && (usercache[j].idlenotify == 0) )
{
API.sendChat('@' + usercache[j].username + ' you have been idle for ' + Math.round(timedelta/(60*1000)) + ' minutes. Please keep chatting in the room if you are going to DJ. Thanks!');
console.log('Warned ' + usercache[j].username + ' of excessive idle time.');
usercache[j].idlenotify = 1;
setTimeout(function(userid) {
for ( var k in usercache )
{
if ( userid == usercache[k].id )
{
var date = new Date();
if ( (date.getTime() - usercache[k].lastactive) > IDLELIMIT )
{
API.moderateRemoveDJ(usercache[k].id);
API.sendChat('@' + usercache[k].username + ' you have been removed for idle DJing. Please keep chatting in the room if you are going to DJ. Thanks!');
usercache[k].idlenotify = 0;
console.log('Removed ' + usercache[j].username + ' for excessive idle time.');
}
}
}
}, (5*(60*1000)), usercache[j].id);
}
}
}
}
}
function updateIdle(id)
{
for ( var i in usercache )
{
if ( id == usercache[i].id )
{
var date = new Date();
usercache[i].lastactive = date.getTime();
usercache[i].idlenotify = 0;
}
}
}
function sortByKey(array, key) {
return array.sort(function(a, b) {
var x = a[key]; var y = b[key];
return ((x > y) ? -1 : ((x < y) ? 1 : 0));
});
}
function isBouncer(id)
{
var staff = API.getStaff();
for ( var i in staff )
{
if (( staff[i].permission > 1 ) && ( staff[i].id == id ))
{
return 1;
}
}
return 0;
}
function isHost(id)
{
var staff = API.getStaff();
for ( var i in staff )
{
if ( (( staff[i].permission == 4 ) || ( staff[i].permission == 5 )) && ( staff[i].id == id ))
{
return 1;
}
}
return 0;
}
function DJ_ADVANCE(obj)
{
// Clear the timer if the skip worked.
clearTimeout(hungSongCheck);
if ( songtimer != null)
{
clearTimeout(songtimer);
songtimer = null;
}
if (obj.lastPlay != null)
{
API.sendChat(obj.lastPlay.media.attributes.author + " - " + obj.lastPlay.media.attributes.title + " :arrow_up: " + obj.lastPlay.score.positive + " :arrow_down: " + obj.lastPlay.score.negative + " :star: " + obj.lastPlay.score.curates);
}
setTimeout(function() {
$('#woot').click()
}, (10*1000));
checkIdle();
if (obj.media != null)
{
if (obj.media.duration > SONGLIMIT)
{
var songlimitmins = SONGLIMIT / 60;
API.sendChat('@' + obj.dj.username + ' - this song is excessively long, please skip before ' + songlimitmins.toFixed(1) + ' minutes. Thanks!');
songtimer = setTimeout(function(djid, songid) {
var curdj = API.getDJ();
var curmedia = API.getMedia();
if ( (djid == curdj.id) && (songid == curmedia.id) )
{
API.sendChat('@' + obj.dj.username + ' - you are about to exceed the song time limit of ' + songlimitmins.toFixed(1) + ' minutes, please skip to the next DJ. Thanks!');
songtimer = setTimeout(function(djid, songid) {
var curdj = API.getDJ();
var curmedia = API.getMedia();
if ( (djid == curdj.id) && (songid == curmedia.id) )
{
API.moderateForceSkip();
API.sendChat('@' + obj.dj.username + ' - you have exceeded the song time limit of ' + songlimitmins.toFixed(1) + ' minutes.');
console.log('Skipped ' + obj.dj.username + '\'s song for exceeding the time limit.');
}
}, (30*1000), djid, songid);
}
}, ((SONGLIMIT - 30)*1000), obj.dj.id, obj.media.id);
}
// Monitor for a hung song
hungSongCheck = setTimeout(function() {
API.sendChat("Hung song, skipping for you.");
API.moderateForceSkip();
}, (data.media.duration + 10) * 1000 );
}
}
function USER_JOIN(obj)
{
var date = new Date();
obj.lastactive = date.getTime();
obj.idlenotify = 0;
usercache.push(obj);
}
function USER_LEAVE(obj)
{
for ( var i in usercache )
{
if ( obj.id == usercache[i].id )
{
usercache.splice(i, 1);
}
}
}
function WAIT_LIST_UPDATE(obj)
{
checkIdle();
}
function DJ_UPDATE(obj)
{
checkIdle();
}
function CHAT(obj)
{
updateIdle(obj.fromID);
if ( obj.message == "!skip" )
{
if ( isBouncer(obj.fromID) )
{
API.moderateForceSkip();
API.sendChat('@' + obj.from + ' - attempted to skip the current song.');
console.log(obj.from + ' had me skip the song.');
}
}
else if ( obj.message == "!reload" )
{
if ( isHost(obj.fromID) )
{
API.sendChat('@' + obj.from + ' - attempting to reload.');
setTimeout(function() {
location.reload(true);
}, (3*1000));
}
}
else if ( obj.message == "!allowsong" )
{
if ( isBouncer(obj.fromID) )
{
if ( songtimer != null )
{
clearTimeout(songtimer);
songtimer = null;
API.sendChat('@' + obj.from + ' - I will allow this song to play.');
console.log(obj.from + ' had me allow a song longer than the time limit.');
}
}
}
else if ( obj.message == "!idle" )
{
if ( isBouncer(obj.fromID) )
{
var idlelist = new Array();
var output = "The most idle DJs are: ";
var waitlist = API.getWaitList();
var date = new Date();
waitlist.push(API.getDJ());
for ( var i in waitlist )
{
for ( var j in usercache )
{
if ( waitlist[i].id == usercache[j].id )
{
idlelist.push({
username: usercache[j].username,
time: Math.round((date.getTime() - usercache[j].lastactive)/(60*1000))
});
}
}
}
if ( idlelist != null )
{
idlelist = sortByKey(idlelist, 'time');
for (var i=0; (i < 5) && (i < idlelist.length); i++)
{
output += idlelist[i].username + ' ' + idlelist[i].time + 'min ';
}
}
else
{
output = 'There are no active DJs or waitlist';
}
API.sendChat(output);
console.log(obj.from + ' requested the idle times.');
}
}
}
function USER_SKIP(obj)
{
updateIdle(obj.id);
}
function USER_FAN(obj)
{
updateIdle(obj.id);
}
function CURATE_UPDATE(obj)
{
updateIdle(obj.user.id);
}
function INIT()
{
var users = API.getUsers();
var date = new Date();
for ( var i in users )
{
users[i].lastactive = date.getTime();
users[i].idlenotify = 0;
}
usercache = users;
$('#playback').css('display', 'none'); // hide video for the bot
API.setVolume(0);
API.sendChat('I\'m back!');
console.log("Bot is running");
}
API.on(API.DJ_ADVANCE, DJ_ADVANCE);
API.on(API.USER_JOIN, USER_JOIN);
API.on(API.USER_LEAVE, USER_LEAVE);
API.on(API.WAIT_LIST_UPDATE, WAIT_LIST_UPDATE);
API.on(API.DJ_UPDATE, DJ_UPDATE);
API.on(API.CHAT, CHAT);
API.on(API.USER_SKIP, USER_SKIP);
API.on(API.USER_FAN, USER_FAN);
API.on(API.CURATE_UPDATE, CURATE_UPDATE);
setTimeout(function () {INIT();}, (5*1000));