Bots Home
|
Create an App
FREE VOTE V2 BOT
Author:
everlast_69
Description
Source Code
Launch Bot
Current Users
Created by:
Everlast_69
/* * Title: FREE POLL V2 * Author: Everlast_69 (original code by Naturist_be) * Version: 2.0 (Original Version 1.0 (07/23/2013)) * Thanks to Naturist_be for your effort in creating this app. * Summary: FREE POLL Everyone can play! * * Description: (3/9/2020) everlast_69: below is the origional description followed by my modifications. Options: - Set the number of votes limit - Set at least 2 choices (maximum of 5) - Users can vote only once List of commands: Users only type the number of the choice they want !stat = display choices and stats Have fun! Naturist_be * * Modified by everlast_69 (3/9/2020) This app was oriionally created by Naturist_be on 07/23/2013, more then 6 years ago and has not been modified since. This was a good app but needed to be maintained over the years. Since this app was written many changes have happened on CB. This app no longer functions corrctly. There are many bots that modify messages by adding things like tokens tipped and other information to the beginning of the message. This causes this app to fail and not count votes. I have modified the way votes are made, so this does not happen, by doing the following modifications: 1. Modified to run as a bot 2. changed the vote syntax from a single number to !n. users now type !1 to vote for option 1 3. modified the onMessage function to find the !n anywhere in the message using Rergular Expressions. 4. stripped off the ! and passed the number to the original code. 5. Added a setting to set the frequency the app will display the voting options 6. added a cb.onEnter function to tell users when they enter the room that the poll is running 7. Added a setting to enter what the poll is for. 8. Added a command !end so the broadcaster can end the voting early 9. Added a command !help to provide help on how to vote. 12/16/2020 by everlast_69 1. corrected bug where item 5 was not being detected. 2. fixed advert display */ var total_voted = 0; var finished = false; var voters = []; var ADVERT_TIME = 5; var voting_for = ' '; var subject_is_set_with_0 = false; var timeout_ID = ""; cb.settings_choices = [ { name: 'voting_for', type: 'str', label: ' What are we voting for?', maxLength: 200 }, { name: 'display_time', type: 'int', label: 'status display time in minutes', minValue: 1, maxValue: 10, default: 3 }, { name: 'end_minutes', type: 'int', label: 'How many minutes to run vote 0 = no end time', maxValue: 999, default: 0 }, { name: 'max_votes', type: 'int', minValue: 1, label: "Limit of votes", maxValue: 99999, default: 50 }, { name: 'choice1', type: 'str', label: "Choice 1", maxLength: 200 }, { name: 'choiceamt1', type: 'int', minValue: 0, label: ' current number of votes for choice 1', default: 0, required: false }, { name: 'choice2', type: 'str', label: "Choice 2", maxLength: 200 }, { name: 'choiceamt2', type: 'int', minValue: 0, label: ' current number of votes for choice 2', default: 0, required: false }, { name: 'choice3', type: 'str', label: "Choice 3", maxLength: 200, required: false }, { name: 'choiceamt3', type: 'int', minValue: 0, label: ' current number of votes for choice 3', default: 0, required: false }, { name: 'choice4', type: 'str', label: "Choice 4", maxLength: 200, required: false }, { name: 'choiceamt4', type: 'int', minValue: 0, label: ' current number of votes for choice 4', default: 0, required: false }, { name: 'choice5', type: 'str', label: "Choice 5", maxLength: 200, required: false }, { name: 'choiceamt5', type: 'int', minValue: 0, label: ' current number of votes for choice 5', default: 0, required: false }]; cb.onMessage(function (msg) { // 1 var exp = /!(stat|end|help|[1-5])/; // regular expression to find !stat or !n with n = 1 to 5 var found = exp.test(msg.m); if (found) { //2 var cmd = exp.exec(msg.m); msg['X-Spam'] = true; switch (cmd[1]) { case "stat": vote_tracker.send_stats(msg.user); break; case "help": vote_tracker.send_help(msg.user); break; case "end": if (msg.user != cb.room_slug) { cb.sendNotice( 'Only the broadcaster may use this command', msg.user, "#ff0000", "#ffffff", 'bold'); } else { vote_tracker.end_voting(msg.user); } break; case "1": case "2": case "3": case "4": case "5": if (msg.user == cb.room_slug) // if broadcaster do not count. { msg['X-Spam'] = false; break; } var vote = cmd[1].toString(); if (testvoter(msg.user) == 1) { for (var i = 0; i < all_choices().length; i++) { if (all_choices()[i].charAt(0) == vote) { vote_tracker.track_vote(0, all_choices()[ i]); voters.push( { user: msg.user }); total_voted++; } } } // end testvoter } // end switch if (cmd[1] != 'stat' && cmd[1] != 'help' && msg['X-Spam'] == true) { update_subject(); drawPanel(); } } // end of If Found }); function drawPanel() { var bestchoice_display = '\u2605 start voting! \u2605'; var nbchoice = 0; if (vote_tracker.bestchoice) { nbchoice = vote_tracker.bestchoice.nb_voted; bestchoice_display = vote_tracker.bestchoice.choice + ' (' + nbchoice + ')'; } if (!finished) cb.sendNotice( 'WELCOME TO FREE POLL!\nTop choice: ' + bestchoice_display + '\nVotes remaining: ' + votes_remaining() + "/" + cb.settings.max_votes, "", "#666699", "#FFFFFF", "bold" ); else { cb.sendNotice( 'FREE POLL - by Naturist_be & Everlast_69\n' + "voting has ended for " + voting_for + "\n" + bestchoice_display + ' Elected\nThanks all for your vote!!', "", "#666699", "#FFFFFF", "bold"); } } cb.onEnter(function (user) { var msg = "Welcome " + user.user + ", " + cb.room_slug + " is taking a poll.\n"; msg += instructions(); cb.sendNotice(msg, user.user); }); function instructions() { var msg = "We are voting for " + voting_for + ".\nanyone, even greys. can vote. you don't need to have tokens\n"; msg += "to enter a vote type an exclamination point(!) followed by the number of the option you want to vote for.\n"; msg += "like this: !2 to vote for option 2. You can only vote one time."; return msg; } var prev_votes = []; var _all_choices_cache = null; function all_choices() { if (_all_choices_cache) { return _all_choices_cache; } var choices = []; for (var i = 1; i <= 5; i++) { var name = 'choice' + i; if (cb.settings[name]) { choices.push(i + ' - ' + cb.settings[name]); name = 'choiceamt' + i; prev_votes[i] = cb.settings[name]; //cb.sendNotice('all choices - ' + prev_votes[i]); } } _all_choices_cache = choices; return choices; } var vote_tracker = { _data: [], bestchoice: null, init: function () { for (var i = 0; i < all_choices().length; i++) { vote_tracker.track_vote(1, all_choices()[i]); } ADVERT_TIME = cb.settings.display_time; voting_for = cb.settings.voting_for; drawPanel(); update_subject(); advert(); }, track_vote: function (voteamount, choice) { var found = false; for (var i = 0; i < vote_tracker._data.length; i++) { if (vote_tracker._data[i].choice == choice) { vote_tracker._data[i].nb_voted += 1; found = true; } } if (!found) { var numvoted = get_prev_votes(choice); vote_tracker._data.push( { choice: choice, nb_voted: numvoted, }); } if (voteamount == 0) { cb.sendNotice("new vote for " + choice,"","#666699", "#FFFFFF", "bold"); } vote_tracker._update_bestchoice(); }, _update_bestchoice: function () { if (vote_tracker._data.length == 0) { return; } vote_tracker._data.sort(function (a, b) { return b.nb_voted - a.nb_voted }); if (vote_tracker._data[0].nb_voted > 0) { vote_tracker.bestchoice = vote_tracker._data[0]; } }, send_stats: function (user) { var dispuser = ''; if (user == cb.room_slug) { dispuser = ""; } else { dispuser = user; } var msg = ""; for (var i = 0; i < vote_tracker._data.length; i++) { if (i > 0) { msg += "\n"; } msg += vote_tracker._data[i].choice + " (" + vote_tracker ._data[i].nb_voted + " votes)"; } // cb.chatNotice(" Poll choices and stats: ", user, "#666699", "#FFFFFF", "bold"); cb.chatNotice("We are voting for \n" + voting_for + "\n Poll choices and stats: ", dispuser, "#666699", "#FFFFFF", "bold"); cb.chatNotice(msg, dispuser, "#666699", "#FFFFFF", "bold"); }, end_voting: function (user) { cb.sendNotice("Voting ended by " + cb.room_slug, "", "#666699", "#FFFFFF", "bold"); total_voted = cb.settings.max_votes; }, send_help: function (user) { var dispuser = ''; if (user == cb.room_slug) { dispuser = ""; } else { dispuser = user; } cb.sendNotice(instructions(), dispuser,"#666699", "#FFFFFF", "bold"); vote_tracker.send_stats(dispuser); } }; function get_prev_votes(choice) { var numvotes = 0; var index = choice.charAt(0); //cb.sendNotice(index + " " + choice); return prev_votes[index]; } function IsNullorEmpty(value) { if (value == null || value == undefined || value.length == 0) { return true; } else { return false; } } function votes_remaining() { var r = cb.settings.max_votes - total_voted; if (r < 1) { finished = true; return 0; } else { finished = false; return r; } } function testvoter(user) { for (var i = 0; i < voters.length; i++) { if (voters[i].user == user) { cb.chatNotice('You can only vote once!', user, "#FF0000", "#FFFFFF", "bold"); return 0; } } return 1; } function advert() { var msgt = ""; var msga = ""; //"Poll choices & stats: \n" for (var i = 0; i < vote_tracker._data.length; i++) { if (i > 0) { msga += "\n"; } msga += vote_tracker._data[i].choice + " (" + vote_tracker._data[i] .nb_voted + " votes)"; } if (finished == true && vote_tracker.bestchoice) { msgt = "Voting has ended for " ; msga += "\n" + vote_tracker.bestchoice.choice + " Elected"; } else { msgt = "We are voting for \n"; msga += "\nTo vote type !n where n = the number of your vote."; msga += "\nTo vote for option 3 type !3, for option 1 type !1 and so on"; } cb.chatNotice(msgt + voting_for + "\n Poll choices and stats: ", '', "#666699", "#FFFFFF", "bold"); cb.chatNotice(msga,'',"#666699", "#FFFFFF", "bold"); timeout_ID = cb.setTimeout(advert, (ADVERT_TIME * 60000)); } function update_subject() { var ft = ""; if (votes_remaining() == 0) { if (subject_is_set_with_0) { return; } subject_is_set_with_0 = true; } else { subject_is_set_with_0 = false; } if (vote_tracker.bestchoice) { if (finished == true) { ft = "Voting finished for " + voting_for + "\n" + vote_tracker.bestchoice.choice + " Elected"; } else { ft = "Top choice: " + vote_tracker.bestchoice.choice + "\n(" + votes_remaining() + " votes remaining) Type !stat to see choices and stats."; } //ft = vote_tracker.bestchoice.choice; } else { ft = "\u2605 start voting! \u2605 Type !stat to see choices and stats."; vote_tracker.send_stats(); //timeout_ID = cb.setTimeout(advert, (ADVERT_TIME * 60000)); } var new_subject = ft; //cb.sendNotice(ft); } vote_tracker.init();
© Copyright Chaturbate 2011- 2024. All Rights Reserved.