Bots Home
|
Create an App
last poll bot
Author:
polymathcs
Description
Source Code
Launch Bot
Current Users
Created by:
Polymathcs
// Based on Token Poll by Fantasycouplered69 as modified by 18yearoldcouple // added starting votes for multiple session voting by mikeimooch //Warning Color var fg_warn = '#FF0000'; var foreground = cb.settings.foreground; var background = cb.settings.background; var txt_never = 'When I end it'; var txt_minutes = 'After x minutes'; var txt_votecount= 'After x votes'; var txt_wincount = 'When one option reaches x votes'; var opt_labels = [cb.settings.opt1_label, cb.settings.opt2_label, cb.settings.opt3_label, cb.settings.opt4_label, cb.settings.opt5_label]; var opt_votes = [cb.settings.opt1_start, cb.settings.opt2_start, cb.settings.opt3_start, cb.settings.opt4_start, cb.settings.opt5_start]; var opt_tokens = [cb.settings.opt1_tokens, cb.settings.opt2_tokens, cb.settings.opt3_tokens, cb.settings.opt4_tokens, cb.settings.opt5_tokens]; var mins_remain = cb.settings.poll_count; var votes_remain = cb.settings.poll_count; var poll_running = true; // bot options cb.settings_choices = [ {name: 'foreground', type: 'str', minLength: 1, maxLength: 7, label: 'Poll Text Color (Hex color codes start with #):', defaultValue:"#810541"}, {name: 'background', type: 'str', minLength: 1, maxLength: 7, label: 'Poll Background Color:', defaultValue:"#FFDFDD"}, {name: 'poll_title', type: 'str', minLength: 1, maxLength: 255, label: 'Poll Title'}, {name: 'board_interval', type: 'int', minValue: 1, default: 3, label: 'Board Display Interval (mins)'}, {name: 'poll_mode', type: 'choice', label: 'Poll ends...', choice1: txt_never, choice2: txt_minutes, choice3: txt_votecount, choice4: txt_wincount, defaultValue: txt_minutes}, {name: 'poll_count', type: 'int', minValue: 1, default: 15, label: '... Where x is ...'}, {name: 'opt1_label', type: 'str', minLength: 1, maxLength: 255, label: 'Option 1'}, {name: 'opt1_start', type: 'int', minValue: 0, default: 0, label: 'Option 1 starting votes'}, {name: 'opt1_tokens', type: 'int', minValue: 1, default: 10, label: 'Option 1 tokens'}, {name: 'opt2_label', type: 'str', minLength: 1, maxLength: 255, label: 'Option 2'}, {name: 'opt2_start', type: 'int', minValue: 0, default: 0, label: 'Option 2 starting votes'}, {name: 'opt2_tokens', type: 'int', minValue: 1, default: 11, label: 'Option 2 tokens'}, {name: 'opt3_label', type: 'str', minLength: 1, maxLength: 255, label: 'Option 3', required: false}, {name: 'opt3_start', type: 'int', minValue: 0, default: 0, label: 'Option 3 starting votes', required: false}, {name: 'opt3_tokens', type: 'int', minValue: 0, default: 0, label: 'Option 3 tokens', required: false}, {name: 'opt4_label', type: 'str', minLength: 1, maxLength: 255, label: 'Option 4', required: false}, {name: 'opt4_start', type: 'int', minValue: 0, default: 0, label: 'Option 4 starting votes', required: false}, {name: 'opt4_tokens', type: 'int', minValue: 0, default: 0, label: 'Option 4 tokens', required: false}, {name: 'opt5_label', type: 'str', minLength: 1, maxLength: 255, label: 'Option 5', required: false}, {name: 'opt5_start', type: 'int', minValue: 0, default: 0, label: 'Option 5 starting votes', required: false}, {name: 'opt5_tokens', type: 'int', minValue: 0, default: 0, label: 'Option 5 tokens', required: false} ]; function showBoard(user) { // var response1 = "Here's the Token Poll board:\n"; var response1 = '------- Tip to Vote Poll board' + ('' == user ? ' (sent to all)' : '') + ': -------\n'; var response2 = cb.settings.poll_title; if (!poll_running) { return; } var ids = []; for (var j in opt_votes) { ids.push({'votes':opt_votes[j], 'id':j}); } ids.sort(function (a, b) { return b['votes']-a['votes']; }); for (i=0; i<ids.length; i++) { if (0 != opt_tokens[ids[i]['id']]) { response2 += "\n - " + opt_labels[ids[i]['id']] + " ["+ opt_votes[ids[i]['id']] + " votes]: " + opt_tokens[ids[i]['id']] + " tokens"; } } var response3 = ''; switch (cb.settings.poll_mode) { case txt_minutes: response3 = mins_remain + ' minute' + (mins_remain > 1 ? 's' : '') + ' remaining to vote\n'; break; case txt_votecount: response3 = votes_remain + ' vote' + (votes_remain > 1 ? 's' : '') + ' remaining before poll closes\n'; break; case txt_wincount: response3 = 'First option to ' + cb.settings.poll_count + ' votes wins!\n'; break; } response3 += 'Simply tip the shown token amounts to register your vote. Type !poll at any time to see poll board.'; if (undefined == user) { // we were triggered by a timeout user = ''; cb.setTimeout(showBoard, cb.settings.board_interval*60*1000); } // cb.sendNotice(response1, user); cb.sendNotice(response1 + response2, user, cb.settings.background, cb.settings.foreground, 'bold'); // if (undefined != response3) { cb.sendNotice(response3, user, cb.settings.background, cb.settings.foreground); // } // cb.sendNotice(response4, user); } function sanitizeBoard() { for (i=1; i<opt_tokens.length; i++) { // start at option 2 if (0 != opt_tokens[i] && '' == opt_labels[i]) { // make sure no option labels are left blank cb.sendNotice("Tip to Vote Poll Warning: Label for option " + (i+1) + " is blank -- removing from poll board!", cb.soom_slug, cb.settings.background, fg_warn, 'bold'); opt_tokens[i] = 0; continue; } for (j=0; j<i; j++) { // check all options before this one if (0 != opt_tokens[i] && opt_tokens[i] == opt_tokens[j]) { // make sure we don't have two options with the same token amounts cb.sendNotice("Tip to Vote Poll Warning: Token amount for option " + (i+1) + " is not unique -- removing from poll board!", cb.soom_slug, cb.settings.background, fg_warn, 'bold'); opt_tokens[i] = 0; break; } else if (0 != opt_tokens[i] && opt_labels[i] == opt_labels[j]) { // make sure we don't have two options with the same label cb.sendNotice("Tip to Vote Poll Warning: Label for option " + (i+1) + " is not unique -- removing from poll board!", cb.soom_slug, cb.settings.background, fg_warn, 'bold'); opt_tokens[i] = 0; break; } } } } function showWinner() { var options = []; for (i=0; i<opt_tokens.length; i++) { options[i] = i; } options.sort(function(a, b){return opt_votes[b]-opt_votes[a]}); var win_count = 1; for (i=1; i<opt_tokens.length; i++) { if (opt_votes[options[i]] != opt_votes[options[0]]) { break; } if (0 != opt_tokens[options[i]]) { win_count++; } } var response1 = '------- Tip to Vote Poll has ended! -------\n'; var response2 = 'Winner' + (win_count > 1 ? 's (' + win_count + '-way tie)' : '') + ':'; for (i=0; i<win_count; i++) { if (opt_tokens[options[i]] != 0) { response2 += "\n - " + opt_labels[options[i]] + ": " + opt_votes[options[i]] + " votes"; } } cb.sendNotice(response1 + response2, '', cb.settings.background, cb.settings.foreground, 'bold'); } function checkPollEnd() { var _poll_running = poll_running; switch (cb.settings.poll_mode) { case txt_never: return; case txt_minutes: if (mins_remain < 1) { poll_running = false; } break; case txt_votecount: if (votes_remain < 1) { poll_running = false; } break; case txt_wincount: for (i=0; i<opt_tokens.length; i++) { if (opt_votes[i] >= cb.settings.poll_count) { poll_running = false; } } break; } if (_poll_running && !poll_running) { showWinner(); } } function tickTimer() { mins_remain--; checkPollEnd(); if (mins_remain > 0) { cb.setTimeout(tickTimer, 60*1000); } } cb.onMessage(function (m) { var is_mod = (cb.room_slug == m['user'] || m['is_mod']); switch (m['m']) { case '!poll': // other users don't need to see this message m['X-Spam'] = !is_mod; if (is_mod) { if (poll_running) { showBoard(''); } else { showWinner(); } } else { if (poll_running) { showBoard(m['user']); } else { showWinner(); } } break; case '!endpoll': if (!is_mod) { break; } poll_running = false; showWinner(); break; } if (m['m'].startsWith('/')) { var message = m['m'].split(" "); m['X-Spam'] = true; if (is_mod) { switch (message[0]) { case '/addvote': if (opt_labels[0].includes(message[2])) { opt_votes[0] = (opt_votes[0] + parseInt(message[1])); cb.sendNotice(m['user'] + ' has added ' + message[1] + ' votes to ' + opt_labels[0] + '', '', background, foreground, 'red' ); } else if (opt_labels[1].includes(message[2])) { opt_votes[1] = (opt_votes[1] + parseInt(message[1])); cb.sendNotice(m['user'] + ' has added ' + message[1] + ' votes to ' + opt_labels[1] + '', '', background, foreground, 'red' ); } else { cb.sendNotice('the commands was not correct original message >' + m['m'], m['user'], background, foreground); } break; case '/subvote': if (opt_labels[0].includes(message[2])) { opt_votes[0] = (opt_votes[0] - parseInt(message[1])); cb.sendNotice(m['user'] + ' has subtracted ' + message[1] + ' votes to ' + opt_labels[0] + '', '', background, foreground); } else if (opt_labels[1].includes(message[2])) { opt_votes[1] = (opt_votes[1] - parseInt(message[1])); cb.sendNotice(m['user'] + ' has subtracted ' + message[1] + ' votes to ' + opt_labels[1] + '', '', background, foreground); } else { cb.sendNotice('the commands was not correct original message >' + m['m'], m['user'], background, foreground); } break; // case '/fmm': // var split_index = m['m'].indexOf(' '); // var second_index = m['m'].indexOf(' ', split_index + 1); // var sender = message[1]; // var msg = m['m'].substring(second_index); // cb.sendNotice('(Private from ' + sender + ') ' + msg, cb.room_slug, // '#fbfcb6', '#dc0000', 'bold'); // break; default: //cb.sendNotice(m['user']+ " said " + m['m'], 'colomtn29', '#fbfcb6', '#dc0000', 'bold'); //cb.sendNotice(m['user']+ " said " + m['m'], 'polymath614', '#fbfcb6', '#dc0000', 'bold'); } } else { // this is where you could add a notice to see if people are running commands that should not be return m['m']; } } }); cb.onEnter(function (user) { cb.sendNotice("Hello " + user['user'] + ", the Tip to Vote bot is currently active in this room.\nType !poll to see voting options.", user['user'], cb.settings.background, cb.settings.foreground); }); cb.onTip(function (tip) { var amount = parseInt(tip['amount']); if (!poll_running) { return; } switch (amount) { case 132: case 264: case 396: case 528: case 660: case 792: case 924: case 1056: case 1188: case 1320: case 1452: case 1584: cb.sendNotice(tip['from_user'] + " Sorry that number is a multiple of both of the polls so your vote did not count please inform a mod or the model where you wanted the votes to go", tip['from_user'], background, foreground); cb.sendNotice(tip['from_user'] + " tipped but it is a multiple of both so you need to ask what one he was voting for.", cb.room_slug, background, foreground); cb.sendNotice(tip['from_user'] + " tipped but it is a multiple of both so you need to ask what one he was voting for.", '', background, foreground, 'bold', 'red'); //cb.sendNotice(tip['from_user'] + " tipped but it is a multiple of both so you need to ask what one he was voting for.", ' ', background, foreground,'bold', 'red'); break; default: for (i=0; i<opt_tokens.length; i++) { //cb.sendNotice(amount % opt_tokens[i],'',background, foreground); if ((amount % opt_tokens[i]) === 0) { var numberOfVotes = (amount / opt_tokens[i]); cb.sendNotice(tip['from_user'] + " has voted for " + opt_labels[i] + " " + numberOfVotes + " times.", '', background, foreground); opt_votes[i] = (opt_votes[i] + numberOfVotes); if (txt_votecount == cb.settings.poll_mode) { votes_remain--; } checkPollEnd(); break; //cb.sendNotice(tip['from_user'] + " sorry that tip did not count as it is possible that it could be for both please tip note what you would like the votes to count towards or tell a mad", '', background, foreground) } } } }); sanitizeBoard(); showBoard(); if (txt_minutes == cb.settings.poll_mode) { cb.setTimeout(tickTimer, 60*1000); }
© Copyright Chaturbate 2011- 2024. All Rights Reserved.