Bots Home
|
Create an App
LearningToCode
Author:
25ursexyguy
Description
Source Code
Launch Bot
Current Users
Created by:
25ursexyguy
///////////////////////////////////////// FIRST POLL ///////////////////////////////////////// var foreground = '#FFFFFF'; var fg_warn = '#FF0000'; var background = '#0629AC'; var background_2 = '#AC069E'; var background_3 = '#AC062A'; 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_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 opt_labels_2 = ["vazio", "vazio"]; var opt_tokens_2 = [0, 0]; var opt_labels_3 = ["vazio", "vazio"]; var opt_tokens_3 = [0, 0]; var mins_remain = cb.settings.poll_count; var votes_remain = cb.settings.poll_count; var poll_1_running = true; var poll_2_running = false; var poll_3_running = false; // 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_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_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} ]; function showBoard(user) { // var response1 = "Here's the Token Poll board:\n"; // BOARD 1 // var response1 = '---------- Token Poll board' + ('' == user ? ' (sent to all)' : '') + ': ----------\n'; var response2 = cb.settings.poll_title; if (poll_1_running) { 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 = ''; 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, background, foreground, 'bold'); // if (undefined != response3) { cb.sendNotice(response3, user, background, foreground); // } // cb.sendNotice(response4, user); } // BOARD 2 // var response4 = '---------- Token Poll board' + ('' == user ? ' (sent to all)' : '') + ': ----------\n'; var response5 = cb.settings.poll_title; if (poll_2_running) { for (i=0; i<opt_tokens_2.length; i++) { if (0 != opt_tokens_2[i]) { response5 += "\n - " + opt_labels_2[i] + " ["+ opt_votes_2[i] + " votes]: " + opt_tokens_2[i] + " tokens"; } } var response6 = ''; switch (cb.settings.poll_mode) { case txt_minutes: response6 = mins_remain + ' minute' + (mins_remain > 1 ? 's' : '') + ' remaining to vote\n'; break; case txt_votecount: response6 = votes_remain + ' vote' + (votes_remain > 1 ? 's' : '') + ' remaining before poll closes\n'; break; case txt_wincount: response6 = 'First option to ' + cb.settings.poll_count + ' votes wins!\n'; break; } response6 += '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(response4, user); cb.sendNotice(response5, user, background_2, foreground, 'bold'); // if (undefined != response6) { cb.sendNotice(response6, user, background_2, foreground); // } // cb.sendNotice(response4, user); } // BOARD 3 // var response7 = '---------- Token Poll board' + ('' == user ? ' (sent to all)' : '') + ': ----------\n'; var response8 = cb.settings.poll_title; if (poll_3_running) { for (i=0; i<opt_tokens_3.length; i++) { if (0 != opt_tokens_3[i]) { response8 += "\n - " + opt_labels_3[i] + " ["+ opt_votes_3[i] + " votes]: " + opt_tokens_3[i] + " tokens"; } } var response9 = ''; switch (cb.settings.poll_mode) { case txt_minutes: response9 = mins_remain + ' minute' + (mins_remain > 1 ? 's' : '') + ' remaining to vote\n'; break; case txt_votecount: response9 = votes_remain + ' vote' + (votes_remain > 1 ? 's' : '') + ' remaining before poll closes\n'; break; case txt_wincount: response9 = 'First option to ' + cb.settings.poll_count + ' votes wins!\n'; break; } response9 += '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(response7, user); cb.sendNotice(response8, user, background_3, foreground, 'bold'); // if (undefined != response9) { cb.sendNotice(response9, user, background_3, 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(poll_number) { cb.sendNotice(poll_number); switch (poll_number) { case 1: 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'); break; case 2: var options = []; for (i=0; i<opt_tokens_2.length; i++) { options[i] = i; } options.sort(function(a, b){return opt_votes_2[b]-opt_votes_2[a]}); var win_count = 1; for (i=1; i<opt_tokens_2.length; i++) { if (opt_votes_2[options[i]] != opt_votes_2[options[0]]) { break; } if (0 != opt_tokens_2[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_2[options[i]] != 0) { response2 += "\n - " + opt_labels_2[options[i]] + ": " + opt_votes_2[options[i]] + " votes"; } } cb.sendNotice(response1 + response2, '', background, foreground, 'bold'); break; case 3: var options = []; for (i=0; i<opt_tokens_3.length; i++) { options[i] = i; } options.sort(function(a, b){return opt_votes_3[b]-opt_votes_3[a]}); var win_count = 1; for (i=1; i<opt_tokens_3.length; i++) { if (opt_votes_3[options[i]] != opt_votes_3[options[0]]) { break; } if (0 != opt_tokens_3[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_3[options[i]] != 0) { response2 += "\n - " + opt_labels_3[options[i]] + ": " + opt_votes_3[options[i]] + " votes"; } } cb.sendNotice(response1 + response2, '', background, foreground, 'bold'); break; } } function checkPollEnd() { var _poll_1_running = poll_1_running; switch (cb.settings.poll_mode) { case txt_never: return; case txt_minutes: if (mins_remain < 1) { poll_1_running = false; } break; case txt_votecount: if (votes_remain < 1) { poll_1_running = false; } break; case txt_wincount: for (i=0; i<opt_tokens.length; i++) { if (opt_votes[i] >= cb.settings.poll_count) { poll_1_running = false; } } break; } if (_poll_1_running && !poll_1_running) { showWinner(); } } function tickTimer() { mins_remain--; checkPollEnd(); if (mins_remain > 0) { cb.setTimeout(tickTimer, 60*1000); } } cb.onMessage(function (m) { var m_message = m['m']; var m_test_1 = m_message.includes('!newpoll1'); var m_test_2 = m_message.includes('!newpoll2'); var m_test_3 = m_message.includes('!newpoll3'); var is_mod = (cb.room_slug == m['user'] || m['is_mod']); m['X-Spam'] = is_mod; if((m_test_1 || m_test_2 || m_test_3) && is_mod){ var poll_selector = m_message.substring(1, 9); idx_in = m_message.indexOf("{"); idx_out = m_message.indexOf(";"); var t_poll = m_message.substring(idx_in + 1, idx_out); idx_in = m_message.indexOf(";"); idx_out = m_message.indexOf(";",idx_in + 1); var opt_poll_1 = m_message.substring(idx_in + 1, idx_out); idx_in = m_message.indexOf(";",idx_in + 1); idx_out = m_message.indexOf(";",idx_in + 1); var opt_val_1 = m_message.substring(idx_in + 1, idx_out); idx_in = m_message.indexOf(";",idx_in + 1); idx_out = m_message.indexOf(";",idx_in + 1); var opt_poll_2 = m_message.substring(idx_in + 1, idx_out); idx_in = m_message.indexOf(";",idx_in + 1); idx_out = m_message.indexOf("}"); var opt_val_2 = m_message.substring(idx_in + 1, idx_out); newPoll(poll_selector,t_poll,opt_poll_1,opt_val_1,opt_poll_2,opt_val_2); } else { } 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_1_running || poll_2_running || poll_3_running) { showBoard(''); } else { showWinner(1); showWinner(2); showWinner(3); } } else { if (poll_1_running || poll_2_running || poll_3_running) { showBoard(m['user']); } else { showWinner(1); showWinner(2); showWinner(3); } } break; case '!endpoll1': if (cb.room_slug != m['user']) { break; } poll_1_running = false; showWinner(1); break; case '!endpoll2': if (cb.room_slug != m['user']) { break; } poll_2_running = false; showWinner(2); break; case '!endpoll3': if (cb.room_slug != m['user']) { break; } poll_3_running = false; showWinner(3); 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_1_running && !poll_1_running && !poll_1_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--; } } if (amount == opt_tokens_2[i]) { cb.sendNotice(tip['from_user'] + " has voted for " + opt_labels_2[i], '', background_2, foreground); opt_votes_2[i]++; if (txt_votecount == cb.settings.poll_mode) { votes_remain--; } } if (amount == opt_tokens_3[i]) { cb.sendNotice(tip['from_user'] + " has voted for " + opt_labels_3[i], '', background_3, foreground); opt_votes_3[i]++; if (txt_votecount == cb.settings.poll_mode) { votes_remain--; } } checkPollEnd(); break; } }); sanitizeBoard(); showBoard(1); showBoard(2); showBoard(3); if (txt_minutes == cb.settings.poll_mode) { cb.setTimeout(tickTimer, 60*1000); } ///////////////////////////////////////// SECOND POLL ///////////////////////////////////////// function newPoll(poll_selector,t_poll,opt_poll_1,opt_val_1,opt_poll_2,opt_val_2) { switch (poll_selector) { case 'newpoll1': showWinner(1); opt_votes = [0, 0, 0, 0, 0]; cb.settings.poll_title = t_poll; opt_labels = [opt_poll_1, opt_poll_2, cb.settings.opt3_label, cb.settings.opt4_label, cb.settings.opt5_label]; opt_tokens = [opt_val_1, opt_val_2, cb.settings.opt3_tokens, cb.settings.opt4_tokens, cb.settings.opt5_tokens]; poll_1_running = true; break; case 'newpoll2': if(poll_2_running = true){ showWinner(2); } else { opt_votes = [0, 0, 0, 0, 0]; cb.settings.poll_title_2 = t_poll; opt_labels_2 = [opt_poll_1, opt_poll_2, cb.settings.opt3_label, cb.settings.opt4_label, cb.settings.opt5_label]; opt_tokens_2 = [opt_val_1, opt_val_2, cb.settings.opt3_tokens, cb.settings.opt4_tokens, cb.settings.opt5_tokens]; poll_2_running = true; } break; case 'newpoll3': if(poll_3_running = true){ showWinner(3); } else { opt_votes = [0, 0, 0, 0, 0]; cb.settings.poll_title_3 = t_poll; opt_labels_3 = [opt_poll_1, opt_poll_2, cb.settings.opt3_label, cb.settings.opt4_label, cb.settings.opt5_label]; opt_tokens_3 = [opt_val_1, opt_val_2, cb.settings.opt3_tokens, cb.settings.opt4_tokens, cb.settings.opt5_tokens]; poll_3_running = true; } break; } return; }
© Copyright Chaturbate 2011- 2024. All Rights Reserved.