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

[API] Re-sending Formatted Result From Chat Handler Function

March 26 (11 years ago)
I'm not sure how to effectively search for other threads with this, so I'll just ask.

I want to make a roll from inside an API script (this is working). I want to do something interesting with the total (also working). I also want to re-chat the pretty, formatted, colourful, hoverable object that would have been the result, if I hadn't co-opted the handler function.

Simple sample:

on("ready", function(msg)
{
sendChat("Test", "1d4: [[1d4]]", function(msg)
{
log(msg[0]);
var roll = msg[0].inlinerolls["1"];
var total = roll.results.total;
doSomethingInteresting(total);
// But also log the nicely-displayed results...
sendChat("Test", "1. " + msg[0]);
sendChat("Test", "2. " + msg[0].content);
sendChat("Test", "3. " + msg[0].inlinerolls);
sendChat("Test", "4. " + msg[0].inlinerolls["1"]);
sendChat("Test", "5. " + roll);
sendChat("Test", "6. " + roll.results);
});
});
function doSomethingInteresting(t)
{
sendChat("Test", "Something interesting! " + t);
}

Most of them output: [object Object]
2 waits a bit and then outputs: 1d4: $[_1_]

...where [_1_] is a formatted result like I want, except that a) it always shows a 1 when the actual result may have been something else, and b) hovering over it always says "Rolling 1 = 1".

Any advice?

Thanks!
March 26 (11 years ago)
Gid
Roll20 Team
Moving this to the API forum.
March 26 (11 years ago)
Thanks. I wasn't sure whether this forum was just for working scripts.

If it's not much trouble, could a mod also change my title to "Need Help Re-sending ...." as that part is now unclear, but the API part obvious, due to the forum change. Thanks!
March 26 (11 years ago)
Never mind, I found it. Whew! I thought I was missing something easy and obvious. Thanks to HoneyBadger's thread on Power Cards and Brian's Critical Hit script, I was able to manage it. I will try to remember to link my finished script here when it's done, because I've stripped out some (some) of the complexity of Brian's script for anyone who wants only the default functionality and not, for example, to redefine what counts as a critical. Anyway, his script is here and it's the buildinline function that does it.

It's awesome that there's such a fantastic, sharing, and industrious community around this place.
March 27 (11 years ago)

Edited March 27 (11 years ago)
So, I ran into trouble with Brian's code when I went to put it into my original, much more complicated script. Probably because the message type was G. Anyway, I played around a little more and found what is, for my purposes, an easier way. Rather than making a direct handler, I just let the message go through as originally formatted and then catch it later:

on("ready", function(msg)
{
sendChat("Test", "1d4: [[1d4]]");
});
on("chat:message", function (msg)
{
if (msg.who == "Test")
{
var roll = JSON.stringify(msg.inlinerolls);
var pos = roll.lastIndexOf("total");
pos = roll.indexOf(":", pos);
var pos2 = roll.indexOf(",", pos);
var total = roll.substring(pos + 1, pos2);
doSomethingInteresting(total);
}
});
function doSomethingInteresting(t)
{
sendChat("Test-Result", "Something interesting! " + t);
}


I ran into some oddness where msg.inlinerolls existed, but the information I needed was under the index of "null" instead of the usual 1. So I just parsed it as a string here. I'll probably have to find a more robust way when I move this, too, into my other script.

Note: One way or another, you have to be careful that any chat messages you send out afterward (eg. in doSomethingInteresting) aren't caught by your handler function. I did it here just by using a different msg.who, but there might be better ways.