Apps Home
|
Create an App
ircapp_x2
Author:
ardo_account
Description
Source Code
Launch App
Current Users
Created by:
Ardo_Account
/* BEGIN setup of the app. This would probably be moved into a different script which could be imported into a different app to make it easier to develop complex features Defines - chat.onMessage(...) // define a callback for each message - chat.onCommand("cmd") // accessible by "/cmd" - chat.settings // not sure if this is fully implemented - chat.notice("...") // blasts a message to the room - MsgContext // passed as an argument to each onMessage callback // allows you to conveniently check permissions, // reject unwanted messages, etc. */ var MsgContext = (function(){ function MsgContext(msg) { this._msg = msg; this.origin = msg.user; this.text = msg.m || ''; } MsgContext.prototype.preventDefault = function() { this._msg['X-Spam'] = true; return this; }; MsgContext.prototype.words = function() { return this.text.split(/\s+/g); } MsgContext.prototype.reject = function(rejectMsg) { //todo: handle rejectMsg this.preventDefault(); return this; } MsgContext.prototype.isOwner = function(){ return this.origin === cb.room_slug; } MsgContext.prototype.isMod = function() { return this.isOwner() || this._msg.is_mod; } MsgContext.prototype.finish = function() { return this._msg; } return MsgContext; })(); var chat = (function(){ var root = this, onMessageCallbacks = [], _settingsLoaded = false; cb.onMessage(function(msg) { _settingsLoaded || ensureSettingsLoaded(); try { var context = new MsgContext(msg); for(var i=0;i<onMessageCallbacks.length;i++) { onMessageCallbacks[i].call(root, context); } return context.finish(); } catch(e) { cb.log("Error: "+e.message); } }); function ensureSettingsLoaded() { if(!_settingsLoaded) { var key, setts = cb.settings || {}; for(key in setts) { if(setts.hasOwnProperty(key)) { settings.set(key, setts[key]); } } cb.settings = new Error("You should access cb.settings with 'chat.settings.get(property)'."); } } var settings = (function(){ var s = {}, onChangeCallbacks = []; cb.log(JSON.stringify(cb.settings_choices)); cb.log(cb.settings_choices); var _oldCbChoices = ([]).slice(0); cb.settings_choices = []; return { set: function(key, val){ s[key] = val; settings.triggerChange(key); }, get: function(key){ return s[key]; }, expect: function(_opts){ var opts, scope, values, i, setting; opts = _opts || {}; scope = opts.scope || false; values = opts.values || []; for(i=0;i<values.length;i++) { cb.settings_choices.push(values[i]); } }, onChange: function(callback) { onChangeCallbacks.push(callback); }, triggerChange: function() { for(var i=0;i<onChangeCallbacks.length;i++) { onChangeCallbacks[i] && onChangeCallbacks[i].call(root, s[key], key, settings); } } }; })(); function notice(){ cb.chatNotice.apply(root, arguments); } var nn = 0; function table(panels=[]) { cb.onDrawPanel(function(){ { return { 'template': '3_rows_of_labels', 'row1_label': 'Tip Received / Goal :', 'row1_value': '0', 'row2_label': 'Highest Tip:', 'row2_value': nn++, 'row3_label': 'Latest Tip Received:', 'row3_value': '0' }; } }); cb.drawPanel() /* usage: chat.drawPanel([ [col0, col1], [r1c0, r1c1], [r2c0, r2c1], ], options) */ } function onTip () { } return { onMessage: function chat__OnMessage(callback){ onMessageCallbacks.push(callback); }, settings: settings, notice: notice, table: table, // onCommand is set below with Command }; })(); (function(){ // default topic is "user"'s room. (as in vanilla room) var topic = ""+cb.room_slug+"'s room."; var getTopic = function(){ return topic; }; var setTopic = function(nextTopic) { if(nextTopic) { topic = nextTopic; cb.changeRoomSubject(topic); } else { return false; } }; chat.getTopic = getTopic; chat.setTopic = setTopic; })(); (function(){ var cmds = {}, root = this; var Command = (function(){ function Command(whichCmd, opts) { this.id = whichCmd; this.opts = opts; this.callback = opts.callback; this.description = this.id + ": " + (opts.description || "No description provided") } Command.prototype.callFn = function(arg, ctx){ if(typeof this.callback === "function") { this.callback.call(root, arg, ctx); } else { throw new Error("Could not call command: "+ this.id); } } Command.prototype.helpMessages = function(){ } return Command; })(); chat.onCommand = function(whichCmd, opts){ if(typeof opts === "function") opts = {callback: opts} if(opts.callback && "function" === typeof opts.callback) { /* temporarily block duplicate actions on the same command */ // cmds[whichCmd] || (cmds[whichCmd] = []); // cmds[whichCmd].push(new Command(whichCmd, opts)); cmds[whichCmd] = [new Command(whichCmd, opts)]; } }; chat.callCommand = function(whichCmd, arg, ctx) { var i, cl, clist = cmds[whichCmd]; if (!clist) { throw new Error("Command not found: " + whichCmd); } for(i=0,cl=clist.length;i<cl;i++) { clist[i].callFn(arg, ctx); } }; chat.onCommand("help", { usage: "/help [command*]", description: "Displays a list of commands or help for an individual command", callback: function(arg, ctx){ var cmdId, cmd; for(var cmdId in cmds) { cmd = cmds[cmdId]; cb.log(cmd.description); } } }); chat.onMessage(function(context){ var cmsg = context.text.match(/^\/(\w+)(\s+.*)?$/); if(cmsg) { (function(command, _arg){ var arg; if(_arg && _arg.trim) { arg = _arg.trim(); } try { chat.callCommand(command, arg, context); } catch(e) { context.reject("Command failed: "+e.message); } })(cmsg[1], cmsg[2]); } }); })(); // END SETUP // BEGIN defining the actual app. // All features are documented like this-- /* feature(s): - "/away brb pee" // sets a temporary room subject - "/back" // restores the room subject */ (function(){ var oldTopic = false; chat.onCommand("away", function(str, ctx){ var awayMessage = (!!str) ? ("away: " + str) : "away" if (ctx.isMod()) { if (!oldTopic) { oldTopic = chat.getTopic(); } chat.notice(awayMessage); chat.setTopic(awayMessage); } else { chat.notice("" + ctx.origin + " is " + awayMessage); } ctx.preventDefault(); }); chat.onCommand("back", function(str, ctx){ if(ctx.isMod()) { chat.notice("back"); if (oldTopic) { chat.setTopic(oldTopic); oldTopic = false; } } else { chat.notice("" + ctx.origin + " is back"); } ctx.preventDefault(); }); })(); /* feature(s): - "/topic best room ever" sets the room topic */ chat.onCommand("topic", { usage: "/topic [new room topic]", description: "Sets the room topic", callback: function(nextTopic, ctx){ if(ctx.isMod()) { chat.setTopic(nextTopic); ctx.reject(); } } }); /* feature(s): - "/me likes this room" writes a note to the room: *username likes this room* */ chat.onCommand("me", { usage: "/me [message to broadcast]", description: "Broadcasts a message to the room", callback: function(status, ctx){ if(status !== undefined) { chat.notice(["*", ctx.origin, status].join(" ")); ctx.preventDefault(); } else { ctx.preventDefault(); } } }); /* feature(s): - "/msg username pm" anyone can send a private message that gets printed for the recipient in the main chat. "/r quick response" replies to the most recent message someone sent you (use carefully) */ //notify sender of sent message. var latestMessageSenders = {}; chat.onCommand("msg", { usage: "/msg [username] [message]", description: "A new way to direct message", callback: function(userAndMsg, ctx){ var blocks = userAndMsg.split(" "); var username = blocks[0], message = blocks.splice(1).join(" "); if(username && message) { chat.notice("Direct message from "+ctx.origin+" to "+username, username); chat.notice("[ "+ message + " ]", username); chat.notice("Direct message from "+ctx.origin+" to "+username, ctx.origin); chat.notice("[ "+ message + " ]", ctx.origin); latestMessageSenders[username] = ctx.origin; } ctx.preventDefault(); } }); chat.onCommand("r", { callback: function(message, ctx){ var username = latestMessageSenders[ctx.origin] if(username) { chat.notice("Direct message from "+ctx.origin+" to "+username, username); chat.notice("[ "+ message + " ]", username); chat.notice("Direct message from "+ctx.origin+" to "+username, ctx.origin); chat.notice("[ "+ message + " ]", ctx.origin); } ctx.preventDefault(); } }); /* feature(s): - "/eval 'arbitraryCode()'" allows mods to execute arbitrary code for debugging / changing settings(?). This is powerful sh*t, and should be used with caution. */ chat.onCommand("eval", function(str, ctx){ var resp; if(!ctx.isMod()) { return ctx.reject("You must be a moderator to run this command"); } if(str.match(/^(\'|\")(.*)(\'|\")$/)) { str = str.match(/^(\'|\")(.*)(\'|\")$/)[2]; } try { resp = eval(str); } catch(e) { resp = "Error: "+e.message; } chat.notice("Evaluated response: " + resp, ctx.origin); }); /* feature(s): - "/silence username" adds a user to a silenced list - "/unsilence username" removes them from that list */ (function silencer(){ var silenced = {}; chat.onCommand("silence", { callback: function(username, ctx){ if (ctx.isMod()) { silenced[username] = true; ctx.preventDefault(); } else { ctx.reject("You cannot silence someone unless you are a mod."); } } }); chat.onCommand("unsilence", { callback: function(username, ctx){ if (ctx.isMod()) { delete silenced[username]; } } }); chat.onMessage(function(context){ if (silenced[context.origin]) { context.reject("You have been silenced"); } }); })(); chat.onCommand('table', { callback: function(context){ cb.log('test'); chat.table([ ['r1', 'r2'], ['3','4'], ['5', '6'] ], { every: 60, }); }, }); /* features: - "/link http://example.com" gets around the strict rules that filter URLs and keywords */ chat.onCommand("link", function(str, ctx){ const { origin } = ctx; chat.notice(`/link command has been called ${origin} sends link to ${str}`); }); /* feature(s): FINALLY something kinky-- - "/cum in 10" sets a timer for 10 minutes and displays a message when the timer is done */ var cumTargetSet = false; chat.onCommand("cum", function(str, ctx){ if(ctx.isOwner() && !cumTargetSet) { var mtch, t, atOrIn, mSecsTillZero, genMessage; if(mtch = str.match(/^(in) (\d)(\s.*)?$/)) { mSecsTillZero = 1000 * (60 - (new Date().getUTCSeconds())); atOrIn = mtch[1]; t = +mtch[2]; genMessage = function(tt){ return ctx.origin + "'s cum countdown: "+ tt +" minute" + (atOrIn === 1 ? "" : "s") + " remaining"; } chat.notice(ctx.origin + "'s cum target set:"); chat.notice("Countdown: " + t +" minute" + (atOrIn === 1 ? "" : "s")) for(var i=0;i<=t;i++) { (function(mins, outOfMins){ setTimeout(function(){ var remaining = outOfMins - mins; if(remaining===0) { chat.notice(":fireworks!!!1!"); cumTargetSet = false; } else { chat.notice(genMessage(remaining)); } }, (60 * 1000 * mins) + mSecsTillZero); })(i, t); } } ctx.reject(); } });
© Copyright Chaturbate 2011- 2025. All Rights Reserved.