-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmusictimer_mod_v10d.js
413 lines (396 loc) · 17.1 KB
/
musictimer_mod_v10d.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
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
// <reference path="../types.d.ts" />
registerPlugin({
name: 'Schedule Music Timer (mod)',
backends: ['ts3'],
version: '1.1',
engine: '>= 1.0.0',
description: 'Play music on a time, move clients with TOT problem, change Description on Channel',
author: 'Filtik <[email protected]> / Modded by <[email protected]>',
vars: {
Tracks: {
title: 'Playing Timers',
type: 'array',
vars: [
{
name: 'Track',
title: 'Play Track',
type: 'track'
},
{
name: 'Time',
title: 'Play Track Time',
type: 'strings',
// placeholder: '14:00,18:26,11:22,12:59,00:11'
},
{
name: 'EveryDay',
title: 'Play Track Day',
type: 'select',
options: ["every day", "custom"]
},
{
name: 'Days',
title: 'Days - 1 = Sun, 2 = Mon, 3 = Tues, 4 = Wed, 5 = Thurs, 6 = Fri, 7 = Sat',
type: 'strings',
// placeholder: '5,1,2',
conditions: [
{ field: 'EveryDay', value: 1 }
]
},
{
name: 'tempdisable',
title: 'Temporary disable',
type: 'checkbox'
},
{
name: 'modenable',
title: 'Enable modifications',
type: 'checkbox'
},
{
name: 'preAudioEnable',
title: 'Play pre-Audio Track',
type: 'checkbox',
conditions: [
{ field: 'modenable', value: true }
]
},
{
name: 'preAudioTrack',
title: 'Track to play before MainTrack',
type: 'track',
indent: 2,
conditions: [
{ field: 'preAudioEnable', value: true }
]
},
{
name: 'tempDescription',
indent: 2,
title: 'Custom channel topic while playing tracks',
type: 'string',
conditions: [
{ field: 'modenable', value: true }
]
},
{
name: 'tot',
indent: 2,
title: 'Prevent Radios from TOT (moving)',
type: 'checkbox',
conditions: [
{ field: 'modenable', value: true }
]
},
{
name: 'muteAll',
indent: 2,
title: 'Mutes all clients in BOTs channel, while track is playing',
type: 'checkbox',
conditions: [
{ field: 'modenable', value: true }
]
}
]
},
}
}, function (_, config, meta) {
var engine = require('engine');
var backend = require('backend');
var event = require('event');
var media = require('media');
var audio = require('audio');
var store = require('store');
var CheckTime = 0;
var CheckTimeTemp = 0;
var PlaySong;
var NextTrack = null;
var NextTrackTime = '';
var TrackPlayed = true;
var TrackPlayedTime = new Date();
var TrackArray = [];
var totenable = false;
var muteall = false;
var newtopic = null;
var modenable = false;
var preAudioEnable = false;
var preAudioTrack = 'undefined';
var queue = [];
var message = meta.name + ' V' + meta.version + ' > ';
function startup() {
for (var num in config.Tracks) {
if (config.Tracks[num].Time == '' || typeof config.Tracks[num].Time == 'undefined') {
engine.log(message + "ERROR: No time in " + num + " seted - STOPP");
return;
}
if (config.Tracks[num].Track == '' || typeof config.Tracks[num].Track == 'undefined') {
engine.log(message + "ERROR: No track in " + num + " seted - STOPP");
return;
}
if (config.Tracks[num].EveryDay == '' || typeof config.Tracks[num].EveryDay == 'undefined') {
engine.log(message + "WARN: No concret play day in " + num + " seted - play every day");
}
else if (config.Tracks[num].EveryDay == '1') {
if (config.Tracks[num].Days == '' || typeof config.Tracks[num].Days == 'undefined') {
engine.log(message + "ERROR: No play day in " + num + " seted - STOPP");
return;
}
}
}
engine.log(message + 'started successfully');
for (var conf in config.Tracks) {
var totenable = config.Tracks[conf].tot || false;
var tempdisable = config.Tracks[conf].tempdisable || false;
var muteall = config.Tracks[conf].muteAll || false;
var newtopic = config.Tracks[conf].tempDescription || 'undefined';
var modenable = config.Tracks[conf].modenable || false;
var preAudioEnable = config.Tracks[conf].preAudioEnable || false;
if (preAudioEnable) var preAudioTrack = config.Tracks[conf].preAudioTrack["url"]; else var preAudioTrack = 'undefined';
// engine.log(config.Tracks[conf].EveryDay);
if (config.Tracks[conf].EveryDay == '0') {
for (var i = 0; i <= 6; i++) {
var stimes = config.Tracks[conf].Time;
stimes.forEach(times => { //(var times in stimes) {
//var sttimes = stimes[times];
var res = times.split(':');
var time = new TrackDate();
time.Day = i;
if (!parseInt(res[0])) return;
time.Hour = res[0];
time.Minute = res[1] || "00";
var newTrack = new Nexttrack();
newTrack.DateTime = time;
newTrack.Title = config.Tracks[conf].Track["title"];
newTrack.URL = config.Tracks[conf].Track["url"];
newTrack.muteAll = muteall;
newTrack.totenable = totenable;
newTrack.tempdisable = tempdisable;
newTrack.newtopic = newtopic;
newTrack.modenable = modenable;
newTrack.preAudioEnable = preAudioEnable;
newTrack.preAudioTrack = preAudioTrack;
TrackArray.push(newTrack);
});
}
}
if (config.Tracks[conf].EveryDay == '1') {
// engine.log('Bedingung erfüllt');
var splitdays = config.Tracks[conf].Days;
// engine.log(splitdays);
//for (var days in splitdays) {
splitdays.forEach(days =>{
//var stdays = splitdays[days];
var stdays = days;
// if (stdays == new Date().getDay() + 1) {
var stimes = config.Tracks[conf].Time;
stimes.forEach(times => {
//var sttimes = stimes[times];
var res = times.split(':');
var time = new TrackDate();
time.Day = parseInt(stdays);
if (!parseInt(res[0])) return;
time.Hour = res[0];
time.Minute = res[1] || "00";
var newTrack = new Nexttrack();
newTrack.DateTime = time;
newTrack.Title = config.Tracks[conf].Track["title"];
newTrack.URL = config.Tracks[conf].Track["url"];
newTrack.muteAll = muteall;
newTrack.totenable = totenable;
newTrack.tempdisable = tempdisable;
newTrack.newtopic = newtopic;
newTrack.modenable = modenable;
newTrack.preAudioEnable = preAudioEnable;
newTrack.preAudioTrack = preAudioTrack;
TrackArray.push(newTrack);
})
// }
})
}
}
TrackArray.sort((a, b) => a - b);
engine.log(meta.name + ' - V' + meta.version + ' has loaded succesfully.');
setInterval(checkNextTrack, 5000);
}
function checkNextTrack() {
// engine.log('Triggert ' + new Date().getHours() + ':' + new Date().getMinutes() + ':' + new Date().getSeconds());
TrackArray.forEach(function (track) {
if (track.DateTime.Day != new Date().getDay() + 1)
return;
var splittettimes = track.DateTime.Hour + ":" + track.DateTime.Minute;
CheckTimeTemp = getMiliTime(splittettimes);
if (CheckTimeTemp > 0) {
if (TrackPlayedTime.getMinutes() != new Date().getMinutes() && TrackPlayed) {
CheckTime = CheckTimeTemp;
TrackPlayed = false;
}
if (CheckTimeTemp < CheckTime ) {
CheckTime = CheckTimeTemp;
NextTrack = track;
PlaySong = track.Title;
NextTrackTime = splittettimes;
}
}
});
if (NextTrack == null)
return;
//if (CheckTime > 0)
// engine.log('Next Track "' + PlaySong + '" at ' + NextTrackTime + '. This is in ' + CheckTime + 'ms');
if (new Date().getHours() != NextTrack.DateTime.Hour)
return;
if (new Date().getMinutes() != NextTrack.DateTime.Minute)
return;
if (TrackPlayed)
return;
playTrack(NextTrack);
}
function playTrack(track) {
if (backend.isConnected) {
if (track.tempdisable) {
engine.log('Not played track: ' + track.Title + ' | Reason: temporary disabled');
NextTrack = null;
NextTrackTime = '';
TrackPlayed = true;
TrackPlayedTime = new Date();
return;
};
var channel = backend.getCurrentChannel();
var clients = channel.getClients();
if (track.totenable) ('TOT prevention for this track is ENABLED'); else engine.log('TOT prevention for this track is DISABLED');
if (track.modenable) {
clients.forEach(client => {
let isServerGroup = client.getServerGroups().some(group => {
return group.id() == 113
});
// store.set(client.uid(), channel.id() + "|" + client.uid() + "|" + channel.topic());
if (isServerGroup) {
if (track.totenable) {
engine.log('Client ' + client.name() + ' with TOT problem found. Move temopary to empty channel');
store.set(client.uid(), channel.id() + "|" + client.uid());
client.moveTo(762);
}
// store.set(client.uid(), channel.id() + "|" + client.uid() + "|" + channel.topic());
// engine.log(store.get(client.uid()));
}
// store.set(client.uid(), channel.id() + "|" + client.uid() + "|" + channel.topic());
});
if (typeof track.newtopic !== 'undefined' || track.muteAll) {
engine.log('Set temp. Channel-Permissions');
store.set('talkpower', 10);
channel.update({
neededTalkPower: 5000,
});
}
if (typeof track.newtopic !== 'undefined' ) {
engine.log('Set temp. channel topic to: ' + track.newtopic);
store.set('topic', channel.topic());
channel.update({
topic: track.newtopic,
});
}
}
if (track.preAudioTrack && track.preAudioEnable) {
engine.log('Playback pre-Audio, then PLAY MUSIC "' + track.Title + '"');
// store.set('action', false);
store.set('nexttrack',track.URL);
media.playURL(track.preAudioTrack);
} else {
media.playURL(track.URL);
// store.set('action', true);
engine.log('PLAY MUSIC "' + track.Title + '"');
}
NextTrack = null;
NextTrackTime = '';
TrackPlayed = true;
TrackPlayedTime = new Date();
}
}
event.on('trackEnd', function (ev) {
if (!store.get('nexttrack') || store.get('nexttrack') == 'undefined') {
//store.get('action') == true) {
//store.unset('action');
var channel = backend.getCurrentChannel();
for (var num in config.Tracks) {
if (config.Tracks[num].modenable) {
if (store.get('topic')) {
engine.log('Restore Channel-Topic to: ' + store.get('topic'));
channel.update({topic: store.get('topic')});
store.unset('topic');
}
if (store.get('talkpower')) {// || config.Tracks[num].muteAll) {
engine.log('Restore Talkpower');
// channel.update({neededTalkPower: store.get('talkpower')});
channel.update({neededTalkPower: store.get('talkpower')});
store.unset('talkpower');
}
if (config.Tracks[num].tot) {
store.getKeys().forEach(key => {
let ar = store.get(key);
let arr = ar.split('|');
let uid = arr[1];
let channelId = arr[0];
let client = backend.getClientByUID(uid);
client.moveTo(channelId);
engine.log('Client ' + client.name() + ' move back to current channel (' + channelId + ')');
store.unset(key);
});
}
}
}
} else {
media.playURL(store.get('nexttrack'));
store.unset('nexttrack');
}
});
event.on('chat', event => {
if (event.text == '!showMusic') {
engine.log(TrackArray);
}
})
function waitForBackend(attempts, wait) {
return new Promise((success, fail) => {
let attempt = 1;
const timer = setInterval(() => {
setTimeout(() => { backend.connect(); }, 3500);
if (backend.isConnected()) {
clearInterval(timer);
if (config.dev) engine.log('waitForBackend() took ' + attempt + ' attempts with a timer of ' + wait + ' seconds to resolve');
success();
return;
} else if (attempt > attempts) {
clearInterval(timer);
if (config.dev) engine.log('waitForBackend() failed at ' + attempt + '. attempt with a timer of ' + wait + ' seconds');
fail('backend');
return;
}
attempt++;
}, wait * 1000);
});
}
function getMiliTime(playtime) {
var res = playtime.split(':');
var d = new Date();
d.setHours(res[0]);
d.setMinutes(res[1]);
d.setSeconds(0);
d.setMilliseconds(0);
var n = d.getTime() - Date.now();
return n;
}
var Nexttrack = /** @class */ (function () {
function Nexttrack() {
}
return Nexttrack;
}());
var TrackDate = /** @class */ (function () {
function TrackDate() {
}
return TrackDate;
}());
event.on('load', () => {
waitForBackend(10, 3)
.then(() => {
startup();
})
});
});