Bots Home
|
Create an App
MoniBot 3000
Author:
podule
Description
Source Code
Launch Bot
Current Users
Created by:
Podule
// MoniBot 3000 - version 0.1 // By podule // // A Chaturbate bot to allow hidden shows to be triggered when certain // conditions are met (e.g., a tip goal is reached). // // This code is public domain. // Set up bot options and defaults cb.settings_choices = [ {name: "show_length_secs", type: "int", minValue: 3, default: 2*60}, {name: "show_start_message", type: "str", minLength: 1, maxLength: 255, default: "Special secret hidden show is starting now!"}, {name: "show_stop_message", type: "str", minLength: 1, maxLength: 255, default: "Special secret hidden show has finished!"}, {name: "ready_message_interval_secs", type: "int", minValue: 10, maxValue: 420, default: 42}, {name: "show_trigger_amount", type: "int", minValue: 3, default: 202}, {name: "viewer_tip_min", type: "int", minValue: 1, default: 25}, {name: "tip_goal", type: "int", minValue: 3, default: 2002}, {name: "command_prefix", type: "str", minLength: 2, maxLength: 22, default: "/mb3k_"} ]; // Define singleton object var mb3k = { // Values _viewer_tip_total: {}, // Tippers seen this round (k: name, v: tokens tipped) _total_tips: 0, // Total tokens tipped this round _timeout_id: -1, // ID to track timeouts sent to cb.setTimeout(...) // Call a function for each tipper seen, passing name as only argument foreach_tipper: function (fn) { for (var name in this._viewer_tip_total) { fn(name); } }, // Returns total number of distinct tippers seen since initialization, // or last reset tipper_count: function () { return Object.keys(this._viewer_tip_total).length; }, // Returns total number of (seen) tokens tipped, by username tipper_tipped_total: function (name) { return this._viewer_tip_total[name] || 0; }, // Update tips-seen statistics update_tipper_tipped_total: function (name, amount) { this._viewer_tip_total[name] = this.tipper_tipped_total(name) + amount; this._total_tips += amount; }, // Reset tips-seen statistics, for new round clear_tipper_tipped_totals: function () { this._viewer_tip_total = {}; this._total_tips = 0; }, // Returns total number of tokens tipped so far, in current round total_tips: function () { return this._total_tips; }, // Returns timeout ID of current hidden show timeout_id: function () { return this._timeout_id; }, // Set and unset timeout ID set_timeout_id: function (id) { this._timeout_id = id; }, unset_timeout_id: function () { this._timeout_id = -1; }, // Returns the lowercased version of the command prefix. Replaces // itself with a new function returning the lowercased version // (effectively a constant) command_prefix: function () { var prefix = cb.settings.command_prefix.toLowerCase(); this.command_prefix = function () { return prefix; }; return this.command_prefix(); }, // More descriptively named wrapper function around cb.room_slug broadcaster_name: function () { return cb.room_slug; }, // Wrapper functions around notice-sending send_public_notice: function (text) { cb.sendNotice("*PUBLIC NOTICE* " + text, this.broadcaster_name()); // XXX }, send_private_notice: function (text, name) { cb.sendNotice(text, name); }, send_broadcaster_notice: function (text) { this.send_private_notice(text, this.broadcaster_name()); }, // Returns true iff hidden show is running show_is_running: function () { return cb.limitCam_isRunning(); }, // Report basic statistics/state to the broadcaster cmd_report_stats: function () { var text = "I've seen tips from " + this.tipper_count() + " viewers, totalling " + this.total_tips() + " tokens [timeout id: " + this.timeout_id() + "]"; this.send_broadcaster_notice(text); }, // Reset bot for new round cmd_reset_totals: function () { this.clear_tipper_tipped_totals(); }, // Stop the hidden show, if it is running, and reset bot cmd_stop_hidden_show: function () { if (this.show_is_running()) { cb.limitCam_stop(); this.unset_timeout_id(); this.cmd_reset_totals(); } }, // Returns true iff user "name" has tipped enough to be viewer of // hidden show has_tipped_enough: function (name) { return this.tipper_tipped_total(name) >= cb.settings.viewer_tip_min; }, // Returns array of usernames allowed to view hidden show allowed_viewers: function () { var allowed = []; this.foreach_tipper(function (name) { if (mb3k.has_tipped_enough(name)) { allowed.push(name); } }); return allowed; }, // Starts hidden show, if not already running, and sets up future call // to stop it after a number of seconds cmd_start_hidden_show: function () { if (!this.show_is_running()) { cb.limitCam_start(cb.settings.show_start_message, this.allowed_viewers()); var id = Math.random(); this.set_timeout_id(id); cb.setTimeout(function () { if (this.timeout_id() === id) { this.cmd_stop_hidden_show(); } }, cb.settings.show_length_secs*1000); } }, // Sends notice to broadcaster that command was unrecognized unknown_command: function (message) { this.send_broadcaster_notice( "I didn't understand your command \"" + message + "\"" ); }, // Sends notice to chat, no more often than x seconds. It does this by-- // once notice has been sent--replacing itself with function do_nothing() // (below), which does nothing. After timeout the original function is // swapped back in to the singleton object print_occasionally: function (text) { if (!this.show_is_running()) { this.send_public_notice(text); var saved_fn = this.print_occasionally; this.print_occasionally = this.do_nothing; cb.setTimeout(function () { mb3k.print_occasionally = saved_fn; }, cb.settings.ready_message_interval_secs*1000); } }, do_nothing: function () {}, // Returns true iff tip goal has been reached goal_is_reached: function () { return this.total_tips() >= cb.settings.tip_goal; }, // Returns true iff tip amount was "magic number" for triggering show show_is_triggered: function (amount) { return amount === cb.settings.show_trigger_amount; }, // Called when tip is made while broadcasting publicly; sends appropriate // message to chat (i.e., that show is ready to begin) on_public_tip: function (from, amount) { var text = this.broadcaster_name() + "'s hidden show could start any moment, thanks to " + from; if (this.goal_is_reached()) { if (this.tipper_count() > 1) { text += " and other tippers"; } this.print_occasionally(text + "!"); } else if (this.show_is_triggered(amount)) { this.print_occasionally(text + "!"); } }, // Called when tip is made while in hidden show; if tipper has tipped // enough, user is added to "allowed viewers" list on_hidden_tip: function (from, amount) { // "amount" unused if (this.has_tipped_enough(from)) { cb.limitCam_addUsers([from]); } }, // Returns true iff string "haystack" starts with "needle" starts_with: function (haystack, needle) { return haystack.substr(0, needle.length) === needle; }, // Returns true iff text was a) sent by broadcaster, and b) begins with // configured command prefix string is_broadcaster_command: function (from, text, prefix) { return from === this.broadcaster_name() && this.starts_with(text, prefix); }, // Mark chat-room message as "spam" so it's hidden from chat mark_message_as_hidden: function (msg) { msg["X-Spam"] = true; }, // Returns command portion of bot command, iff the command is validly // formed, and was sent by the broadcaster. Commands are of the form // <prefix><action>, e.g. (by default) /mb3k_start parse_broadcaster_command: function (msg) { var text = msg["m"].toLowerCase(), prefix = this.command_prefix(); if (this.is_broadcaster_command(msg["user"], text, prefix)) { this.mark_message_as_hidden(msg); return text.substr(prefix.length); }; return undefined; } }; // Install on-tip handler, which updates tips-seen statistics and dispatches // to mb3k.on_(hidden|public)_tip(...), depending on whether hidden show is // running at the time tip is made cb.onTip(function (tip) { var from = tip["from_user"], amount = parseInt(tip["amount"]); mb3k.update_tipper_tipped_total(from, amount); if (mb3k.show_is_running()) { mb3k.on_hidden_tip(from, amount); } else { mb3k.on_public_tip(from, amount); } }); // Install on-message handler, which attempts to parse message text as // mb3k command and, if recognized, dispatches appropriately cb.onMessage(function (msg) { var cmd = mb3k.parse_broadcaster_command(msg); switch (cmd) { case undefined: // not a broadcaster command: do nothing break; case "start": mb3k.cmd_start_hidden_show(); break; case "stop": mb3k.cmd_stop_hidden_show(); break; case "report": mb3k.cmd_report_stats(); break; case "reset": mb3k.cmd_reset_totals(); break; default: mb3k.unknown_command(msg["m"]); } }); // vim: set sts=4 sw=4 et ft=javascript:
© Copyright Chaturbate 2011- 2026. All Rights Reserved.