Bots Home
|
Create an App
EZ Timer test
Author:
ardchism
Description
Source Code
Launch Bot
Current Users
Created by:
Ardchism
cb.settings_choices = [ {name: 'color', label: 'Color', type: 'str', defaultValue: '#DC0000', required: false}, {name: 'commanders', label: 'Allow these users to operate the timer', type: 'str', defaultValue: 'codeanon', required: false}, {name: 'allow_mods', label: 'Allow all moderators to operate the timer', type: 'choice', choice1: 'Yes', choice2: 'No', defaultValue: 'No'}, {name: 'timer_cmd', label: 'Timer command', type: 'str', defaultValue: '/timer', required: false}, ]; var $ = JSON.parse(JSON.stringify(cb.settings)); var say = (message, user, color, wt) => cb.sendNotice(newLines(message), user, '', color || $.color || '#DC0000', wt || $.weight || 'bold'); var newLines = (input) => '|\u2007 ' + input.replace(/\n/g, '\n|\u2007 '); var isAllowed = (msg) => commanders.includes(msg.user) || (msg.is_mod && cb.settings.allow_mods === 'Yes'); var commanders = $.commanders ? $.commanders.split(/\s*,\s*/).filter((obj) => obj.length) : []; commanders.push(cb.room_slug); cb.onMessage(function (msg) { if (isAllowed(msg)) ezTimer(msg); }); function ezTimer (msg) { msg.m = msg.m.trim(); var index = msg.m.includes(' ') ? msg.m.indexOf(' ') : Infinity; var cmd = msg.m.slice(0, index); var prm = msg.m.slice(index + 1); if (cb.settings.timer_cmd === cmd) { msg['X-Spam'] = true; cb.log('Command detected...'); zTimer.operations(prm, msg.user); } } function evaluate(str, user) { try { var result = eval(str); var notice = 'TYPE: ' + typeof result + '\nVALUE: '; result === void 0 ? notice += 'undefined' : notice += JSON.stringify(result, null, '\u2007\u2007\u2007\u2007'); cb.setTimeout(() => {cb.sendNotice(newLines(notice), user, '', '#00CC00', '', '')}, 100); } catch (e) { cb.setTimeout(() => {cb.sendNotice(newLines(e.name + ': ' + e.message), user, '', '#FF0000', '', '')}, 100); } } var zTimer = {seconds: Infinity, timeLeft: Infinity}; zTimer.operations = function (args, user) { var p = args.split(/\s+/); if (!p[0]) { zTimer.help(user); return; } var op = p[0].toLowerCase(); switch (op) { case 'add': zTimer.add(true, p.slice(1)); break; case 'sub': zTimer.add(false, p.slice(1)); break; case 'stop': case 'cancel': zTimer.cancel(); break; case 'text': zTimer.setGoal(p.slice(1)); break; case 'help': zTimer.help(user); break; default: zTimer.start(p); break; } }; zTimer.goal = ''; zTimer.prep = 'in'; zTimer.cap = (text) => text.charAt(0).toUpperCase() + text.slice(1); zTimer.setGoal = (input) => {if (input && input.trim() && isFinite(zTimer.timeLeft)) zTimer.goal = input.trim()}; zTimer.say = (txt) => say(':star_olivia_dellvine ' + txt.replace(/\n/g, ' :star_olivia_dellvine\n :star_olivia_dellvine ') + ' :star_olivia_dellvine'); zTimer.help = function (user) { var sep = '\n' + '\u2007'.repeat(4); var output = [ '------------------------------------', '* EZ Timer --- Command List *', '------------------------------------', $.timer_cmd + ' [n]' + sep + 'Start [n]-minute timer', $.timer_cmd + ' for [n] [activity]' + sep + 'Start [n]-minute timer for continuous activity', $.timer_cmd + ' at [n] [activity]' + sep + 'Start [n]-minute timer with activity when timer reaches 0', $.timer_cmd + ' text [activity]' + sep + 'Update the current activity or action', $.timer_cmd + ' stop' + sep + 'Cancel the timer', $.timer_cmd + ' help' + sep + 'Show this menu again', ].join('\n'); say(output, user); }; zTimer.add = function (positive, p) { if (isFinite(zTimer.timeLeft)) { cb.log(positive ? 'Adding to timer...' : 'Subtracting from timer...'); var n = Number(p[0]); if (n) { cb.log('Adding ' + n); var phrase; if (positive) { phrase = 'added to'; } else { phrase = 'removed from'; n = n * -1; } zTimer.timeLeft += Math.round(n * 60); zTimer.say(n + ' minutes ' + phrase + ' the timer.'); } } }; zTimer.start = function (p) { cb.log('Starting timer...'); if (isNaN(Number(p[0]))) { p[0] = String(p[0]).toLowerCase().replace('at','in'); if (!['for','in','at'].includes(p[0])) return; zTimer.prep = p[0]; p.splice(0, 1); } else { zTimer.prep = 'in'; } var min = p[0]; var goal = p.slice(1).join(' '); if (min) { zTimer.timeLeft = Math.round(min * 60); zTimer.seconds = Math.round(min * 60); zTimer.goal = goal || ''; if (zTimer.goal) { if (zTimer.prep === 'in') goal = '\n' + zTimer.cap(goal) + ' at time up!'; if (zTimer.prep === 'for') goal = '\n' + zTimer.cap(goal) + ' until time up!'; } say(':starx\n' + min + ' minute timer set!' + goal + '\n:starx'); } }; zTimer.cancel = function () { cb.log('Canceling timer...'); if (isFinite(zTimer.timeLeft)) { zTimer.timeLeft = Infinity; zTimer.seconds = Infinity; zTimer.goal = ''; zTimer.say('Timer cancelled!'); } }; zTimer.go = function () { if (zTimer.timeLeft > 0) { if (zTimer.timeLeft % 10 === 0) cb.log(zTimer.timeLeft + ' seconds left...'); if (zTimer.timeLeft % 60 === 0 && zTimer.timeLeft !== zTimer.seconds) { var mins = zTimer.timeLeft / 60; var mins_word = ' minute' + (mins === 1 ? '' : 's'); if (zTimer.goal) { zTimer.say(zTimer.cap(zTimer.goal) + ' ' + zTimer.prep + ' ' + mins + (zTimer.prep === 'for' ? ' more ' : '') + mins_word + '!'); } else { zTimer.say(mins + mins_word + ' left!'); } } zTimer.timeLeft--; } else { zTimer.say(':timesup\n:timesup\n:timesup'); zTimer.timeLeft = Infinity; zTimer.seconds = Infinity; zTimer.goal = ''; } cb.setTimeout(zTimer.go, 1000); }; zTimer.go(); zTimer.help(cb.room_slug);
© Copyright Chaturbate 2011- 2024. All Rights Reserved.