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

Using API output inside macro (formatting and execution)

Hi, I am fairly new to the API for roll20 and have limited knowledge with javascript, but I'm well versed in other programming languages and I'm a fast learner. Hence, I figured I could create my own API script to add a feature I was missing for my macros. I need to be able to reuse the same roll within the same macro.         - I'm running into some issues though ^^' As I see it, when you send a message to the chat via the API (sendChat) you lose the macro formatting. How do I keep that formatting? This is my script to roll any size dice and keep them temporarily: on("chat:message", function(msg) {     if(msg.type !== "api") return;     if(msg.content.indexOf("!keeproll ") !== -1) {         var apicallmsg = msg.content.substring(msg.content.indexOf("!keeproll "), msg.content.indexOf("SR!") + 2);         var dindex = apicallmsg.indexOf("d")                  var redicecount = apicallmsg.substring(10, dindex); //<redicecount>d<redicesize>         var redicesize = apicallmsg.substring(dindex + 1, apicallmsg.length - 2);                  var remsg = msg.content.replace("!keeproll " + redicecount + "d" + redicesize + "SR!","");                  var i;         var reroll = 0;         for (i = 0; i < redicecount; i++) {              reroll += randomInteger(redicesize); //between [1,d]         }                  while(remsg.indexOf("!getroll") !== -1) {             remsg = remsg.replace("!getroll", reroll);         }         sendChat(msg.who, remsg);     } }); This is my macro and the output in roll20 chat: PS: I am also using !TokenMod within the same type of macro. I left it out of the example above to simply things. Any help is greatly appreciated! Thanks!
1557431463

Edited 1557431745
GiGs
Pro
Sheet Author
API Scripter
If you're sending stuff to to the API, you should just send the parameters needed, then create the formatting within the script. Treat the macro as a function call, with arguments. For instance, in your macro, you'd probably be better off sending it as something like !keeproll [[2d6!]] (what's the 2d6SR! roll supposed to do btw, I dont recognise SR as roll modifier?) Then in your script you can get the roll result from msg.inlinerolls, or using Aarons function to convert rolls to text , and in your script create the output something like let output = "&{template:default} {{name=API Roll Output}} {{roll=" + rollresultvariable + "}}; sendChat(msg.who,output); This is a quick and dirty example. There are better ways to do it. I'm just giving you pointers on this approach.
1557435394

Edited 1557436719
Thanks for the quick response GiGs! The "SR!" is used as a terminator to the API call, to allow for any sized input. Thanks for providing a quick resolve to the inline rolls and convertion to text. Seems to work only if I don't include an API call inside the [[ ]] brackets though.
1557436492
GiGs
Pro
Sheet Author
API Scripter
Why do you want to put an API call inside the [[ ]] ?
1557436750
GiGs
Pro
Sheet Author
API Scripter
By the way, if you don't have complex needs for your macro, and just need to reuse a roll within a macro, you might find the Power Cards API script saves you some work. It's a script already written that allows lots of formatting options for macros, and lets you reuse rolls. there's a thread for it in the API forum for getting help with it if you want to try it out.
Well my use would be replacing the "!getroll" by the number rolled in the script (say it was "7") like so: From: [[!getroll + @{target|dexterity_mod}]] To: [[7 + @{target|dexterity_mod}]] But I think I might be able to do this inside the API script instead. I'll give it go there first!