Bots Home
|
Create an App
CAPTCHA
Author:
smartypants_the_2nd
Description
Source Code
Launch Bot
Current Users
Created by:
Smartypants_The_2nd
"use strict"; cb.settings_choices = [{ name: 'trustTippers', type: 'choice', label: 'Trust dark blue and light/dark purple users not to be spam bots', required: true, choice1: 'true', choice2: 'false', defaultChoice: 'true' }]; /** * The isMessageByHuman function will check message senders against a whitelist of real human people. If the sender is not whitelisted yet, the message will be replaced with a captcha challenge for the user to solve. If the isMessageByHuman function returns false, better stop processing the message, as it probably was replaced with a captcha challenge. * * Always run this bot (or the bot containing this function) in last slot ("Bot #3"). If not, other bots with lower numbers might alter the message (especially a possible challenge response from the user), making it impossible for the isMessageByHuman function to validate the user. * * @type Function * @param Object message The message object that was passed to the cb.onMessage function * @return Boolean True if and only if the message was send by a human and intended for public chat audience * * You may call the lambda function below with a boolean parameter "trustTippers" if you want the created isMessageByHuman function to auto whitelist people who tipped recently (dark blue, light or dark purple). */ const isMessageByHuman = (function(trustTippers) { function randomInteger(min, max) { return min + Math.floor(Math.random() * (max - min + 1)); } class Challenge { constructor(description, expectedResponse) { this.description = description; this.expectedResponse = String(expectedResponse).trim(); } isValidResponse(response) { return String(response).trim() == this.expectedResponse; } toString() { return this.description; } } class MathChallenge extends Challenge { static createRandomAddition() { const a = randomInteger(1, 50), b = randomInteger(1, 50), aPlusB = a + b; return new MathChallenge(`${a} + ${b} = ?`, aPlusB); } static createRandomSubtraction() { const a = randomInteger(1, 50), b = randomInteger(1, 50), aPlusB = a + b; return new MathChallenge(`${aPlusB} - ${b} = ?`, a); } constructor(description, expectedResponse) { super(description, parseInt(expectedResponse, 10)); } isValidResponse(response) { return super.isValidResponse(parseInt(response, 10)); } } const challengeFactories = [ MathChallenge.createRandomAddition, MathChallenge.createRandomSubtraction ]; function createRandomChallenge() { return challengeFactories[Math.floor(Math.random() * challengeFactories.length)].call(null); } const whitelist = new Set([ cb.room_slug ]); // add the broadcaster to whitelist right away const userChallenges = new Map(); function addToWhitelist(username) { whitelist.add(username); userChallenges.delete(username); return true; } return function(message) { if (whitelist.has(message.user)) { return true; } else if (message.in_fanclub || message.is_mod) { return addToWhitelist(message.user); } else if (trustTippers && (message.tipped_recently || message.tipped_alot_recently || message.tipped_tons_recently)) { return addToWhitelist(message.user); } else { let challenge = userChallenges.get(message.user); if (challenge && challenge.isValidResponse(message.m)) { addToWhitelist(message.user); message['X-Spam'] = true; message.m = '[CAPTCHA] Your solution is correct, you are now allowed to chat.'; return false; } else { if (!challenge) userChallenges.set(message.user, challenge = createRandomChallenge()); message['X-Spam'] = true; message.m = '[CAPTCHA] Your message has been blocked. Please prove that you are not a spam robot by solving this captcha challenge and typing the solution to public chat: ' + challenge.toString().replace(/ /g, '\u00A0'); return false; } } }; })(cb.settings.trustTippers == 'true'); cb.onMessage((message) => { if (!isMessageByHuman(message)) { // stop processing the message return message; } // otherwise do your things // and in the end, as always, return the message return message; });
© Copyright Chaturbate 2011- 2026. All Rights Reserved.