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 .
×

Api for art showcase in chat

Hi everyone! Just like to post an API script of mine that can show art of characters in chat :) Not really seen any post about this so I created my own script. If you add this to your own scripts as a .js file. After that you just need select token and type !art in chat. (Or just create a macro for it) It helped me a lot in my games. getCharNameById = function (id) { const character = getObj("character", id); return (character) ? character.get("avatar") : ""; } on('chat:message', function(msg) {     if (msg.type === 'api' && msg.content === '!art' && msg.selected) {         _.each(msg.selected, (s) => {                           obj = getObj(msg.selected[0]._type, msg.selected[0]._id);                            charid = obj.get("represents")                            url = getCharNameById(charid)                            sendChat(msg.who, `${url}`);         });     } }); Still working on a solution where players can just hover over tokens and that will showcase character art at left down corner but not yet sure how. Best regards, csbala! 
1670200700
The Aaron
Roll20 Production Team
API Scripter
That's a nice little script!  Here's how I might approach the same thing: on('ready',()=>{ on('chat:message', (msg) => { if('api'===msg.type && /^!art(\b\s|$)/i.test(msg.content) ){ let c = [...(new Set((msg.selected || []) .map(o=>getObj('graphic',o._id)) .filter(g=>undefined !== g) .map(g=>getObj('character',g.get('represents'))) .filter(c=>undefined !== c) .map(c=>c.get('avatar')) .map(u=>`<div><img src="${u.replace(/\?.*$/,'')}"></div>`) ))] .join('') ; if(c){ sendChat(msg.who, c); } } }); }); It's a good idea to wrap all your scripts in a closure, either by putting them in an IIFE, or in the handler for on('ready',...).  That prevents cluttering the global namespace, and functions like getCharNameById() might already be defined in other scripts and so would cause a collision.  I've expanded this to operate on all selected graphics, and then made use of Set() to reduce to just the unique images.  I also like to use a regular expression to match the commands, that way I can account for if they are being called with a space or other arguments after them.  That can reduce confusion. Hope you find that useful!
This is a really useful tool for me, thank you!  I've been using a macro that prompts me for an image URL, but this will save me a ton of clicks.  I've got a question, though.  There are NPC sheets (5e by Roll20) that have a large token image and the artwork from which it was created.  When I run this script, I get the large token image (I suspect it's because of its placement on the page).  Is there a way around this?