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

Consuming "chat:message"

1376672229

Edited 1376746396
Lithl
Pro
Sheet Author
API Scripter
Is there any way to consume a message event ( on("chat:message", function(msg) {...}); )? Or alternatively, is there a way to remove specific messages from the chat history? What I'm trying to do is expand the functionality of the dice roller with a repeat-dice ability. Specifically, I want to be able to make macros/abilities for AoE powers in D&D4, which roll to-hit separately but a single damage roll for all targets hit. I'm working with inline rolls at the moment, because that's what my friends and I have been using most extensively. The repetition works, but the chat shows the original message text (along with extraneous die roll[s]) in addition to the final product. The one option I see at the moment is simply using API messages rather than general messages, but then if I use queries to make the number of rolls variable (the primary purpose of the exercise!), the line[s] with the query is output last. The code I've been using, which doesn't consume anything: on("chat:message", function(msg) {     var repeatInlineRolls = msg.content.match(/\$\[\[\d+?[^\[\]]*?\]\]\{\d+?\}/gm);     if(repeatInlineRolls != null)     {         var finalMsg = msg.content;         for(var i = 0; i < repeatInlineRolls.length; i++)         {             var repeat = repeatInlineRolls[i].substring(repeatInlineRolls[i].lastIndexOf('{')+1,                         repeatInlineRolls[i].lastIndexOf('}'));             var roll = msg.inlinerolls[i].expression;             var fullRoll = '';             for(var r = 0; r < repeat; r++)             {                 fullRoll += '[[' + roll + ']], ';             }             fullRoll = fullRoll.substring(0, fullRoll.lastIndexOf(','));             finalMsg = finalMsg.replace(repeatInlineRolls[i], fullRoll);         }                 sendChat(msg.who, finalMsg);     } }); The regex finds text in the format "$[[0]]{0}"; by the time the code flow reaches this point, inline rolls have been indexed and appear as "$[[0]]" in the message content. My repeating syntax simply adds "{0}" directly after the inline roll. There are two main problems. One, the original message isn't consumed. If a player enters: Rolling d20+8 three times: [[d20+8]]{3} The result is something to the effect of: Rolling d20 three times: [20]{3} Rolling d20 three times: [9], [25], [17] The second problem comes from multiple lines and queries. With a macro of: /me does a thing! To lots of dudes! [[d20+8]]{?{Targets}} vs. AC Hit: [[d8+4]] fire damage I get a result of: Brian does a thing! To lots of dudes! [17]{3} vs. AC Hit: [10] fire damage [28], [14], [14] vs. AC I can alter the API code to work with API calls (and strip the ! off the front before posting the sendChat ), which does prevent both problems. However, the macro becomes extremely ugly, and it doesn't seem like a good idea to be sending API calls that aren't for anything except consuming the chat: !/me does a thing! To lots of dudes! ![[d20+8]]{?{Targets}} vs. AC ! !Hit: [[d8+4]] fire damage Side note: sendChat(msg.who, ...) doesn't seem to post the message entirely correctly. The name is correct, but the background is grey instead of blue (as though the message was sent by a different player, rather than myself), and my gravatar does not appear in the chat. I thought it was simply an issue of posting as a character named "Brian (GM)" vs. posting as myself , but even when I hard-coded the speakingAs variable to my name and player ID it didn't seem to work (in fact, the name of the posting character was "Brian (GM)|-Abc123" etc. -- I tried a few variations).
1376746783

Edited 1376747794
Lithl
Pro
Sheet Author
API Scripter
Brian said: Side note: sendChat(msg.who, ...) doesn't seem to post the message entirely correctly. The name is correct, but the background is grey instead of blue (as though the message was sent by a different player, rather than myself), and my gravatar does not appear in the chat. I thought it was simply an issue of posting as a character named "Brian (GM)" vs. posting as myself , but even when I hard-coded the speakingAs variable to my name and player ID it didn't seem to work (in fact, the name of the posting character was "Brian (GM)|-Abc123" etc. -- I tried a few variations). Well, I've discovered the (partial) solution to this problem, at least! I misunderstood the documentation on the sendChat function. Where it said 'player|' and 'character|', I assumed it meant to substitute the words with the names. Instead, now I have this: var characters = findObjs({_type: 'character'}); var speaking; characters.forEach(function(chr) { if(chr.get('name') == msg.who) speaking = chr; }); if(speaking) sendChat('character|'+speaking.id, finalMsg); else sendChat('player|'+msg.playerid, finalMsg); With this, if I'm speaking as myself, everything posts correctly. The background is still grey for me rather than blue, but other players are going to see gray background anyway, so it's not a big deal. Speaking as a character, the avatar does not appear, unfortunately! I still haven't been able to find an answer to this question, and I assume the only solution for now is using API commands. I'm going to finish up the script and post it; I'll edit a link to the script thread here once I'm done. Edit: link!