Bots Home
|
Create an App
perl
Author:
norasins
Description
Source Code
Launch Bot
Current Users
Created by:
Norasins
var foreground = '#070042'; var fg_warn = '#9F0000'; var background = '#FFBE31'; var txt_never = 'When I/mod end(s) it with !endpoll command'; var txt_minutes = 'Ends after xx minutes'; var txt_votecount= 'Ends after xx votes'; var txt_wincount = 'When one option reaches xx votes'; 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 votes_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 (set minutes/vote qty in next entry box) ...', choice1: txt_never, choice2: txt_minutes, choice3: txt_votecount, choice4: txt_wincount, defaultValue: txt_never}, {name: 'poll_count', type: 'int', minValue: 1, default: 15, label: '... Where xx qty is (ignore this if "when I end it") ...'}, {name: 'opt1_label', type: 'str', minLength: 1, default: 'Facial', maxLength: 255, label: 'Poll Option 1'}, {name: 'opt1_tokens', type: 'int', minValue: 1, default: 9, label: 'Option 1 tokens'}, {name: 'opt2_label', type: 'str', minLength: 1, maxLength: 255, label: 'Poll Option 2'}, {name: 'opt2_tokens', type: 'int', minValue: 1, default: 8, label: 'Option 2 tokens'}, {name: 'opt3_label', type: 'str', minLength: 1, maxLength: 255, label: 'Poll 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: 'Poll 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: 'Poll Option 5', 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 = '---------- Token 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 amount for your choice listed above to register your vote. Vote as often as you wish! 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, 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(":alert1 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(":alert1 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(":alert1 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 = '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, '', background, 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; } }); cb.onEnter(function (user) { cb.sendNotice("Hello " + user['user'] + ", the Token Poll 'Updated' bot is currently active!! \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]++; if (txt_votecount == cb.settings.poll_mode) { votes_remain--; } checkPollEnd(); break; } } }); sanitizeBoard(); showBoard(); if (txt_minutes == cb.settings.poll_mode) { cb.setTimeout(tickTimer, 60*1000); }
© Copyright Chaturbate 2011- 2024. All Rights Reserved.