Bots Home
|
Create an App
Timer Bot
Author:
shivatest1
Description
Source Code
Launch Bot
Current Users
Created by:
Shivatest1
/** * Timer Bot * * Author: Harry241 * Version: 1.2 * */ /* Bot Variables */ var timerActive = false; var timerID = 0; var timerSeconds = 0; var timerTokens = 0; var timerText = ''; var timerDate; var timerTextColor = ''; var timerBGColor = ''; // #D8BFD8 #D2B48C var alertTextColor = '#A0522D'; var alertBGColor = '#EEE8AA'; var cmdTextColor = '#483D8B'; var cmdBGColor = ''; var shy = '\u00ad'; // soft hyphen, ­ /* Chaturbate settings */ cb.settings_choices = [ { name: 'GoalText', label: 'The text to use for the timer ("5 minutes <text>")', type: 'str', minLength: 0, maxLength: 255, required: true, defaultValue: 'left to reach goal!' }, { name: 'TextColor', label: 'The text color to use for the notices', type: 'str', minLength: 0, maxLength: 6, required: false, defaultValue: '483D8B' }, { name: 'BGColor', label: 'The background color to use for the notices', type: 'str', minLength: 0, maxLength: 6, required: false, defaultValue: 'E6E6FA' }, ]; cb.onMessage(function (msg) { var u = msg['user']; var isModel = (u == cb.room_slug); var isMod = msg['is_mod']; if (isModel) { // if this is a timer command if (msg['m'].search(/^ *!timer.*$/) > -1) { msg['X-Spam'] = true; var p; // !timer <mins> <tks> <text> if (msg['m'].search(/^ *!timer +(\d+)( +)?(\d+)?(.*)$/) > -1) { p = msg['m'].match(/^ *!timer +(\d+)( +)?(\d+)?(.*)$/); cb.log('params: ' + p); startTimer(p[1],p[3],p[4]); // !timer <command> } else if (msg['m'].search(/^ *!timer +(\S+) *$/) > -1) { p = msg['m'].match(/^ *!timer +(\S+) *$/); cb.log('params: ' + p); switch (p[1]) { case 'stop': cb.log('timer stop command'); // stop the timer stopTimer(); break; case 'notice': cb.log('timer notice command'); // show the timer notice showTimerNotice(); break; case 'info': cb.log('timer info command'); // show the timer info showTimerInfo(); break; default: cb.log('invalid command parameter'); break; } } else { cb.log('timer help'); // show the timer help showTimerHelp(); } } } if (msg['X-Spam']) { msg['background'] = '#F0F0F0'; msg['c'] = '#909090'; } return msg; }); cb.onTip(function (tip) { var amountTipped = parseInt(tip['amount']); if (timerActive && (timerTokens > 0)) { timerTokens = Math.max(0,timerTokens - amountTipped); if (timerTokens == 0) { // tokens goal was reached timerNotice('The goal was reached!'); stopTimer(); } } }); function timerNotice(msg,user) { if (user) { cb.chatNotice(msg,user,alertBGColor,alertTextColor,''); } else { cb.chatNotice(msg,'',timerBGColor,timerTextColor,'bold'); } } function startTimer(mins,tokens,text) { if (timerActive) { cb.log('active timer: ' + timerID); // there is an active timer timerNotice('There is an active timer, use "!timer stop" to stop the current timer.'); return false; } timerID++; timerActive = true; timerSeconds = (mins*60); timerTokens = (tokens)?tokens:0; timerText = (text)?text:cb.settings.GoalText; timerFunc(timerID,0); } function stopTimer() { cb.log('stopTimer()'); if (timerActive) { timerActive = false; timerSeconds = 0; timerTokens = 0; timerNotice('The timer has been stopped.'); } else { timerNotice('There is no active timer.',cb.room_slug); } } function showTimerNotice() { cb.log('showTimerNotice()'); if (timerActive) { var l = getTimeLeft(); timerNotice(l + ' ' + timerText); } else { timerNotice('There is no active timer.',cb.room_slug); } } function showTimerInfo() { cb.log('showTimerInfo()'); if (timerActive) { var l = getTimeLeft(); var txt = 'Time left: ' + l; if (timerTokens > 0) { txt += '\nTokens remaining: ' + timerTokens; } txt += '\nNotice text: "... ' + timerText + '"'; timerNotice(txt,cb.room_slug); } else { timerNotice('There is no active timer.',cb.room_slug); } } function getTimeLeft() { var ms = Math.max(1,new Date() - timerDate); var t = timerSeconds - Math.round(ms/1000); var m = Math.floor(t/60); var s = t - (m*60); cb.log('ms: ' + ms); var l = (m>0)?m+' minute'+((m>1)?'s':''):''; l += (s>0)?((m>0)?', ':'')+s+' second'+((s>1)?'s':''):''; return l; } function showTimerHelp() { cb.log('showTimerHelp()'); var txt = '' + 'use "!timer <minutes> [tokens] [text]" to start the timer' + '\n<minutes> = the number of minutes for the timer' + '\n[tokens] = optional, the number of tokens needed to reach the goal' + '\n[text] = optional, the display text for the time left notification' + '\n' + '\nuse "!timer stop" to stop the current timer' + '\nuse "!timer info" to show information about the current timer (model)' + '\nuse "!timer notice" to manually show how much time is left (public)' + '\n' + '\nExamples:' + '\n"!timer 5" - starts a 5 minute timer' + '\n"!timer 15 1500" - starts a 15 minute timer with a goal of 1500 tokens' + '\n"!timer 10 left to decide for topic!" - starts a 10 minute timer displaying "10 minutes left to decide for topic!" notices to the public'; timerNotice(txt,cb.room_slug); } function timerFunc(tID,sec) { cb.log('timerFunc(' + tID + ',' + sec + ')'); if ((!timerActive) || (timerID != tID)) { cb.log('timerActive: ' + timerActive); cb.log('timerID: ' + timerID + ' (' + tID + ')'); return false; } var m,s,r,t; timerSeconds = timerSeconds - sec; if (timerSeconds <= 0) { cb.log('timer done..'); timerNotice('The timer is done!'); timerActive = false; return false; } m = Math.floor(timerSeconds/60); if (m >= 30) { r = m%10; cb.log('r (>=30): ' + r); if (r==0) { r = 10; } s = r*60; } else if (m >= 10) { r = m%5; cb.log('r (>=10): ' + r); if (r==0) { r = 5; } s = r*60; } else if (m > 1) { s = 60; } else if (m == 1) { s = 30; } else { if (timerSeconds == 30) { s = 20; } else { s = 10; } } var timestr = ''; if (m>0) { timestr = m + ' minute'+((m>1)?'s':''); } else { timestr = timerSeconds + ' second'+((s>1)?'s':''); } timerNotice(timestr + ' ' + timerText); t = s*1000; cb.log('timeout: ' + t + 'ms (' + tID + ')'); timerDate = new Date(); cb.setTimeout(timerFunc.bind(this,tID,s),t); } function init() { cb.log('reading settings'); if (cb.settings.TextColor != '') { timerTextColor = '#' + cb.settings.TextColor; } if (cb.settings.BGColor != '') { timerBGColor = '#' + cb.settings.BGColor; } timerNotice('type "!timer" for options',cb.room_slug); } init();
© Copyright Chaturbate 2011- 2024. All Rights Reserved.