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

Catching dice rolls

1390247326

Edited 1390247567
DXWarlock
Sheet Author
API Scripter
Ok ive been banging my head against this for a while..perhaps its just me being dense. How do you catch rolls done from in chat with the API? For example if someone does /roll 1d20 or [[1d20]] in the chat window, I cant for the life of me figure out the API callback to work with that. The reason is I got a lot of API scripts I pieced together from other peoples to get what I need, but my players miss seeing the 3d dice roll, since I'm doing a saychat(msgwho, /roll 1d20); for example, which doesn't do a 3d dice roll, or they forget to do !roll and do /roll and it doesn't work how they expected. So trying to catch when they actually do a roll to parse it out. Is it possible to listen for chat rolls? If so anyone gracious enough to give a short example of how to catch and parse it, so I can expand on it?
1390259902

Edited 1390259984
Lithl
Pro
Sheet Author
API Scripter
on('chat:message', function(msg) { var rolls = []; if (msg.type == 'rollresult') { // User typed /roll rolls.push(msg.content); } if (msg.inlinerolls) { // User has at least one inline roll _.each(msg.inlinerolls, function(r) { rolls.push(r); }); } _.each(rolls, function(rolldata) { log(rolldata); }); }); The data you get from a dice roll is large, so it's probably worth simply looking at what's available in the log over simply being told to use {X}, when what you really needed was {Y}.
1390262917
DXWarlock
Sheet Author
API Scripter
AH thanks! think that got it. I can get the result from it now..but results returns: [{"v":11}] if I add this under both 'if's : var message = JSON.parse(msg.content); log(message.rolls[0].results); log(message); (and totally breaks on inline rolls with that.) Total returns a raw number, but I need the dice result how to I get a raw number rolled (without mods), vs that output type?
1390279506
Lithl
Pro
Sheet Author
API Scripter
Sorry, the rollresult 's msg.content needs to be parsed, but msg.inlinerolls does not. [{"v":11}] is an array (length 1) containing an object with a single property (v) with the value 11. message.rolls[0].results[0]['v'] should be the number 11 in that scenario. Here's an example from typing /r 3d7+10 , with some line breaks and comments thrown in { "type":"V", "rolls": [ { "type":"R", "dice":3, // 3 dice "sides":7, // which were d7s "mods":{}, "results":[{"v":6},{"v":2},{"v":3}] // rolled a 6, 2, and 3 }, { "type":"M", "expr":"+10" // Added 10 to the 3d7 } ], "resultType":"sum", "total":21 } Contrast that with /r [[2d5]]d6+d20 { "type":"V", "rolls": [ { "type":"R", "dice":5, // the 2d5 totalled 5, see below "sides":6, // 5d6 "mods":{}, "results":[{"v":4},{"v":4},{"v":5},{"v":4},{"v":5}] // rolled 4, 4, 5, 4, 5 }, { "type":"M", "expr":"+" // Add the previous roll with the next one }, { "type":"R", "dice":1, "sides":20, // 1d20 "mods":{}, "results":[{"v":17}] // rolled a 17 } ], "resultType":"sum", "total":39 } The 2d5 was stored in msg.inlinerolls : { "expression":"2d5", "results": { "resultType":"sum", "rolls": [ { "dice":2, // rolled 2 dice "results":[{"v":4},{"v":1}], // rolled 4 and 1 "sides":5, // rolling d5s "type":"R" } ], "total":5, "type":"V" } } As you can see, the information you get from the dice roller can be comlicated, and these examples didn't even delve into things like comparison operators, rerolls, grouping, keep high/low, etc. If you've got some more specific needs for your script, it's easier, but trying to handle all cases is difficult, to say the least. Fortunately, you won't ever have to deal with queries in your script, since those are processed before control is passed on to the API, so you don't have to worry about them.
1390330233

Edited 1390333822
DXWarlock
Sheet Author
API Scripter
I got it half working, the inline rolls part is kicking me in the butt :P I figured out how to check if its a d20 if so store result as a number (var actualr), and pass it to the end..but doing the same to inline rolls just gets me errors no matter what I do: What im attempting to do is get 1d20 rolls either /r d20 or [[1d20]] and check the raw result without mods.. on('chat:message', function (msg) { var rolls = []; var actualr = []; if (msg.type == 'rollresult') { rolls.push(msg.content); var message = JSON.parse(msg.content); if (message.rolls[0].sides != '20') { log('not d20'); return; } var actualr = (message.rolls[0].results[0]['v']); } if (msg.inlinerolls) { log(msg.inlinerolls); _.each(msg.inlinerolls, function (r) { rolls.push(r); }); } _.each(rolls, function (rolldata) { log(rolldata); }); log(actualr); // do something with actualr here later });
1390367044
Lithl
Pro
Sheet Author
API Scripter
if (msg.inlinerolls) { _.each(msg.inlinerolls, function(r) { var firstPart = r.results.rolls[0]; if (firstPart.sides == 20) { log(firstPart.results[0]['v']); } }); } That ought to do it. Remember that there might be multiple inline rolls in a single message (hence _.each(msg.inlinerolls, ...); ). Also, on line 3 you're defining actualr to be an empty array, while on line 10 you're defining it to be the value of a single roll. (You're also using var a second time on line 10, which you shouldn't do.) That doesn't actually hurt anything since JS is a dynamically typed language, but it does make the code more confusing to read ("Did he mean actualr should be an array...?")
1390367544

Edited 1390367592
DXWarlock
Sheet Author
API Scripter
Ah I was thinking I had to declare it, so the last line could read it outside the if statement. And never noticed I was defining it twice as a var, instead of setting it, then just redefining its value, thanks for catching it And thanks for all the help, the roll/inline results parsing was kicking my butt :)
1390370417

Edited 1390372517
DXWarlock
Sheet Author
API Scripter
Ah ha, finally got it with thanks to Brian! Now if they do /r 1d20 attack , /r 1d20+2 attack , [[1d20]] attack , or [[1d20+10]] attack..etc (attack being the key to trigger the lookup vs other d20 rolls..it works! (couldn't figure out a way to stop a loop at the bottom with my sendchat retriggering the whole script other than making sure actualr = a number..but it works) Now to flesh out the part of actually DOING something with it. Thanks a million man, I owe you a glass of scotch. on('chat:message', function (msg) { var rolls = []; var actualr = []; var msgTxt = msg.content; var msgFormula = msgTxt.split(" "); if (msg.type == 'rollresult') { rolls.push(msg.content); var message = JSON.parse(msg.content); if (message.rolls[1].text == null) return; if (message.rolls[0].sides == 20 && message.rolls[1].text == ' attack') { actualr = (message.rolls[0].results[0]['v']); } } if (msg.inlinerolls) { _.each(msg.inlinerolls, function (r) { var firstPart = r.results.rolls[0]; if (firstPart.sides == 20 && msgFormula[1] == 'attack') { actualr = (firstPart.results[0]['v']); } }); } if (actualr >= 1) { sendChat('dm', '/direct <b><big>Attack Roll: ' + actualr) } });
1390372504

Edited 1390372618
DXWarlock
Sheet Author
API Scripter
hmm spoke too soon :) need to figure out a way that if 'text' attack doesn't exist it doesn't crash. seems if (message.rolls[1].text == null) return; isnt what I need...since it doesnt know what 'text' is unless the roll had text following it.
1390372702

Edited 1390372717
hello, just a lowly newbie, but i saw this forum had the word dice in it. close enough. is there a way to roll more then one kind of die? im trying to figure out how to roll a fistfull of dice, and have it all calculate. Eg 2d8+1d6+12 any ideas?
1390373256

Edited 1390373275
DXWarlock
Sheet Author
API Scripter
MichaelAB do you mean in my API I'm fighting with, or in general? If you are just trying to get a normal roll, type /r 2d8+1d6+12 in chat or [[2d8+1d6+12]] for a single output if so this may be what your looking for: <a href="https://wiki.roll20.net/Dice_Reference" rel="nofollow">https://wiki.roll20.net/Dice_Reference</a>
thank you kindly! that is exactly what i am looking for
1390374931
DXWarlock
Sheet Author
API Scripter
No problem :)