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

Finding the roll result of inline rolls

1432361797

Edited 1432361988
So I have been unsuccessfully trying to find the results of an inlineroll. Here is what I have been trying but the numbers it is returning are the dice I am rolling not the results. on("chat:message", function(msg) { //log(msg); if (msg.type == 'general') { var inlineroll = null; try { inlineroll = msg.inlinerolls; } catch (e) { log(e); return; } //log(inlineroll); if(inlineroll) { for(var i = 0; i < inlineroll.length;i++) { if(inlineroll[i]) { var numbers = inlineroll[i].expression.match(/\d+/g); sendChat(msg.who, numbers.toString()); } } } } });
1432362619
The Aaron
Pro
API Scripter
Here is a function I use for substituting in the total of the inline rolls of a message: function processInlinerolls(msg) { if (_.has(msg, 'inlinerolls')) { return _.chain(msg.inlinerolls) .reduce(function(previous, current, index) { previous['$[[' + index + ']]'] = current.results.total || 0; return previous; },{}) .reduce(function(previous, current, index) { return previous.replace(index, current); }, msg.content) .value(); } else { return msg.content; } } <a href="https://wiki.roll20.net/API:Cookbook#processInline" rel="nofollow">https://wiki.roll20.net/API:Cookbook#processInline</a>... Inline rolls are an object, and the expression property is just what was typed in between the [[ ]] after attributes and such are expanded. You are interested in the results.total (if you just want the final sum) or the results.rolls array if you want to examine each die. For you code, I'd modify it like: on("chat:message", function(msg) { if (msg.type == 'general' && _.has(msg, 'inlinerolls') ) { for(var i = 0; i &lt; msg.inlinerolls.length;i++) { sendChat(msg.who, 'Total was: '+msg.inlinerolls[i].results.total); } } });
Using the modifed code I receive the following error: Your scripts are currently disabled due to an error that was detected. Please make appropriate changes to your scripts and click the "Save Script" button and we'll attempt to start running them again. More info... For reference, the error message generated was: /home/symbly/www/d20-api-server/node_modules/firebase/lib/firebase-node.js:1 orts, require, module, __filename, __dirname) { function g(a){throw a;}var j=v ^ TypeError: Cannot read property 'results' of undefined at evalmachine.&lt;anonymous&gt;:322:54 at eval (
1432368289
The Aaron
Pro
API Scripter
I went to bed hours ago so I can't directly help you debug it right now. For some reason, msg.inlinerolls[i] doesn't exist. You could add some guards around the sendChat() like: if(msg.inlinerolls[i]) { sendChat('',...); }
That did it, You have again saved me! Thank you!
1432396346
The Aaron
Pro
API Scripter
No problem. =D If I were actually writing that, I would have used something like: _.each(msg.inlinerolls, function(r){ sendChat(msg.who, 'Total was: '+r.results.total); } Letting _.each() handle the iteration avoids indexing issues like that, generally. if you're interested, I have some tutorials in the API Cookbook: <a href="https://wiki.roll20.net/API:Cookbook#Collections" rel="nofollow">https://wiki.roll20.net/API:Cookbook#Collections</a>