Bots Home
|
Create an App
Basic Timer Objects Reference
Author:
nicky7282
Description
Source Code
Launch Bot
Current Users
Created by:
Nicky7282
/******************************************************************** ** Basic timer object example for multiple timers ** by Nicky7282 (druixure@yahoo.com) (twitter: @nicky7282) ** ** Credit me if you use this. If you don't, I will find you. ** Feel free to copy this into your bot with credit. ********************************************************************/ //Describes a new kind of "thing" called a Timer function Timer(name, seconds){ this.name = name; //Timers can have names. this.seconds = seconds; //Timers can and should have seconds amirite? this.startTicking = startTicking;//Timers can start ticking... I think? maybe? Pretty sure timers do that. } //Something Timers can do: function startTicking() { if (this.seconds > 0){ //If the timer we're looking at has seconds left, write them to the chat log (/debug in chat) cb.log(this.name + ' has ' + this.seconds + ' seconds left'); //Don't forget to subtract one second from 'this' timer too. (ayy we're learning keywords) this.seconds = this.seconds - 1; } else { //If this timer ran out of seconds, delete it and stop ticking by leaving the function. cb.log(this.name + ' has expired'); delete timers[this.name]; delete this; return; } cb.setTimeout(startTicking.bind(this), 1000); //Start the function over again after waiting 1000 milliseconds. } //We need a place to put our timers, so we'll put them in a "thing" called "timers" var timers = {}; cb.onMessage(function (msg) { //msg is a "thing" that contains a message "m" and a user "user" //If the first character of the message is / or ! check if it's a command. if ( (msg.m.charAt(0)=='/') || (msg.m.charAt(0)=='!') ){ //Put each word into a numbered list that starts at 0. var word = msg.m.split(' '); //We want commands to be case Insensitive, so we'll make the first word lowercase. word[0] = word[0].toLowerCase(); //If the first word is "/timer" if (word[0] == '/timer'){ //The timer's name is the second word. var name = word[1]; //The amount of seconds is the third word. If the user gave us a decimal, we'll round it down to the nearest integer. var seconds = Math.floor(parseFloat(word[2])); //If there wasn't a second word or it was a number, don't make the timer and tell them to try again. if (!name || (name == parseFloat(name))){ //(Who names a timer a number anyway? Jeeze...) cb.sendNotice('You did not specify a name for the timer. example: "/timer timer1 60"', msg.user, '', '#FF0000', 'bold'); } //If there wasn't a third word or it was 0, or it was Not-a-Number, don't make the timer and tell them to try again. else if (!seconds || isNaN(seconds)){ cb.sendNotice('You forgot to specify a number of seconds, or it was not a number. example: "/timer timer1 60"', msg.user, '', '#FF0000', 'bold'); } //If the timer already exists, reset it's value. else if (timers[name]){ timers[name].seconds = seconds; } //If they entered it correctly, tell them, and... else { cb.sendNotice('Okay. Starting timer '+name+' with '+seconds+' seconds.', msg.user, '', '#FF0000', 'bold'); //Make a new timer, and put it into the place where we put our timers. We will access it by "name" timers[name] = new Timer(name, seconds); //And tell the timer to start ticking. timers[name].startTicking(); } } //If the first word is "/addtime" else if (word[0] == '/addtime'){ //The timer's name is the second word. var name = word[1]; //The amount of seconds is the third word. If the user gave us a decimal, we'll round it down to the nearest integer. var seconds = Math.floor(parseFloat(word[2])); //If the timer doesn't exist, tell the user and ask them to try again. if (!timers[name]){ var timerNames = ''; for (i in timers){ timerNames = timerNames + timers[i].name + ', '; } if (timerNames == ''){ cb.sendNotice('There are no running timers to add time to.', msg.user, '', '#FF0000', 'bold'); } else { cb.sendNotice('That timer doesn\'t exist. Please pick from the following timers:\n' + timerNames, msg.user, '', '#FF0000', 'bold'); } } //If there wasn't a second word or it was a number, don't edit the timer and tell them to try again. else if (!name || (name == parseFloat(name))){ cb.sendNotice('You did not specify a name for the timer. example: "/addtime timer1 60"', msg.user, '', '#FF0000', 'bold'); } //If there wasn't a third word or it was 0, or it was Not-a-Number, don't edit the timer and tell them to try again. else if (!seconds || isNaN(seconds)){ cb.sendNotice('You forgot to specify a number of seconds, or it was not a number. example: "/addtime timer1 60"', msg.user, '', '#FF0000', 'bold'); } //If they entered it correctly, tell them, and... else { cb.sendNotice('Okay. Adding '+seconds+' to timer '+name+'.','#FF0000', 'bold'); timers[name].seconds = timers[name].seconds + seconds; } } } });
© Copyright Chaturbate 2011- 2024. All Rights Reserved.