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

Help getting result from SendChat API /roll outside of sendchat

1417989465

Edited 1417990680
DXWarlock
Sheet Author
API Scripter
Back for some help from the api/js gurus. Not sure if the Topic makes any sense at all. Im trying to make a function that does the the same as this: sendChat('', "/roll [[1d6]]", function (ops) { rollresult = JSON.parse(ops[0].content); total = rollresult.total; }); That I can call to do dynamic dice rolls in the API on the fly. OR a way to reference that 'total' outside of the send chat. As you can't read that var of total anywhere but inside that sendChat I tried var avc = DiceRoll("1d6"); function DiceRoll(dice) { sendChat('', "/roll [[" + dice + "]]", function (ops) { rollresult = JSON.parse(ops[0].content); return rollresult.total; }); } log(avc); But that doesn't seem to do what I expected it to..logging the variable after it is still undefined. Reason is I have a lot of places in scripts that uses entries on a players character sheet with values like 2D6 or 1D4*10..that gets exponentially complicated the more rolls I need to do inside one script as I have to nest sendChat inside sendChat inside sendChat..etc. to get all the totals as variables I can use. if I could find a way to convert such things as [[1d6]] [[4]] [[1d8+4]] into a result without having to nest tons of sendChats to get the result it would save me tons of headaches and many many lines of code. Let me see if I can clear it up some for an example. Say a api command is: "!cast --Test Spell --100 ft --2d6 --2d6 --50 --Casting Test Spell --1 ISP" I need to be able to turn 2d6, 3d6, and 50 into [[2d6]], [[3d6]] and [[50]]..which I can do just by wrapping the variable in [[ and ]] no problem. (the reason for doing the seemingly retarded thing of inline roll a 50..is that MAY be a 2d10 for someone else there..so I just inline all numbers..a [[50]] will return 50 anyway) Problem comes when I need to find the result of them..as you cant put [[something]] inside of a html div..it prints $[[1]] in chat like such Only way I can find is to keep nesting sendChats with function (ops) for each one so I can read the result, which gets messy in scripts i need to get 8-9 of them for. Also I cant really use 'msg.inlinerolls[0];' and such as that means it has to come into the chat as inline rolls..and making the players [[-]] all numbers on the character sheet is rather ugly and error prone. Anyone know a quick and easy way to change [[something]] into a number within a script without the need for the whole nested sendChats?
1417990792
The Aaron
Roll20 Production Team
API Scripter
Here is a snippet of code from Ammo: HandleInput = function( msg_orig ) { var msg = _.clone(msg_orig), args,attr,amount,chr,token,text=''; if (msg.type !== "api") { return; } if(_.has(msg,'inlinerolls')){ msg.content = _.chain(msg.inlinerolls) .reduce(function(m,v,k){ m['$[['+k+']]']=v.results.total || 0; return m; },{}) .reduce(function(m,v,k){ return m.replace(k,v); },msg.content) .value(); } The bold parts result in the message having the numbers instead of the $[[1]]. For example, if you called the api with: !msg bar --[[1d12]] with [[1d20+3]] and [[50]] In the script, msg.content would look like: !msg bar ——$[[0]] with $[[1]] and $[[2]] The large bold block at the end will replace all those $[[#]] bits with the values of the inline rolls: !msg bar ——3 with 22 and 50 The earlier msg_orig bits are to create a copy of the message object passed into the function. This is because each handler in the API gets passed the same message object, so making changes to it will cause every following handler to get the changed version. Hope that helps! Full msg object for reference: { . "content": "!msg bar ——$[[0]] with $[[1]] and $[[2]]", . "inlinerolls": [ . . { . . . "expression": "1d12", . . . "results": { . . . . "resultType": "sum", . . . . "rolls": [ . . . . . { . . . . . . "dice": 1, . . . . . . "results": [ . . . . . . . { . . . . . . . . "v": 3 . . . . . . . } . . . . . . ], . . . . . . "sides": 12, . . . . . . "type": "R" . . . . . } . . . . ], . . . . "total": 3, . . . . "type": "V" . . . }, . . . "rollid": "—JcaosC_Sv7fVN9vkaLe", . . . "signature": "8c84a91f34a03015c98e37684b883cc74cb7ad5154bb12bd353b55edc083d004f9c79c2d39f4c9b492713cd690ffe82595294d3ff98c1af4b7a380922a371e7f" . . }, . . { . . . "expression": "1d20+3", . . . "results": { . . . . "resultType": "sum", . . . . "rolls": [ . . . . . { . . . . . . "dice": 1, . . . . . . "results": [ . . . . . . . { . . . . . . . . "v": 19 . . . . . . . } . . . . . . ], . . . . . . "sides": 20, . . . . . . "type": "R" . . . . . }, . . . . . { . . . . . . "expr": "+3", . . . . . . "type": "M" . . . . . } . . . . ], . . . . "total": 22, . . . . "type": "V" . . . }, . . . "rollid": "—JcaosCbFWbXeMf4mPI3", . . . "signature": "8f4eb42756b09503ae22318eaf1ab095409e576a0292b9e71c36539672dd9aee8a7814c89c492cf3ee7779b86399038b60cd1c2e5ff564625fe4b1b679542d3d" . . }, . . { . . . "expression": "50", . . . "results": { . . . . "resultType": "M", . . . . "rolls": [ . . . . . { . . . . . . "expr": 50, . . . . . . "type": "M" . . . . . } . . . . ], . . . . "total": 50, . . . . "type": "V" . . . }, . . . "rollid": "—JcaosCch6elAyLnK4sd", . . . "signature": "ac905405a7936c694af2168b6924b37a644e1d830c859a712e1e55d8fa587707f5a5cae50e539bd66e4a7d75165c14b4a528583cc23e50ad7e275eb7cc6769f3" . . } . ], . "playerid": "—JS3qKxIUPHLzSbK24ve", . "type": "api", . "who": "The Aaron" }
1417991040

Edited 1417991969
DXWarlock
Sheet Author
API Scripter
(Think I edited my post above to talk about that after you started replying..sorry Aaron) That would help if they as coming into with the command as [[something]] but they are pulling from the character sheet as '2d6'. I tried to get the players to put [[2d6]] all the time on the sheet, but they messed it up a lot, and made the sheet mighty ugly when they did :( Im having to convert them to [[2d6]] after i get it and make a result, and only way I know is a sendChat again. So cant use the msg.inlinerolls as it has none :\ I could do a regex to parse out the 2, and the 6 and do a randomint thing for example. but it can be 2d6, 1d4*10, 50, 4, 3d6/2..etc so a bit hard to regex it to make it figure out what to do. Why I was trying to work out how to do a inline roll in API that I can call as a function, since just wrapping it in [[-]] and getting a result works for anything.
1417991838

Edited 1417991889
DXWarlock
Sheet Author
API Scripter
Maybe this will help explain my horrible way of explaining it :P here is the button and entry on the sheet: the button sends it to chat as: "!cast --Test Spell --100 ft --2d6 --2d6 --Casting Test Spell --1 ISP" which with my script does this: Which is great, but trying to convert that 2d6 and 2d6 to numbers without overly nested sendChat is killing me. And the simple solution of having them use [[ and ]] on the sheet seems to confound and confuse them..they broke it ALL the time using [2d6]] or [[2d6] or ((2d6)) or any other user error you could think of..haha So trying to compensate by working it on my end to do it for them.
1417992237

Edited 1417992329
The Aaron
Roll20 Production Team
API Scripter
Ah. Sorry, I misunderstood. =D Using sendChat() is very frustrating as it's asynchronous. In scripts I've written that need to do this preprocessing, I build up one large message with multiple inline blocks, then read them back in the order I wrote them in, allowing me to have only a single sendChat() call. I don't think there is a better way of handling that. Here is a small script I wrote for someone that wanted to expand attributes passed to HB's power cards script, but only after they had been adjusted in the same macro by !ammo calls. (Just putting the attribute in meant he got the pre-!ammo adjusted value.) The basic idea was to write inline expressions as [#[ something ]#] with attribute references as @#{attr|stuff}. The first _.reduce() call finds all the [#[ ... ]#] blocks and makes a map from them to the expression to execute for them. The second _.reduce() is expanding the @#{...} attribute references to their values. The third _.reduce() is replacing the attributes and [#[ and ]#] bits with their value or [[ and ]] respectively. The fourth _.reduce() is basically just doing a .join(), but for an object, resulting in the expr to pass to sendChat(). The fifth _.reduce() then replaces the [#[ text ]#] in the original message with the $[[#]] syntax, and appends the inline rolls from the sendChat() to the original messages's inline rolls, then passes it to HB's PowerCardScript.Process(msg); on('ready',function(){ 'use strict'; on('chat:message',function(msg_orig) { var msg = _.clone(msg_orig), cmd,postMap,expr; if (msg.type !== "api") { return; } cmd = msg.content.split(/\s+/,1); if(cmd === "!post-power") { // Get the API Chat Command msg.who = msg.who.replace(" (GM)", ""); msg.content = msg.content.replace("(GM) ", ""); postMap = _.reduce ( msg.content.match(/\[#\[(.*?)\]#\]/g), function(gmap,g){ var attrmap= _.reduce (g.match(/@#\{(.*?)\}/g), function(amap,a){ var parts=a.match(/@#\{([^|]*)\|([^\|]*)\|?(.*)?\}/), char = findObjs({ type:'character', name: parts[1] })[0], attr; amap[a]=0; if(char) { attr = findObjs({ type: 'attribute', characterid: char.id, name: parts[2] })[0]; if(attr) { amap[a]=attr.get(parts[3]==='max'?'max':'current'); } } return amap; },{}); attrmap['[#[']='[['; attrmap[']#]']=']]'; gmap[g]= _.reduce (attrmap,function(gmemo,v,k){ return gmemo.replace(k,v); },g); return gmap; },{}); expr= _.reduce (postMap,function(m,g){return m+g;},''); sendChat ('',expr,function(res){ var num=msg.inlinerolls.length; msg.content= _.reduce (postMap,function(msgCon,v,k){ return msgCon.replace(k,'$[['+(num++)+']]'); },msg.content); _.each(res[0].inlinerolls,function(r){ msg.inlinerolls.push(r); }); PowerCardScript.Process(msg); }); } }); }); That's probably a massively complicated example of what you're trying to do, but maybe it will help. (man.. who knew I used that many _.reduce() calls!) If that sounds like what you're wanting to do, I can probably help you figure it out.
1417993552
DXWarlock
Sheet Author
API Scripter
Wow, that is beautifully complexly simple (if that makes sense) to do what seems such a simple thing, as a work around..haha. See if I was trying to do what you did above Id end up with a 500 line separate function script trying to do it all..:) if I could get my players to use syntax right that would be an amazing way to do it. But I think that puts me back where I was of making sure they put stuff on the sheet correct..which they never do. some of them anything beyond '/roll 2d6+1' gets a response of "Come on, Im not a programmer man..I cant remember that hieroglyphic code stuff". BUT i think with what you got above, I can piecemeal together something that I can do with the button and parts of it..and just force send it from the button with the correct syntax for that. Since the button on the sheet sends it. Thanks man! will take me a bit to go thru it and sort out which part is doing what (and what some parts of it is doing at all..time to google and learn what some of those calls even are :P) but that seems 10x easier than my super 'inception like' nested sendChats solution.
1417993888
The Aaron
Roll20 Production Team
API Scripter
William R. said: Maybe this will help explain my horrible way of explaining it :P here is the button and entry on the sheet: the button sends it to chat as: "!cast --Test Spell --100 ft --2d6 --2d6 --Casting Test Spell --1 ISP" which with my script does this: Which is great, but trying to convert that 2d6 and 2d6 to numbers without overly nested sendChat is killing me. And the simple solution of having them use [[ and ]] on the sheet seems to confound and confuse them..they broke it ALL the time using [2d6]] or [[2d6] or ((2d6)) or any other user error you could think of..haha So trying to compensate by working it on my end to do it for them. So, just the Damage and Duration need to be "rolled" in the output? Could you just adjust your output to put the [[ and ]] around them? So, not expand them in the script, but let the final sendChat() that displays the card do the expand?
1417994321

Edited 1417994761
DXWarlock
Sheet Author
API Scripter
Well it can be any of them are variable, and some are static values, depending on the power. like Range can be 100 ft or 2d6 ft. or even "touch' which I cant do [[touch]]. So was not sending it as [[something]] from the macro. I was checking in my script if the first letter was a number.. if it was parse out the first part before the space, like '100 ft' becomes [[100]] and '2d6 ft' becomes [[2d6]] and try to figure out how to roll it. I'm starting to realize I'm making it WAY overly complicated for me to figure out, just so its simple and quick for my players. lol If only the randomInteger(max) api function took dice, like randomInteger(2d6), would be so much simpler.