Bots Home
|
Create an App
F-Tipper
Author:
eronova_bot
Description
Source Code
Launch Bot
Current Users
Created by:
Eronova_Bot
/********************************************* Tip Announce --------------- Notifies the room when a specific tipper tips Version history: 3 Oct 28, 2022: v1.0; initial release *********************************************/ var IAMBOT = true, COMMAND_SHOW_LEADERBOARD = '!lb', CONFIG_COLOR_LEADER = '#9f9', NL = '\n', user_total_tips = {}, user_last_tip_time = {}, leader_username, draw_leaderboard_timer; // No settings at all :) cb.settings_choices = []; //handlers cb.onTip(function (tip) { handleTip(tip.amount, tip.from_user); IAMBOT ? scheduleDrawLeaderboard(5000) : cb.drawPanel(); }); if (!IAMBOT) { cb.onDrawPanel(function (user) { var lb = sortLeaderboard(); return { 'template': '3_rows_of_labels', 'row1_label': 'Tip leader:', 'row1_value': formatRanking(lb, 0), 'row2_label': 'Runner up:', 'row2_value': formatRanking(lb, 1), 'row3_label': 'Third place:', 'row3_value': formatRanking(lb, 2) }; }); } cb.onMessage(function (msg) { // user commands if ((msg.m.indexOf(COMMAND_SHOW_LEADERBOARD) == 0) && !msg['X-Spam']) { drawLeaderboard(true, msg.user); msg['X-Spam'] = true; } if (!IAMBOT && (msg.user == leader_username)) { msg.background = CONFIG_COLOR_LEADER; } return msg; }); function handleTip(amount, user) { user_total_tips[user] = (user_total_tips[user] || 0) + amount; user_last_tip_time[user] = new Date().valueOf(); } function sortLeaderboard(){ var lb = new Array(); for (user in user_total_tips) { lb.push([user_total_tips[user], -user_last_tip_time[user], user]); } if (lb.length > 0){ lb.sort(function(a, b){ var r,i; for (i=0; i < a.length; i++){ if (a[i] < b[i]){ return 1; } if (a[i] > b[i]){ return -1; } } }); 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 drawLeaderboard(full, to_user) { var rank, lb = sortLeaderboard(), l = full ? lb.length : 3, out = 'Tippers leaderboard'; draw_leaderboard_timer = false; if (l<3) { l=3; }; if (l < lb.length) { out += ' top 3 (Type !lb to see the full leaderboard)'; } for (rank=0; rank<l; rank+=1) { out += NL + '' + (rank+1) + '. ' + formatRanking(lb, rank); } cb.chatNotice(out, to_user); } function drawLeaderboardLine() { var rank, lb = sortLeaderboard(), out = 'Tippers leaderboard '; draw_leaderboard_timer = false; out += ' 1: ' + formatRanking(lb, 1); out += ', 2: ' + formatRanking(lb, 1); out += ', 3: ' + formatRanking(lb, 1); cb.chatNotice(out, to_user); } function clearDrawTimeout(){ if (draw_leaderboard_timer){ clearTimeout(draw_leaderboard_timer); draw_leaderboard_timer = false; } } function scheduleDrawLeaderboard(timeout){ if (!draw_leaderboard_timer){ draw_leaderboard_timer = setTimeout(drawLeaderboardLine, timeout); } } function init(){ if (!IAMBOT) { cb.drawPanel(); } } init();
© Copyright Chaturbate 2011- 2024. All Rights Reserved.