Skip to content

Instantly share code, notes, and snippets.

@Bastlifa
Created November 24, 2017 23:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Bastlifa/a74a4af2f621383959a8e3208458e14a to your computer and use it in GitHub Desktop.
Save Bastlifa/a74a4af2f621383959a8e3208458e14a to your computer and use it in GitHub Desktop.
Face Changing API Script for Roll20
/*
* A Simple face changing script.
* Make a rollable table with the character name (one word characer name) plus -Faces
* Example, character named John Smith
* Table name is John-Faces
* Fill table with face names, and pictures which you have uploaded. Can't be from purchase or search
* Must be uploaded to your library.
* Make macro with this body, as token action macro:
* !FaceChange John-?{Face|Normal|Soldier|Popper|Pirate|Noble|Elder|Cleric}
* Where those options (other than the title "Face") are the names of the items in your rollable table.
*/
on("ready", function() {
var getCleanImgsrc = function (imgsrc) {
var 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;
};
on("chat:message", function (msg) {
if (msg.type === "api" && msg.content.split(' ')[0] === "!FaceChange") {
FaceGet(msg);
}
});
function FaceGet(msg)
{
var args = msg.content.split(' ');
var charName = args[1].split('-')[0];
var charFaceName = args[1].split('-')[1];
var faceTableName = charName + "-Faces";
var faceTable = findObjs({type: 'rollabletable', name: faceTableName})[0];
var faceItem = findObjs({type: 'tableitem', _rollabletableid: faceTable.id, name: charFaceName})[0];
_.each(msg.selected,function (o) {
getObj(o._type,o._id).set({
imgsrc: getCleanImgsrc(faceItem.get('avatar'))
});
});
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment