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

Trouble calling character ability from script if ability is whispered

1595801223
David M.
Pro
API Scripter
I have a script that fires an ability from a given character sheet if certain conditions are met. It works fine unless the ability is whispered, in which case nothing happens. My first thought was that it probably has something to do with the forward slash (/w), so was messing around with escape characters, html replacements, template literals, standing on one leg, etc. but not having any luck. I set up a generic case for testing. Character=CharName with ability=Ability 1. With and without the whisper, Ability1 is defined as: &{template:default} {{label=some text}} /w CharName &{template:default} {{label=some text}} Simplified code I'm using is: on('ready',()=>{ on('chat:message',msg=>{ if(msg.type=="api" && msg.content.indexOf("!AbilityTest")==0) { var who = getObj('player',msg.playerid).get('_displayname');     //hardcoded for testing var charName = 'CharName'; var abilityName = 'Ability1';     //get character obj from Name in order to get charID var charObj = findObjs({_type: 'character',name: charName})[0];; var charID = charObj.get("id");     //Get the ability object from character sheet & ability name var abilityObj = findObjs({_type: 'ability',_characterid: charID,name: abilityName})[0];     //Get the text of the ability and send to chat var action = abilityObj.get("action");     log('action = ' + action); sendChat(who, action); } }); }); Works fine for non-whispered Ability1 but does nothing when changed to the whispered version. I confirmed that the ability's action property contains the string "/w CharName". It's just not sending to chat properly. Seems like there would be a simple way to handle this, but I haven't been able to figure it out. Thoughts?
1595854198
timmaugh
Forum Champion
API Scripter
Hmm... if the '/w' isn't the first thing sent, you won't get the whisper effect, so I wonder if something about the roll template is wrapping the output in a way that is masking the /w. Try this. It might not be the way you want to handle it in the end, but it should provide a test, anyway. Replace your sendChat() line with these 2 lines: let a = /^(\/w\s[^&]*)&{template:default} /.exec(action); sendChat(who, a ? a[1] + action : action); Let us know what you find.
1595877109

Edited 1597062219
David M.
Pro
API Scripter
Good thought, but same result. I also tried completely hardcoding the sendChat with the literal text of the ability as shown with no luck sendChat(who, '/w CharName &{template:default} {{label=some text}}'); Did some more testing and it seems that the whisper sendChat only fails if the ability is a whisper to the character name. If the ability has a whisper to the player , it works fine. Weird, since I verified that the test character I was using is in all player's journals and can be edited by all players. Also, when I run the ability directly from the character sheet with /w character name, it displays properly.  If I parse the action and strip out "/w CharName" from the action string, then prepend with a  "'/w "'+who+'"' + ...", it will work for both cases.  if (action.indexOf("/w")==0) { let actionArr = action.split(/\s+/); actionArr.shift(); actionArr.shift(); action = actionArr.join(); sendChat(who, '/w "'+who+'" ' + action); } else{ sendChat(who, action); } However, as I'm writing this I'm now realizing that since "who" is the displayname of the player, this potentially circumvents the intended target of the whisper, and only whispers back to the player who called the script. Irritating.