Bots Home
|
Create an App
Simple Token Poll
Author:
hl_broadcaster
Description
Source Code
Launch Bot
Current Users
Created by:
Hl_Broadcaster
var PINK = '#ffc0cb'; var BLACK = '#000000'; var RED = '#ff0000'; var FONT_WEIGHT = 'bold'; var current_poll = null; var votes = {}; var set_current_poll = function(text) { reset_poll(); var values = text .replace('/pollnew', '') .split('|') .map(function(val) { return val.trim() }) .filter(function(val) { return !!val }); var choices = values.slice(1) current_poll = { label: values[0], options: choices.map(function(val) { return {label: val} }), }; choices.forEach(function(val) { votes[val] = 0; }) } var reset_poll = function() { current_poll = null; votes = {}; } var show_poll = function() { if (!current_poll) { cb.log('no poll defined'); return; } var percentages = {}; var total = 0; Object.keys(votes).forEach(function(v) { total += votes[v]; }) Object.keys(votes).forEach(function(v) { var percentage = total != 0 ? ((votes[v] / total) * 100) : 0; percentages[v] = percentage.toFixed(2); }) var message = "Token Poll: " + current_poll.label + "\n"; Object.keys(percentages).forEach(function(v) { message += "--> " + v + ": " + percentages[v] + "%\n"; }) message += "Vote by selecting an option when tipping (one token = one vote)"; cb.sendNotice(message, '', PINK, BLACK, FONT_WEIGHT) } cb.tipOptions(function(user) { return current_poll; }); cb.onTip(function (tip) { if (!current_poll) { return; } var amount = parseInt(tip['amount']) var choice = tip['message']; if (choice in votes) { votes[choice] += amount; } }); cb.onMessage(function (msg) { var user = msg['user']; var text = msg['m']; var roomHost = cb.room_slug; if (user == roomHost) { if (text.startsWith('/pollnew')) { cb.log('defining new poll'); set_current_poll(text); show_poll(); msg['X-Spam'] = true; } else if (text.startsWith('/pollend')) { show_poll(); cb.sendNotice("Poll has ended", '', RED, BLACK, FONT_WEIGHT) reset_poll(); msg['X-Spam'] = true; } else if (text.startsWith('/pollshow')) { show_poll(); msg['X-Spam'] = true; } } return msg; });
© Copyright Chaturbate 2011- 2025. All Rights Reserved.