Character Vault
Any Concept / Any System
Compendium
Your System Come To Life
Roll20 for Android
Streamlined for your Tablet
Roll20 for iPad
Streamlined for your Tablet

Personal tools

API:Short Community Scripts

From Roll20 Wiki

Jump to: navigation, search

Main Page: API:Script Index

This is a collection of shorter API scripts and snippets created the community, which haven't ended up in the one-click menu for for being so situational or small.


Contents


Chat

Blind Roll (TheAaron)


Example:

!blindroll [[@{selected|skill}+1]]d20+[[ [[@{selected|attack]]+@{target|ac} ]] Weird attack..

Results in player seeing:

Blind roll sent to GM
3d20+8 Weird attack..
Blind Roll API code
/*******************************************************************************
* rollers.js - Provides specialized dice rolling alternatives.
*
* Dependencies: none
*******************************************************************************
*
* Commands for performing specialty dice rolling.
*
* !blindroll roll
* Players may issue this command to have a roll of the dice generated in
* secret to the GM only. This is useful when the player should not know
* the result of the roll (e.g., a Stealth check).
*
* Example:
* !blindroll 1d20+7 Stealth check
* This example would be rolled as a GM roll so that the results are only
* visible to the GM.
*
* !rigroll <num dice>d<num sides>:<modifier>:<actual rolls (comma-separated)>
* Occasionally, a GM may want to exert control over fate and have the dice
* fall a certain way (for cinematic reasons or to avoid a TPK). This command
* allows a GM to roll "in the open" and still get the desired outcome.
* This command cannot currently deal with complex roll types that are
* available generally in Roll20.
* Whenever this command is issued, the GM also receives a whisper informing
* him of the rigged roll. This is favored over simply denying players the
* ability to rig rolls because there may be times when the GM and a player
* conspire together for story purposes and require a particular outcome.
*
* Examples:
* !rigroll 1d20:+3:18
* !rigroll 5d4:+8:2,3,1,3,4
* The first example would produce a result of 21 (showing that an 18 was
* rolled). The second example would also produce a result of 21 (showing
* that a 2, 3, 1, 3, and 4 were rolled.
*
*******************************************************************************
* Copyright (C) 2014 Aaron Garrett (aaron.lee.garrett@gmail.com)
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*******************************************************************************/
if(!('contains' in String.prototype)) {
    String.prototype.contains = function(str, startIndex) {
        return ''.indexOf.call(this, str, startIndex) !== -1;
    };
}
var inspired = inspired || {};
on("chat:message", function(msg_orig) {
    var msg=_.clone(msg_orig);
    if(msg.type != "api") return;


    if(_.has(msg,'inlinerolls')){
        msg.content = _.chain(msg.inlinerolls)
        .reduce(function(m,v,k){
            m['$[['+k+']]']=v.results.total || 0;
            return m;
        },{})
        .reduce(function(m,v,k){
            return m.replace(k,v);
        },msg.content)
        .value();
    }


    if(msg.content.contains("!blindroll ")) {
        var roll = msg.content.replace("!blindroll ", "").trim();
        if(roll.length > 0) {
            sendChat(msg.who, "Blind roll sent to GM <br/><small>(" + roll + ")</small>.");
            sendChat(msg.who, "/gmroll " + roll + " from " + msg.who);
        }
    }
});
on("chat:message", function(msg) {
    if(msg.type != "api") return;
    if(msg.content.contains("!rigroll ")) {
        var parts = msg.content.replace("!rigroll ", "").split(":");
        var roll = parts[0];
        var temp = roll.split("d");
        var numdice = temp[0];
        var faces = temp[1];
        var modifier = parts[1];
        var values = parts[2].split(",");
        var formulaStyle = "font-size:inherit;display:inline;padding:4px;background:white;border-radius:3px;";
        var totalStyle = formulaStyle;
        totalStyle += "border:1px solid #d1d1d1;cursor:move;font-size:1.4em;font-weight:bold;color:black;line-height:2.0em;";
        formulaStyle += "border:1px solid #d1d1d1;font-size:1.1em;line-height:2.0em;word-wrap:break-word;";
        var clearStyle = "clear:both";
        var formattedFormulaStyle = "display:block;float:left;padding:0 4px 0 4px;margin:5px 0 5px 0";
        var dicegroupingStyle = "display:inline";
        var uisortableStyle = "cursor:move";
        var rolledStyle = "cursor:move;font-weight:bold;color:black;font-size:1.4em";
        var uidraggableStyle = "cursor:move";
        var html = "<div style=\"" + formulaStyle + "\"> rolling " + numdice + "d" + faces + modifier + " </div>";
        html += "<div style=\"" + clearStyle + "\"></div>";
        html += "<div style=\"" + formulaStyle + ";" + formattedFormulaStyle + "\">";
        html += " <div style=\"" + dicegroupingStyle + ";" + uisortableStyle + "\" data-groupindex=\"0\">";
        var total = 0;
        html += " (";
            for(var i = 0; i < numdice; i++) {
                var value = values[i];
                var color="black";
                if (value == "1") {
                    color="#730505";
                }
                else if (value == faces) {
                    color="#247305";
                }
                var didrollStyle = "text-shadow:-1px -1px 1px #fff,1px -1px 1px #fff,-1px 1px 1px #fff,1px 1px 1px #fff;z-index:2;position:relative;color:"+color+";height:30px;min-height:29px;margin-top:-3px;top:0;text-align:center;font size=16px;font-family:sans-serif;";
                var dicerollStyle = "display:inline-block;font-size:1.2em;font-family:san-sarif" + faces;
                var diconStyle = "display:inline-block;min-width:30px;text-align:center;position:relative";
                var backingStyle = "position:absolute;top:-2px;left:0;width:100%;text-align:center;font-size:30px;color:#8fb1d9;text-shadow:0 0 3px #8fb1d9;opacity:.75;pointer-events:none;z-index:1";
                html += " <div data-origindex=\"0\" style=\"" + dicerollStyle + "\" class=\"diceroll d" + faces + "\">";
                html += " <div style=\"" + diconStyle + "\">";
                html += " <div class=\"backing\"></div>"
                html += " <div style=\"" + didrollStyle + "\">"
                total += eval(value);
                if ((value=="1")||(value==faces)){
                    html+= "<strong>"
                }
                html += value;
                if ((value=="1")||(value==faces)){
                    html+= "</strong>"
                }
                html += "</div>";
                html += " <div style=\"" + backingStyle + "\"></div>";
                html += " </div>";
                html += " </div>";
                if(i == numdice - 1) html += ")";
                else html += "+";
            }
            html += " </div>";
            total = eval(total + modifier);
            html += modifier;
            html += "</div>";
            html += "<div style=\"" + clearStyle + "\"></div><strong> = </strong><div style=\"" + totalStyle + ";" + uidraggableStyle + "\"><strong><font size=\"6\"> " + total + "</strong> </div>";
            sendChat(msg.who, "/direct " + html);
            sendChat("Roll20", "/w gm " + roll + " was rigged to have values " + values.join() + ".");
    }
});

Blind Roll (Stephen)

Works just like a GM Roll.... only the player doesn't see the dice roll result.

!broll 1d100+10 to pick pocket Jimmy<pre>

Would result in the players seeing:

:''"Secret roll sent to GM (1d100+10 to picket pocket Jimmy)"'', with no dice roll result.

<pre data-language="javascript">
on("chat:message", function(msg) {
    var cmdName = "!broll ";
	var msgTxt = msg.content;
	var msgWho = msg.who;
	var msgFormula = msgTxt.slice(cmdName.length);

	if(msg.type == "api" && msgTxt.indexOf(cmdName) !== -1) {
		sendChat(msgWho, "/gmroll " + msgFormula);
		sendChat(msgWho, "/w " + msgWho + " secret roll sent to GM (" + msgFormula + ")");
  	};
});

Token

Make Rollable Table Tokens

!make-rtt(Forum) by Aaron

Little script for making Rollable Table Tokens. Just select a bunch of graphics on the page and run:

!make-rtt

and it will zip them up in a Rollable Table Token. it sorts them in order from top to bottom, left to right. If any graphics are from the marketplace, it will put the dead X on them and not include them. It places the new token at the lop left of the tokens (or at (0,0 if you have some bizzare selected tokens that are somewhat off screen..)


on('ready',()=>{

  const s = {
    err: "padding: 1px 1em; size:.8em; font-weight:bold; background: #cccccc; border:2px solid black; border-radius:1em; color: #990000;"
  };

  const getCleanImgsrc = (imgsrc) => {
    let parts = imgsrc.match(/(.*\/images\/.*)(thumb|med|original|max)([^?]*)(\?[^?]+)?$/);
    if(parts) {
      return parts[1]+'thumb'+parts[3]+(parts[4]?parts[4]:`?${Math.round(Math.random()*9999999)}`);
    }
    return;
  };


  const positionalSorter = (a,b) => {
    let at = Math.round((a.get('top')+17)/35);
    let bt = Math.round((b.get('top')+17)/35);
    let al = Math.round((a.get('left')+17)/35);
    let bl = Math.round((b.get('left')+17)/35);
    let abt = at-bt;
    let abl = al-bl;
    return (0 === abt ? abl : abt);
  };

  const findTraits = (b) => (o) => {
    let x = parseFloat(o.get('left'));
    let y = parseFloat(o.get('top'));
    let w = parseFloat(o.get('width'));
    let h = parseFloat(o.get('height'));
    b.minX = Math.min(b.minX,x-(w/2));
    b.minY = Math.min(b.minY,y-(h/2));
    b.maxX = Math.max(b.minX,x+(w/2));
    b.maxY = Math.max(b.minY,y+(h/2));
    b.layer = o.get('layer');
    b.pageid = o.get('pageid');
    return o;
  };

  on('chat:message',msg=>{
    if('api'===msg.type && /^!make-rtt(\b\s|$)/i.test(msg.content) && playerIsGM(msg.playerid)){
      let who = (getObj('player',msg.playerid)||{get:()=>'API'}).get('_displayname');

      let traits = {
        minX: Number.MAX_SAFE_INTEGER,
        minY: Number.MAX_SAFE_INTEGER,
        maxX: -Number.MAX_SAFE_INTEGER,
        maxY: -Number.MAX_SAFE_INTEGER,
        layer: 'objects',
        pageid: ''
      };


      if(0===(msg.selected||[].length)){
          sendChat('',`/w "${who}" <div style="${s.err}">Please selected some tokens.</div>`);
          return;
      }

      let images = (msg.selected || [])
        .map(o=>getObj('graphic',o._id))
        .filter(g=>undefined !== g)
        .sort(positionalSorter)
        .map(findTraits(traits))
        .reduce((m,g)=>{
          let i = getCleanImgsrc(g.get('imgsrc'));
          if(i){
            m.push(i);
          } else {
            g.set('status_dead',true);
          }
          return m;
        },[])
        ;

        if(images.length){
          let token = createObj('graphic',{
            pageid: traits.pageid,
            layer: traits.layer,
            left: traits.minX||0,
            top: traits.minY||0,
            width: 70,
            height: 70,
            imgsrc: images[0],
            sides: images.map(encodeURIComponent).join('|')
          });
          if(token){
            toFront(token);
          } else {
            sendChat('',`/w "${who}" <div style="${s.err}">Failed to create token!</div>`);
          }
          
        } else {
          sendChat('',`/w "${who}" <div style="${s.err}">Only marketplace images found!</div>`);
        }
    }
  });
});

Show Tooltip

Showtooltip(Forum) by Aaron

Shows Token Tooltip of selected tokens in the chat.

!show-tip //show in chat to all
!wshow-tip   //whisper to self


on('ready',()=>{


  const s = {
    container: `display:inline-block;border:1px solid #999;border-radius:.2em; padding: .1em;background-color:white;width:100%;`,
    img: `max-width: 5em;max-height:5em;display:block;overflow:auto;background-color:transparent;float:left;margin:.5em;`,
    quote: `font-weight: bold;font-style:italic;padding:.3em;`,
    clear: `clear:both;`
  };
  const f = {
    container: (d,q) => `<div style="${s.container}">${d}${q}${f.clear()}</div>`,
    item: (d)=>`<img src="${d}" style="${s.img}">`,
    quote: (q)=>q?`<div style="${s.quote}">${q}</div>`:'',
    clear: () => `<div style="${s.clear}"></div>`
  };

  on('chat:message',msg=>{
    if('api'===msg.type && /^![w]?show-tip(\b\s|$)/i.test(msg.content) && playerIsGM(msg.playerid)){
      let who = (getObj('player',msg.playerid)||{get:()=>'API'}).get('_displayname');
      let whisper = /^!w/i.test(msg.content);
      let msgs = (msg.selected || [])
        .map(o=>getObj('graphic',o._id))
        .filter(g=>undefined !== g)
        .filter(g=>0 !== g.get('tooltip').length)
        .map(t=>f.container(f.item(t.get('imgsrc')),f.quote(t.get('tooltip'))))
        ;
      if(msgs){
        sendChat('',`${whisper ? `/w "${who}" `: ''}${msgs.join('')}`);
      }
    }
  });
});


DropTorch

DropTorch(Forum), A little script that lets you and your players drop their light source on the ground.

RetrieveTokens

RetrieveTokens(Forum) -- get graphics that are off the page

HeathStat

Map

ToggleDaylight

ToggleDaylight(Forum) turn daylight mode on, off, or toggle it for LDL or UDL on the current page.



Other Community APIs