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 .
×

Best practice for multi-line api message handling?

I have a question for those of you that are pretty comfortable with the api in general, which is simply the title. I had thought that api-type messages from the 'chat:message' events were delimited to individual calls for each '![function-call]' received from the chat, until I had to process a macro that sent two api commands: !token-mod --set statusmarkers|=dead !xp @{selected|xp} Which in turn threw my basic chat handling listener a functional error: //expects a single api call handleCall = function(msg){     log(msg.content); //returns the above commands delimited by a carriage return     if(msg.type == "api" && msg.content.indexOf("!xp " !== -1)){         let xpAdd = parseInt(msg.content.split(' ')[1]);         if(xpAdd != "NaN"){             xp(xpAdd);         }     } } I'm working on refactoring the handler instead of trying to fix or add a couple lines, so In general, what best practices do you all use to safely handle and parse chat commands?
1559790043

Edited 1559790980
Scott C.
Forum Champion
Sheet Author
API Scripter
Compendium Curator
chat messages are single lines, unless you bracket things in {{ }}. Unless that's been changed recently. They are handled in sequence though, so both would be triggered. If you are getting chat messages as multiline, then that's a bug, and should be reported as such. EDIT: Just tested, and it does still pass as single lines. How/where is your macro saved?
1559790695

Edited 1559797762
The Aaron
Roll20 Production Team
API Scripter
A newline in the chat box always creates a separate chat:message event, with the exception of newlines enclosed within {{ and }}. Here's what the API sees for this two line command: !foo !bar Without seeing the error, it's hard to say what the issue was for certain (what functional error did you have?), but the first thing I would change is your check for !xp.  What you have now is "Does `!xp` occur anywhere in the msg.content".  What you really want is "Does msg.content begin with !xp".  At a minimum, you'd want (offset is 0, beginning of string): msg.content.indexOf("!xp " === 0) My preference is (regex, case insensitive): /^!xp/i.test(msg.content) More info plz!
1559793290

Edited 1559794045
Thank you two for your inputs, it gave me a much better understanding on the chat event functionality in general, your regex search that replaced the indexOf search actually fixed the whole thing like a missing puzzle piece, Aaron, so much thanks for that! The error I tried to document was a result of the xp function firing multiple times, since it was intended to only be called once. I tested until I could find a cause for this functionality, and eventually came to my own conclusion that if any chat message contained '!xp ', it would call the xp function for every command and pass whatever was delimited by a space. Xp function in question: xp = function(amount){ log(xpAttributes); //Supposed to log/return only one array, logged/fired twice with one of the arrays having null values for all the 'current' properties xpAttributes.forEach(function(x, i){ x.set({ current: parseInt(x.get('current')) + Math.round(amount/xpAttributes.length) }); checkLevelUp(characters[i]); }); } As mentioned in the initial post, my 'Death' macro that I use for enemy tokens was the two commands: !token-mod --set statusmarkers|=dead !xp @{selected|xp} So I was completely puzzled when I saw that my xp function was firing twice from one call, and the only cause I could think of that would make it behave like that is the two api calls being bundled together into one message, which is where most of my functional misunderstanding of the situation stemmed from. The whole root cause of the problem though was merely the way I checked what the message started with; I'll now be making good use of regex expressions in the future for situations like this, thanks again you two!
1559797838
The Aaron
Roll20 Production Team
API Scripter
No worries, glad that sorted it out.