Bots Home
|
Create an App
Token poll with default
Author:
tygreenhhs
Description
Source Code
Launch Bot
Current Users
Created by:
Tygreenhhs
var foreground = '#FFFFFF'; var fg_warn = '#FF0000'; var background = '#945FFF'; var txt_minutes = 'After x minutes'; var opt_votes = [0, 0, 0, 0, 0]; 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_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 poll_running = true; // bot options cb.settings_choices = [ {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_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_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_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_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_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_tokens', type: 'int', minValue: 0, default: 0, label: 'Option 5 tokens', required: false}, {name: 'default_opt', type: 'str', minLength: 1, maxLength: 255, label: 'Default option if no other options reach the minimum'}, {name: 'min_vote', type: 'int', minValue: 1, default: 20, label: 'minimum votes for an option to win'} ]; function showBoard(user) { // var response1 = "Here's the Token Poll board:\n"; var response1 = '---------- Token Poll board' + ('' == user ? ' (sent to all)' : '') + ': ----------\n'; var response2 = cb.settings.poll_title; if (!poll_running) { return; } for (i=0; i<opt_tokens.length; i++) { if (0 != opt_tokens[i]) { response2 += "\n - " + opt_labels[i] + " ["+ opt_votes[i] + " votes]: " + opt_tokens[i] + " tokens"; } } var response3 = ''; response3 = mins_remain + ' minute' + (mins_remain > 1 ? 's' : '') + ' remaining to vote\n'; response2 += "\n - " + cb.settings.default_opt + " is the default option"; response3 += 'Simply tip the shown token amounts to register your vote. Type !poll at any time to see poll board.\nIf no option recieves at least ' + cb.settings.min_vote + ' votes, the default option will win. '; 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, background, foreground, 'bold'); // if (undefined != response3) { cb.sendNotice(response3, user, background, 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("Token Poll Warning: Label for option " + (i+1) + " is blank -- removing from poll board!", cb.soom_slug, 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("Token Poll Warning: Token amount for option " + (i+1) + " is not unique -- removing from poll board!", cb.soom_slug, 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("Token Poll Warning: Label for option " + (i+1) + " is not unique -- removing from poll board!", cb.soom_slug, 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 = '---------- Token Poll has ended! ----------\n'; var response2 = '' if (opt_votes[options[0]] >= cb.settings.min_vote) { 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"; } } } else { response2 = "No option recieved enough votes, the default option of " + cb.settings.default_opt + " wins!"; } cb.sendNotice(response1 + response2, '', background, foreground, 'bold'); } function checkPollEnd() { var _poll_running = poll_running; if (mins_remain < 1) { poll_running = false; } if (_poll_running && !poll_running) { showWinner(); } } function tickTimer() { mins_remain--; checkPollEnd(); if (mins_remain > 0) { cb.setTimeout(tickTimer, 60*1000); } } cb.onMessage(function (m) { switch (m['m']) { case '!poll': // other users don't need to see this message var is_mod = (cb.room_slug == m['user'] || m['is_mod']); 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 (cb.room_slug != m['user']) { break; } poll_running = false; showWinner(); break; } }); cb.onEnter(function (user) { cb.sendNotice("Hello " + user['user'] + ", the Token Poll app is currently active in this room.\nType !poll to see voting options.", user['user'], background, foreground); }); cb.onTip(function (tip) { var amount = parseInt(tip['amount']); if (!poll_running) { return; } for (i=0; i<opt_tokens.length; i++) { if (amount == opt_tokens[i]) { cb.sendNotice(tip['from_user'] + " has voted for " + opt_labels[i], '', background, foreground); opt_votes[i]++; checkPollEnd(); break; } } }); sanitizeBoard(); showBoard(); if (txt_minutes == cb.settings.poll_mode) { cb.setTimeout(tickTimer, 60*1000); };
© Copyright Chaturbate 2011- 2024. All Rights Reserved.