Bots Home
|
Create an App
Timer Bot Flex Interval
Author:
noiett
Description
Source Code
Launch Bot
Current Users
Created by:
Noiett
/** * Title: Timer Bot Flex Interval * Version: 1.4 * Author: noiett * Email: noiett.cb@gmail.com * Twitter: @biosandapps * * Description: This bot is a modification of Timer Bot by Harry241 * Custom timer interval has been added. * Bot can now be used by room moderators. */ /* Bot Variables */ var timerActive = false; var timerID = 0; var timerSeconds = 0; var timerIntervalSeconds = 0; var timerTokens = 0; var timerInterval = 1; var timerText = ''; var timerDate; var timerTextColor = ''; var timerBGColor = ''; // #D8BFD8 #D2B48C var timerMods = true; var alertTextColor = '#A0522D'; var alertBGColor = '#EEE8AA'; var cmdTextColor = '#483D8B'; var cmdBGColor = ''; var shy = '\u00ad'; // soft hyphen, ­ /* Chaturbate settings */ cb.settings_choices = [ { name: 'AllowMods', label: 'Allow mods to use Timer Bot commands?', type: 'choice', choice1: 'Yes', choice2: 'No', required: true, defaultValue: 'Yes' }, { 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: 'CustomInterval', type:'int', minValue: 1, maxValue: 30, defaultValue: 1, label: 'Interval between notices before the 5-min mark (mins)' }, { 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 || (isMod && timerMods)) { // 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(u); break; case 'notice': cb.log('timer notice command'); // show the timer notice showTimerNotice(u); break; case 'info': cb.log('timer info command'); // show the timer info showTimerInfo(u); break; default: cb.log('invalid command parameter'); break; } } else if (msg['m'].search(/^ *!timer +(\S+)( +)?(\d+)*$/) > -1) { p = msg['m'].match(/^ *!timer +(\S+)( +)?(\d+)*$/); cb.log('params: ' + p); switch (p[1]) { case 'interval': // set a new interval por the timer cb.log('timer interval command'); setTimerInterval(p[3], u); break; default: cb.log('invalid command parameter'); break; } } else { cb.log('timer help'); // show the timer help showTimerHelp(u); } } } 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) { if (user == 'red') { cb.chatNotice(msg,'',alertBGColor,alertTextColor,'',user); } else { 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(target) { cb.log('stopTimer()'); if (timerActive) { timerActive = false; timerSeconds = 0; timerTokens = 0; timerNotice('The timer has been stopped.'); } else { timerNotice('There is no active timer.',target); } } function showTimerNotice(target) { cb.log('showTimerNotice()'); if (timerActive) { var l = getTimeLeft(); timerNotice(l + ' ' + timerText); } else { timerNotice('There is no active timer.',target); } } function showTimerInfo(target) { cb.log('showTimerInfo()'); if (timerActive) { var l = getTimeLeft(); var txt = 'Time left: ' + l; txt += '\nInterval: ' + timerInterval + ' minute' + (timerInterval > 1 ? 's' : ''); if (timerTokens > 0) { txt += '\nTokens remaining: ' + timerTokens; } txt += '\nNotice text: "... ' + timerText + '"'; timerNotice(txt,target); } else { timerNotice('There is no active timer.',target); } } function setTimerInterval(value, target) { cb.log('setTimerInterval()'); timerInterval = parseInt(value); if (timerActive) { timerNotice('Timer interval has been set to ' + value + ' minutes.\n' + 'Current interval will not be affected by the new value.',target); } else { timerNotice('Timer interval has been set to ' + value + ' minutes.',target); } } 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(target) { 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)' + '\nuse "!timer interval <minutes>" to set a custom interval between notices' + '\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,target); } 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; timerIntervalSeconds = 0; return false; } m = Math.floor(timerSeconds/60); // s = seconds before sending next notification // m = minutes remaining // Aproaching the 5-min mark if (m > 5) { if (m >= 5 + timerInterval) { r = timerInterval; } else { r = m-5; } cb.log('r (>=5): ' + r); s = r*60; // Last 5 minutes } else if (m > 1) { s = 60; // Last minute } else if (m == 1) { s = 30; } else { if (timerSeconds == 30) { s = 20; } else { s = 10; } } var timestr = ''; timerIntervalSeconds = s; 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; } timerMods = cb.settings.AllowMods == 'Yes'; timerInterval = cb.settings.CustomInterval; timerNotice('type "!timer" for options', (timerMods? 'red' : cb.room_slug)); } init();
© Copyright Chaturbate 2011- 2024. All Rights Reserved.