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

Working /as & /emas with Avatars.

So, my problem is this: I pre-set my sessions with macros full of story and descriptions. I do a lot of /emas, /as, and /desc in it, obviously.  But this doesn't show the avatars. I've tried to create a simple script that should fix that: on("chat:message", function(msg) { if(msg.type == "api" && msg.content.indexOf("!as ") != -1) { log(msg); var n = msg.content.split('"', "3"); //Splits the message into three components separated by double quotes: cmd "as" mes var cmd = n[0]; var as = n[1]; var mes = n[2]; sendChat('character|' + as, mes); } }); But when I use it it doesn't turn out correctly.  Input: !as "Dack Cyon" Hello there. Output: character|Dack Cyon: Hello There I've tried quite a few iterations of it, and always the same. Need some help. (And yes, I made sure there's a character named "Dack Cyon". )
1552213357

Edited 1552220577
GiGs
Pro
Sheet Author
API Scripter
You need to use the character id, not their name, with 'character!'. that means you need a function to grab the character id from the name. This is not tolerant of typos. Also your split limit of 3 there seems a bit risky. If you have more than 3 elements, the ones after the 3rd will be discarded. I'd recommend using a different separator, like -- or |, so you would type say !as|Bilbo|<your outputted message> You could use anything as the symbol as long as its not going to be repeated in your message text. Here's a quick-and-dirty version of the script. on("ready", function() {      const separator = '|',         scriptName = 'EMAS';     const getCharacter = (name) => {         const characters = findObjs({                 type: 'character',                 name: name         });         if (!characters || !characters.length) return `No Matching Characters for Name: ${name}`;         else if(characters.length > 1) return "More Than One Matching Character";         return 'character|' + characters[0].id;//'character|' + characters[0].id;     };     on("chat:message", function(msg) {         if(msg.type == "api" && msg.content.indexOf(`!as${separator}`) === 0)         {             const n = msg.content.split(separator);    //Splits the message into three components separated by double quotes: cmd "as" mes             const as = getCharacter(n[1].trim()); // added a trim just to handle any accidental spaces             const mes = n[2];             if(as.indexOf('|') !== -1) // tests to see 'as' is in format 'character!'                 sendChat(as, mes);              else                 sendChat(scriptName, `/w ${msg.who} Error: ${as}. <br>Message:<br> ${mes}`);                      // in case of an error, sends you the message so you can copy & paste without having to retype it         }     }); });
Thank you so much! I’ve hobbied in Coding/Scripting for the past 7 or 8 years, and know enough to be dangerous. Always considered going back to school for it   I definitely see why that should work now and why mine wasn’t  lol  
1552220653
GiGs
Pro
Sheet Author
API Scripter
You're welcome! I just realised I'd left in some of my testing lines, I've updated the script above.