Roll20 uses cookies to improve your experience on our site. Cookies enable you to enjoy certain features, social sharing functionality, and tailor message and display ads to your interests on our site and others. They also help us understand how our site is being used. By continuing to use our site, you consent to our use of cookies. Update your cookie preferences .
×
Create a free account

[Script] DM Toolkit - Snippets

1491036521

Edited 1491038413
Gonna use this thread to post a bunch of little snippets of code I use to make my job as DM easier. A lot of these can be replicated by other scripts, but perhaps some people intimidated by those more complex and powerful scripts will like these little snippets instead. The first one is something I wrote a long time ago to reset all the tokens on the map to full health with no status icons on the tokens. Usage: !reset-tokens This simple command can be used to quickly reset all the tokens on the map to full health and remove any status icons you have set on them, such as the red X for a dead token. Click anywhere on the map while on the Objects & Tokens layer to set your focus to the map part of the window and then hit CTRL + A to select all the tokens on that layer. Then type !reset-tokens into chat and all the tokens selected will have the status icons removed from them and their Bar1 value restored to the Bar1 max value. If you use Bar2 or Bar3 instead for hit points, change HPBar = 1 to 2 or 3 instead. // API COMMAND HANDLER on("chat:message", function(msg) {     if (msg.type !== "api") return;          // Set tokens to full hit points and remove all status icons...     if (msg.content.split(" ")[0] === "!reset-tokens") {         var TokenID = msg.content.split(" ")[1];         var Token;         if (TokenID !== undefined) {             Token = getObj("graphic", TokenID);             if (Token !== undefined) {                 Token.set("statusmarkers", "");                 Token.set("bar1_value", Token.get("bar1_max"));             }         } else if (msg.selected !== undefined) {             _.each(msg.selected, function(a) {                 Token = getObj("graphic", a["_id"]);                 if (Token !== undefined) {                     Token.set("statusmarkers", "");                     Token.set("bar1_value", Token.get("bar1_max"));                 }             });         } else {             log ("No tokens selected or targeted.");         }     } });
1491037634
Jakob
Sheet Author
API Scripter
You should probably add a safeguard there for when there are objects not of type graphic on the objects layer (such as drawings). Token will end up undefined if any of these are selected with CTRL-A, and then Token.set gives an error and crashes the sandbox.
1491037817

Edited 1491038473
USAGE: !remove-sight This snippet will remove all light source and vision settings on selected tokens. Useful for removing sight and lighting from all the monster manual tokens or tokens on the map in modules like Lost Mine of Phandelver or the Yawning Portal addons. This will remove light from everything though, including campfires, torches, and such if they are on the layer you use this script on. So before using this snippet, make sure all light sources are moved to either the map layer or dynamic lighting layer and then use CTRL + A to select everything on the Objects & Tokens layer or the GM Layer. // API COMMAND HANDLER on("chat:message", function(msg) {     if (msg.type !== "api") return;          // Remove Light & Vision settings from MM Tokens...     if (msg.content.split(" ")[0] === "!remove-sight") {         var TokenID = msg.content.split(" ")[1];         var Token;         if (TokenID !== undefined) {             Token = getObj("graphic", TokenID);             if (Token !== undefined) {                 Token.set("light_radius", "");                 Token.set("light_dimradius", "");                 Token.set("light_hassight", false);             }         } else if (msg.selected !== undefined) {             _.each(msg.selected, function(a) {                 Token = getObj("graphic", a["_id"]);                 if (Token !== undefined) {                     Token.set("light_radius", "");                     Token.set("light_dimradius", "");                     Token.set("light_hassight", false);                 }             });         } else {             log ("No tokens selected or targeted.");         }     } });
1491038137

Edited 1491038727
Jakob said: You should probably add a safeguard there for when there are objects not of type graphic on the objects layer (such as drawings). Token will end up undefined if any of these are selected with CTRL-A, and then Token.set gives an error and crashes the sandbox. Ah, never used it without any tokens and only drawings on the page. Thanks! -edit- Snippets fixed! Thanks again Jakob.
RollHP I use it to re-roll HP for several tokens at once. Very useful for purchased modules where all tokens usually have average HP. Thanks to  Aaron whos script I repurposed.  var RollHP = RollHP || (function () { 'use strict'; var barNumber = 1, attrName = 'npc_hpformula', setupManualAttribute = function (token) { const attr = findObjs({type: 'attribute', characterid: token.get('represents'), name: attrName})[0]; if (attr) { sendChat('', `[[${attr.get('current')}]]`, (results) => { if (results && results.length === 1) { if (!results[0].inlinerolls || !results[0].inlinerolls[0]) { log('HP roll didn\'t have the expected structure. This is what we got back: ' + results[0]); } else { const hp = results[0].inlinerolls[0].results.total; token.set(`bar${barNumber}_value`, hp); token.set(`bar${barNumber}_max`, hp); } } }); } }, handleInput = function (msg) { var args; if (msg.type !== "api") { return; } args = msg.content.split(/\s+/); switch (args.shift()) { case '!roll-hp': _.chain(msg.selected) .map(function (o) { return getObj('graphic', o._id); }) .reject(_.isUndefined) .reject(function (t) { return undefined === t.get('represents'); }) .each(setupManualAttribute) ; break; } }, registerEventHandlers = function () { on('chat:message', handleInput); }; return { RegisterEventHandlers: registerEventHandlers }; }()); on('ready', function () { 'use strict'; RollHP.RegisterEventHandlers(); }); Usage: Select one or several tokens (you can use ctrl+A to select everything on current layer) and type !roll-hp in chat.
These are awesome, I would love to see more people add small utilities like this to the thread!
1491063753
keithcurtis
Forum Champion
Marketplace Creator
API Scripter
Thanks, Sky.
Kevin said: These are awesome, I would love to see more people add small utilities like this to the thread! I have more. I'm just going through and cleaning them up and adding error-checking/idiot-proofing since a lot of them were written way back when I was still learning javascript.
1492561428
Garrett K.
Marketplace Creator
more more more!
Garrett K. said: more more more! Sorry... got sidetracked with other stuff. :D
1494544977
Nick S.
Pro
Marketplace Creator
Translator
Thanks for these Sky! Would it be possible to change the !reset-tokens so that it also uses the "long rest" feature for all tokens? (Using the 5e shaped sheet) It's not always necessary, but when using casters it recovers the spell slots and so on. Cheers!
Sorry, I don't give two shits about the shaped sheet. It's horrible, imo.