Bots Home
|
Create an App
Lily's Top Tippers
Author:
mixailo
Description
Source Code
Launch Bot
Current Users
Created by:
Mixailo
var VERSION = '1.0', COMMAND_SHOW_LEADERBOARD = '!lb', COMMAND_SEND_WINNER_MSG = '!LILYSWinMsg', COMMAND_DISABLE_PRIZE_MSG = '!LILYSPrizeOff', COMMAND_ENABLE_PRIZE_MSG = '!LILYSPrizeOn', CONFIG_COLOR_LEADER = '#9f9', INTERVAL_MULTIPLIER = 60000, UPDATE_TIME = 5000, RATE_FROM = 25, RATE_MESSAGE = ":lilyrateme", RATE_MESSAGE_DELAY = 1500, NL = '\n', user_total_tips = {}, user_last_tip_time = {}, last_top3 = '', leader_username, update_counter = 0, interval_counter = 0, silent_room = true; cb.settings_choices = [{ name: 'print_on_tip', type: 'choice', choice1: 'always', choice2: 'only if the top 3 changed', choice3: 'never', defaultValue: 'only if the top 3 changed', label: "Print top 3 after tips" }, { name: 'print_interval', type: 'int', minValue: 0, defaultValue: 10, label: "Print top 3 at least once every (minutes, 0 to disable)", required: true }, { name: 'highlight', type: 'choice', choice1: 'yes', choice2: 'no', defaultValue: 'yes', label: "Highlight tip leader in chat" }, { name: 'rate', type: 'choice', choice1: 'yes', choice2: 'no', defaultValue: 'yes', label: "Remind tippers who tipped 25 tokens to rate" }]; cb.onTip(function (tip) { handleTip(tip.amount, tip.from_user); scheduleUpdate(); silent_room = false; }); cb.onMessage(function (msg) { var message = msg['m'].split(' '); var updatedMsg = ''; //0 = invalid command, 1 = valid command var cmd = 0; if(message[0].charAt(0) == '/') { //don't print this message to chat msg['X-Spam'] = true; switch(message[0]) { case '/lb': { //user entered a proper command cmd = 1; sendLeaderboard(true, msg.user); break; } //functionality added to enable testing of the bot /* case '/simtip': { //user entered a proper command cmd = 1; simulateTip(parseInt(message[1]),message[2]); break; }*/ case '/disablehighlight': { //user entered a proper command cmd = 1; cb.settings.highlight = 'no'; break; } case '/enablehighlight': { //user entered a proper command cmd = 1; cb.settings.highlight = 'yes'; break; } } } if (message[0] == ':TopCock2' || message[0] == ':TopCock1'){ for(var i = 1; i < message.length; i++) { updatedMsg = updatedMsg + message[i]; } msg['m'] = updatedMsg; } if ((cb.settings.highlight == 'yes') && (msg.user == leader_username)) { msg['m'] = ':TopCock2 ' + msg['m']; } if (update_counter == 0){ scheduleInterval(); } if (!msg['X-Spam']){ silent_room = false; } return msg; }); function simulateTip(amount, user){ handleTip(amount, user); scheduleUpdate(); silent_room = false; } function sortLeaderboard(){ var lb = []; for (var user in user_total_tips) { if (user_total_tips.hasOwnProperty(user)){ lb.push([user_total_tips[user], -user_last_tip_time[user], user]); } } if (lb.length > 0){ lb.sort(function(a, b){ for (var i=0; i < a.length; i++){ if (a[i] < b[i]){ return 1; } if (a[i] > b[i]){ return -1; } } return 0; }); leader_username = lb[0][2]; } return lb; } function formatRanking(leaderboard, rank){ if (rank < leaderboard.length) { var p = leaderboard[rank]; return p[2] + ' (' + p[0] + ' token' + (p[0]!=1 ? 's' : '') + ')'; } else { return '--'; } } function getTop3(leaderboard){ var rank, result=''; for (rank=0; rank<3; rank++){ if (rank>0){ result += '|'; } if (rank < leaderboard.length){ result += leaderboard[rank][2]; } } return result; } function scheduleInterval(){ var interval = cb.settings.print_interval; if (interval > 0) { var counter = ++interval_counter; cb.setTimeout(function(){ if (interval_counter == counter){ sendLeaderboard(!silent_room); } }, interval*INTERVAL_MULTIPLIER); silent_room = true; } } function sendLeaderboard(force, to_user) { var lb = sortLeaderboard(), l = to_user ? lb.length : 3, out = ':lilystt '; out = '***** Lily\'s Top Tippers ***** '; if (!force) { var top3 = getTop3(lb); if (top3 == last_top3){ return; } last_top3 = top3; } //cb.sendNotice(':LILYSTopTippers2',to_user,'','#FF439F','bold'); for (var rank=0; rank<3; rank++) { out += NL + '' + (rank+1) + '. ' + formatRanking(lb, rank); //cb.sendNotice(out,to_user,'','#FF439F','bold'); } cb.sendNotice(out,to_user,'','#FF439F','bold'); if (!to_user){ scheduleInterval(); } } function handleTip(amount, user) { if (amount <= 0) return; var before = user_total_tips[user] || 0, after = user_total_tips[user] = before + amount; user_last_tip_time[user] = new Date().valueOf(); if ((before < RATE_FROM) && (after >= RATE_FROM)){ cb.setTimeout(function(){ cb.chatNotice(RATE_MESSAGE, user); }, RATE_MESSAGE_DELAY); } } function scheduleUpdate(){ var print_on_tip = cb.settings.print_on_tip; if (print_on_tip != 'never') { var counter = ++update_counter; cb.setTimeout(function(){ if (update_counter == counter){ sendLeaderboard(print_on_tip == 'always'); } }, UPDATE_TIME); } }
© Copyright Chaturbate 2011- 2024. All Rights Reserved.