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

Get roll result from chat

Hello everyone, I'd like to pull into my script the last roll result done in the chat, how can I do this? I've looked at the chat section of the API Help Center but it makes no mention of that! Is the roll result still a chat:message  event?
As far as I know you can't unless you are using it in an api within the same macro, for example powercards or scriptcards. Or even alterbars. You could possibly store it on a character sheet using chatsetattr.  !power {{ --roll|[[ [$roll]1d20]] --api_setatr|_name @{selected|token_name} _LastRoll|$roll }} Not sure if _LastRoll|$roll is the right format.  Might work better if you had a generic character sheet to store it on though.
1632602823
timmaugh
Forum Champion
API Scripter
Everything going through the chat will create a chat event, even messages not intended for the API. A script can listen and take action based on a message that wasn't intended for the API, but it cannot stop the original message from hitting the chat. So you could write a script that would listen to every message and track the roll information and that wouldn't interfere with how the message progressed through the chat. The question would be what to do with a message that had multiple rolls, or nested rolls. Which one would you want to track? Off the shelf, you might be able to do something like this with Custom Roll Listener. If that doesn't let you do what you want to do, post back with your specific use-case and maybe we can come up with something better.
1632603018
timmaugh
Forum Champion
API Scripter
Unless, of course, the roll is in the command line to YOUR macro... in which case you have to parse the inlineRoll property of the message object. libInline is a library script that can handle roll parsing for you, if you need to go this route.
timmaugh said: Everything going through the chat will create a chat event, even messages not intended for the API. A script can listen and take action based on a message that wasn't intended for the API, but it cannot stop the original message from hitting the chat. So you could write a script that would listen to every message and track the roll information and that wouldn't interfere with how the message progressed through the chat. The question would be what to do with a message that had multiple rolls, or nested rolls. Which one would you want to track? Off the shelf, you might be able to do something like this with Custom Roll Listener. If that doesn't let you do what you want to do, post back with your specific use-case and maybe we can come up with something better. My use case is this: player picks roll (plain ones, like 1-10d4-10) script gets result and sets an attribute to that number. I have the set the attribute to number, but I don't know how to take that number from a roll
Peacekeeper B said: As far as I know you can't unless you are using it in an api within the same macro, for example powercards or scriptcards. Or even alterbars. You could possibly store it on a character sheet using chatsetattr.  !power {{ --roll|[[ [$roll]1d20]] --api_setatr|_name @{selected|token_name} _LastRoll|$roll }} Not sure if _LastRoll|$roll is the right format.  Might work better if you had a generic character sheet to store it on though. I see what you mean! But I don't think it should be that complicated for my use case
1632632814
Andrew R.
Pro
Sheet Author
It sounds like something akin to the 13th Age Glorantha Berserker Die, where a Berserker has their own Escalation Die that they roll instead of using the Escalation Die controlled by the GM.  I handle that in my 13th Age campaign using the Turn Tracker, so each Berserker has their own So-And-So Die  entry in the Turn Tracker along with the Initiative entries and the Escalation Die / Chaos Die / Lunar Die entries controlled by the GM. 
Welcome to roll20. Where things get complicated for no reason.  Bonjwa Dillon said: Peacekeeper B said: As far as I know you can't unless you are using it in an api within the same macro, for example powercards or scriptcards. Or even alterbars. You could possibly store it on a character sheet using chatsetattr.  !power {{ --roll|[[ [$roll]1d20]] --api_setatr|_name @{selected|token_name} _LastRoll|$roll }} Not sure if _LastRoll|$roll is the right format.  Might work better if you had a generic character sheet to store it on though. I see what you mean! But I don't think it should be that complicated for my use case
1632662177
timmaugh
Forum Champion
API Scripter
I'm going back and reading what you've written, and it sounds like you're writing your own script. If so, you have a couple of hurdles: 1) listen to all chat messages, but differentiate which ones contain the rolls you care about 2) take action only on the messages that matter, and only on the rolls that you need within that message 3) determine to which attribute you wish to apply any given roll (if the message contains more than one) 4) get the roll value 5) apply it to the attribute To answer your specific question of getting the value from an inline roll (in a javascript context, anyway -- hopefully that's what you need)... you'll want one of 2 approaches. If all you need is to extract the value, you can use this snippet: 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(); I broke this down and explained it in this post , if you wanted a little more information about how it works... but basically it looks for any roll marker ($[[0]]) in the command line and replaces it with the value of the roll. This changes the command line. Again, if you are listening to all messages (including non-API), then the message you are working on will have already hit the chat output. However, if it is an API message, then you might want to take measures -- like not acting on a message intended for another script, or not rewriting the command line of the message object but just writing the new line into a variable of your own. The other option you can use is to hook into libInline , which will not only give you the value of a roll, but also the breakdown of dice, or tables, or even the HTML of the roll tip for a chat output. It just depends on how much you need to do with the rolls once you have them.
timmaugh said: I'm going back and reading what you've written, and it sounds like you're writing your own script. If so, you have a couple of hurdles: 1) listen to all chat messages, but differentiate which ones contain the rolls you care about 2) take action only on the messages that matter, and only on the rolls that you need within that message 3) determine to which attribute you wish to apply any given roll (if the message contains more than one) 4) get the roll value 5) apply it to the attribute To answer your specific question of getting the value from an inline roll (in a javascript context, anyway -- hopefully that's what you need)... you'll want one of 2 approaches. If all you need is to extract the value, you can use this snippet: 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(); I broke this down and explained it in this post , if you wanted a little more information about how it works... but basically it looks for any roll marker ($[[0]]) in the command line and replaces it with the value of the roll. This changes the command line. Again, if you are listening to all messages (including non-API), then the message you are working on will have already hit the chat output. However, if it is an API message, then you might want to take measures -- like not acting on a message intended for another script, or not rewriting the command line of the message object but just writing the new line into a variable of your own. The other option you can use is to hook into libInline , which will not only give you the value of a roll, but also the breakdown of dice, or tables, or even the HTML of the roll tip for a chat output. It just depends on how much you need to do with the rolls once you have them. I've tried this, it works but it bugs out sometimes and picks the wrong roll. I've decided to cut to the chase and have my own dice rolling directly inside the script, it takes number of dices, number of faces and some math.random(s) later I have my result. I know math.random isn't really random, but I've asked a professor in uni (I'm studying computer science) and he says it's good enough since I also randomize the use of ceil and floor to approximate the random number, thanks for all the effort though, not only from timmaugh but from everyone else! Loving this Roll20 community
Peacekeeper B said: Welcome to roll20. Where things get complicated for no reason.  Bonjwa Dillon said: Peacekeeper B said: As far as I know you can't unless you are using it in an api within the same macro, for example powercards or scriptcards. Or even alterbars. You could possibly store it on a character sheet using chatsetattr.  !power {{ --roll|[[ [$roll]1d20]] --api_setatr|_name @{selected|token_name} _LastRoll|$roll }} Not sure if _LastRoll|$roll is the right format.  Might work better if you had a generic character sheet to store it on though. I see what you mean! But I don't think it should be that complicated for my use case I've noticed that ahah