Skip to content
Navigation Menu
{{ message }}
forked from PhantomBot/PhantomBot
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathraffleSystem.js
More file actions
503 lines (448 loc) · 18.7 KB
/
Copy pathraffleSystem.js
File metadata and controls
503 lines (448 loc) · 18.7 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
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
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
/**
* raffleSystem.js made for giveaways on Twitch
*
*/
(function() {
var entries = [],
entered = [],
keyword = '',
entryFee = 0,
timerTime = 0,
followers = false,
usePoints = true,
status = false,
sendMessages = $.getSetIniDbBoolean('raffleSettings', 'raffleMSGToggle', false),
whisperWinner = $.getSetIniDbBoolean('raffleSettings', 'raffleWhisperWinner', false),
allowRepick = $.getSetIniDbBoolean('raffleSettings', 'noRepickSame', true),
raffleMessage = $.getSetIniDbString('raffleSettings', 'raffleMessage', 'A raffle is still opened! Type (keyword) to enter. (entries) users have entered so far.'),
messageInterval = $.getSetIniDbNumber('raffleSettings', 'raffleMessageInterval', 0),
subscriberBonus = $.getSetIniDbNumber('raffleSettings', 'subscriberBonusRaffle', 1),
regularBonus = $.getSetIniDbNumber('raffleSettings', 'regularBonusRaffle', 1),
interval, timeout, followMessage = '', timerMessage = '';
/**
* @function reloadRaffle
* @info used for the panel.
*/
function reloadRaffle() {
sendMessages = $.getIniDbBoolean('raffleSettings', 'raffleMSGToggle');
allowRepick = $.getIniDbBoolean('raffleSettings', 'noRepickSame');
whisperWinner = $.getIniDbBoolean('raffleSettings', 'raffleWhisperWinner');
raffleMessage = $.getIniDbString('raffleSettings', 'raffleMessage');
messageInterval = $.getIniDbNumber('raffleSettings', 'raffleMessageInterval');
subscriberBonus = $.getIniDbNumber('raffleSettings', 'subscriberBonusRaffle');
regularBonus = $.getIniDbNumber('raffleSettings', 'regularBonusRaffle');
}
/**
* @function open
* @info opens a raffle
*
* @param {string} username
* @param {arguments} arguments
*/
function open(username, arguments) {
var args = arguments.split(' '),
i = 1;
/* Check if there's a raffle already opened */
if (status) {
$.say($.whisperPrefix(username) + $.lang.get('rafflesystem.open.error.opened'));
return;
}
/* Check if the caster wants to use time or points for the raffle */
if (arguments.match('-usetime')) {
usePoints = false;
} else if (arguments.match('-usepoints')) {
usePoints = true;
} else {
usePoints = null;
}
/* Check if the caster wants the raffle to be for followers only or not */
if (arguments.match('-followers')) {
followers = true;
followMessage = ' ' + $.lang.get('rafflesystem.common.following');
}
/* Check the entry fee of points, or the minimum time */
if (!isNaN(parseInt(args[i])) && usePoints !== null) {
if (usePoints) {
entryFee = parseInt(args[i]);
} else {
entryFee = (parseInt(args[i]) * 60);
}
i++;
}
/* Check for the keyword */
if (args[i] !== undefined) {
keyword = args[i].toLowerCase();
i++;
/* Ensure that keyword is not already a registered command. */
if (keyword.startsWith('!') && $.commandExists(keyword.substring(1))) {
$.say($.whisperPrefix(username) + $.lang.get('rafflesystem.open.keyword-exists', keyword));
return;
}
} else {
$.say($.whisperPrefix(username) + $.lang.get('rafflesystem.open.usage'));
return;
}
/* Check if the caster wants a auto close timer */
if (!isNaN(parseInt(args[i])) && parseInt(args[i]) !== 0) {
timerTime = parseInt(args[i]);
timeout = setTimeout(function() { close(); }, (timerTime * 6e4));
timerMessage = $.lang.get('rafflesystem.common.timer', timerTime);
}
/* Say in chat that the raffle is now opened. */
if (!usePoints && usePoints !== null) {
$.say($.lang.get('rafflesystem.open.time', keyword, Math.floor(entryFee / 60), followMessage, timerMessage));
} else if (usePoints && entryFee !== 0) {
$.say($.lang.get('rafflesystem.open.points', keyword, $.getPointsString(entryFee) + followMessage, timerMessage));
} else {
$.say($.lang.get('rafflesystem.open', keyword, followMessage, timerMessage));
}
if (parseInt(messageInterval) !== 0) {
interval = setInterval(function() { $.say(raffleMessage.replace('(keyword)', keyword).replace('(entries)', String(Object.keys(entered).length))); }, messageInterval * 6e4);
}
/* Clear the old raffle data */
entries = [];
register(true, keyword.includes('!'));
$.inidb.RemoveFile('raffleList');
$.inidb.set('raffleresults', 'raffleEntries', 0);
/* Mark the raffle as opened */
status = true;
}
/**
* @function close
* @info closes the raffle
*
* @param {string} username
*/
function close(username) {
/* Check if there's a raffle opened */
if (!status) {
$.say($.whisperPrefix(username) + $.lang.get('rafflesystem.close.error.closed'));
return;
}
$.say($.lang.get('rafflesystem.close.success'));
/* Clear the timer if there is one active. */
clearInterval(timeout);
clearInterval(interval);
/* Close the raffle and clears the old data */
clear();
/* Choose the winner */
winner(username, true);
}
/**
* @function winner
* @info chooses a winner for the raffle
*
* @param {boolean} firstWinner
*/
function winner(sender, firstWinner) {
/* Check to see if this is a repick or not */
if (firstWinner) {
/* Check if anyone entered the raffle */
if (entries.length === 0) {
return;
}
var username = $.randElement(entries);
$.say($.lang.get('rafflesystem.winner', username));
$.inidb.set('raffleresults', 'winner', username);
/* whisper the winner if the toggle is on */
if (whisperWinner) {
$.say($.whisperPrefix(username, true) + $.lang.get('rafflesystem.whisper.winner', $.channelName));
}
/* Remove the user from the array if we are not allowed to have multiple repicks. */
if (allowRepick) {
for (var i in entries) {
if (entries[i].equalsIgnoreCase(username)) {
entries.splice(i, 1);
}
}
$.inidb.del('raffleList', username);
$.inidb.decr('raffleresults', 'raffleEntries', 1);
}
return;
}
/* Checks if there's anyone in the raffle list */
if (entries.length === 0) {
$.say($.whisperPrefix(sender) + $.lang.get('rafflesystem.repick.error'));
return;
}
/* Pick a new winner */
var username = $.randElement(entries);
$.say($.lang.get('rafflesystem.repick', username));
$.inidb.set('raffleresults', 'winner', username);
/* whisper the winner if the toggle is on */
if (whisperWinner) {
$.say($.whisperPrefix(username, true) + $.lang.get('rafflesystem.whisper.winner.repick', $.channelName));
}
/* Remove the user from the array if we are not allowed to have multiple repicks. */
if (allowRepick) {
for (var i in entries) {
if (entries[i].equalsIgnoreCase(username)) {
entries.splice(i, 1);
}
}
$.inidb.del('raffleList', username);
$.inidb.decr('raffleresults', 'raffleEntries', 1);
}
}
/**
* @function message
* @info messages that user if the raffle toggles are on
*
* @param {string} username
* @param {string} message
*/
function message(username, message) {
if (sendMessages) {
$.say($.whisperPrefix(username) + message);
}
}
/**
* @function enter
* @info enters the user into the raffle
*
* @param {string} username
*/
function enter(username, tags) {
/* Check if the user already entered the raffle */
if (entered[username] !== undefined) {
message(username, $.lang.get('rafflesystem.enter.404'));
return;
}
/* Check if the user is following the channel. */
if (followers && !$.user.isFollower(username)) {
message(username, $.lang.get('rafflesystem.enter.following'));
return;
}
/* Check the entry fee */
if (entryFee > 0 && usePoints !== null) {
/* If we are using points */
if (usePoints) {
if (entryFee > $.getUserPoints(username)) {
message(username, $.lang.get('rafflesystem.enter.points', $.pointNameMultiple));
return;
} else {
$.inidb.decr('points', username, entryFee);
}
} else {
if (entryFee > $.getUserTime(username)) {
message(username, $.lang.get('rafflesystem.enter.time'));
return;
}
}
}
/* Push the user into the array */
entered[username] = true;
if (subscriberBonus > 0 && $.isSubv3(username, tags)) {
for (var i = 0; i < subscriberBonus; i++) {
entries.push(username);
}
} else if (regularBonus > 0 && $.isReg(username)) {
for (var i = 0; i < regularBonus; i++) {
entries.push(username);
}
} else {
entries.push(username);
}
/* Push the panel stats */
if ($.bot.isModuleEnabled('./handlers/panelHandler.js')) {
$.inidb.set('raffleList', username, true);
$.inidb.incr('raffleresults', 'raffleEntries', 1);
}
}
/**
* @function clear
* @info resets the raffle information
*/
function clear() {
register(false, keyword.includes('!'));
keyword = '';
followMessage = '';
timerMessage = '';
usePoints = false;
followers = false;
status = false;
entryFee = 0;
timerTime = 0;
entered = [];
}
/**
* @function register
* @info binds a new ircChannelMessage event
*
* @param {boolean} register
*/
function register(register, useCommand) {
if (useCommand) {
if (register) {
$.registerChatCommand('./systems/raffleSystem.js', keyword.substring(1), 7);
} else {
$.unregisterChatCommand(keyword.substring(1));
}
} else {
if (register) {
$.bind('ircChannelMessage', function(event) {
if (event.getMessage().equalsIgnoreCase(keyword)) {
enter(event.getSender(), event.getTags());
}
});
} else {
$.unbind('ircChannelMessage');
}
}
}
/**
* @event command
* @info handles the command event
* @param {object} event
*/
$.bind('command', function(event) {
var sender = event.getSender(),
command = event.getCommand(),
arguments = event.getArguments(),
args = event.getArgs(),
action = args[0],
subAction = args[1];
if (command.equalsIgnoreCase('raffle')) {
if (action === undefined && !status) {
$.say($.whisperPrefix(sender) + $.lang.get('rafflesystem.usage'));
return;
}
/**
* @commandpath raffle open [entry fee] [keyword] [close timer] [-usepoints / -usetime / -followers] - Opens a custom raffle.
*/
if (action.equalsIgnoreCase('open')) {
open(sender, arguments);
$.log.event('A raffle was opened by: ' + sender + '. Arguments (' + arguments + ')');
return;
}
/**
* @commandpath raffle close - Closes the current raffle.
*/
if (action.equalsIgnoreCase('close')) {
close(sender);
$.log.event('A raffle was closed by: ' + sender + '.');
return;
}
/**
* @commandpath raffle repick - Repicks a new winner from the current raffle list.
*/
if (action.equalsIgnoreCase('repick')) {
winner(sender, false);
return;
}
/**
* @commandpath raffle results - Give you the current raffle information if there is one active.
*/
if (action.equalsIgnoreCase('results')) {
if (status) {
$.say($.lang.get('rafflesystem.results', keyword + (usePoints ? $.lang.get('rafflesystem.fee', $.getPointsString(entryFee)) : ''), Object.keys(entered).length))
}
return;
}
/**
* @commandpath raffle subscriberbonus [1-10] - Sets the bonus luck for subscribers.
*/
if (action.equalsIgnoreCase('subscriberbonus')) {
if (subAction === undefined || isNaN(parseInt(subAction)) || parseInt(subAction) < 1) {
$.say($.whisperPrefix(sender) + $.lang.get('rafflesystem.subbonus.usage'));
return;
}
subscriberBonus = parseInt(subAction);
$.inidb.set('raffleSettings', 'subscriberBonusRaffle', subscriberBonus);
$.say($.whisperPrefix(sender) + $.lang.get('rafflesystem.subbonus.set', subscriberBonus));
return;
}
/**
* @commandpath raffle regularbonus [1-10] - Sets the bonus luck for regulars.
*/
if (action.equalsIgnoreCase('regularbonus')) {
if (subAction === undefined || isNaN(parseInt(subAction)) || parseInt(subAction) < 1) {
$.say($.whisperPrefix(sender) + $.lang.get('rafflesystem.regbonus.usage'));
return;
}
regularBonus = parseInt(subAction);
$.inidb.set('raffleSettings', 'regularBonusRaffle', regularBonus);
$.say($.whisperPrefix(sender) + $.lang.get('rafflesystem.regbonus.set', regularBonus));
return;
}
/**
* @commandpath raffle whisperwinner - Toggles if the raffle winner gets a whisper from the bot saying he won.
*/
if (action.equalsIgnoreCase('whisperwinner')) {
whisperWinner = !whisperWinner;
$.inidb.set('raffleSettings', 'raffleWhisperWinner', whisperWinner);
$.say($.whisperPrefix(sender) + $.lang.get('rafflesystem.whisper.winner.toggle', (whisperWinner ? '' : $.lang.get('rafflesystem.common.message'))));
}
/**
* @commandpath raffle togglewarningmessages - Toggles the raffle warning messages when entering.
*/
if (action.equalsIgnoreCase('togglewarningmessages')) {
sendMessages = !sendMessages;
$.inidb.set('raffleSettings', 'raffleMSGToggle', sendMessages);
$.say($.whisperPrefix(sender) + 'Raffle warning messages have been ' + (sendMessages ? $.lang.get('common.enabled') : $.lang.get('common.disabled')) + '.');
return;
}
/**
* @commandpath raffle togglerepicks - Toggles if the same winner can be repicked more than one.
*/
if (action.equalsIgnoreCase('togglerepicks')) {
allowRepick = !allowRepick;
$.inidb.set('raffleSettings', 'noRepickSame', allowRepick);
$.say($.whisperPrefix(sender) + (allowRepick ? $.lang.get('rafflesystem.raffle.repick.toggle1') : $.lang.get('rafflesystem.raffle.repick.toggle2')));
return;
}
/**
* @commandpath raffle message [message] - Sets the raffle auto annouce messages saying that raffle is still active.
*/
if (action.equalsIgnoreCase('message')) {
if (subAction === undefined) {
$.say($.whisperPrefix(sender) + $.lang.get('rafflesystem.message.usage'));
return;
}
raffleMessage = arguments.substring(action.length() + 1);
$.inidb.set('raffleSettings', 'raffleMessage', raffleMessage);
$.say($.whisperPrefix(sender) + $.lang.get('rafflesystem.message.set', raffleMessage));
return;
}
/**
* @commandpath raffle messagetimer [minutes] - Sets the raffle auto annouce messages interval. 0 is disabled.
*/
if (action.equalsIgnoreCase('messagetimer')) {
if (subAction === undefined || isNaN(parseInt(subAction))) {
$.say($.whisperPrefix(sender) + $.lang.get('rafflesystem.timer.usage'));
return;
}
messageInterval = parseInt(subAction);
$.inidb.set('raffleSettings', 'raffleMessageInterval', messageInterval);
$.say($.whisperPrefix(sender) + $.lang.get('rafflesystem.timer.set', messageInterval));
return;
}
}
/**
* @info command for entering the raffle.
*/
if (command.equalsIgnoreCase(keyword.substring(1))) {
enter(sender, event.getTags());
}
});
/**
* @event initReady
* @info event sent to register commands
*/
$.bind('initReady', function() {
if ($.bot.isModuleEnabled('./systems/raffleSystem.js')) {
$.registerChatCommand('./systems/raffleSystem.js', 'raffle', 2);
$.registerChatSubcommand('raffle', 'open', 2);
$.registerChatSubcommand('raffle', 'close', 2);
$.registerChatSubcommand('raffle', 'repick', 2);
$.registerChatSubcommand('raffle', 'results', 7);
$.registerChatSubcommand('raffle', 'subscriberbonus', 1);
$.registerChatSubcommand('raffle', 'regularbonus', 1);
$.registerChatSubcommand('raffle', 'togglemessages', 1);
$.registerChatSubcommand('raffle', 'togglerepicks', 1);
$.registerChatSubcommand('raffle', 'message', 1);
$.registerChatSubcommand('raffle', 'messagetimer', 1);
}
});
$.reloadRaffle = reloadRaffle;
})();
You can’t perform that action at this time.
