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 code help

1591274868

Edited 1591274911
hello, after lots of reading - errors ..and more errors i almost managed to make a script that i nead. but it has some problems. if anyone can help me ..please do. my code is: on("chat:message", function(msg) { var cmdName = "!magiccharges"; var msgTxt = msg.content; var cmdNamePortion = msgTxt.slice (0, cmdName.length); if (msg.type !== 'api') return; if (cmdNamePortion !== cmdName) return; sendChat(msg.who, "/w gm !!!"); var selectedTokenID = msg.selected[0]._id var getCharacterAttribute = function (tokenID) {     var token = findObjs({type: 'graphic', _id: tokenID })[0];     var character = findObjs({type: 'character', _id: token.get('represents')})[0];     var attribute = findObjs({ type: 'attribute', characterid: character.id, name: 'Will'   })[0];     var ChargeItem_BoomStick = findObjs({ type: 'attribute', characterid: character.id, name: 'ChargeItem_BoomStick'   })[0];     var Current_ChargeItem_BoomStick = ChargeItem_BoomStick.get('current');     var Renas = findObjs({ type: 'attribute', characterid: character.id, name: 'Renas'   })[0];     var Current_Renas = Renas.get('current');     var Spendedenas = findObjs({ type: 'attribute', characterid: character.id, name: 'Spendedenas'   })[0];     var Current_Spendedenas = Spendedenas.get('current');     var HP = findObjs({ type: 'attribute', characterid: character.id, name: 'hp_total'   })[0];     var Current_HP = HP.get('current');          if(Current_ChargeItem_BoomStick>1)         {             Current_ChargeItem_BoomStick--;              }     else         {             if(Current_Renas>10)             {                 Current_Renas=Current_Renas - 10;                 Current_Spendedenas = Current_Spendedenas - 10 + 20;             }             else             {                 Current_HP=Current_HP-10;             }         }     ChargeItem_BoomStick.set('current', Current_ChargeItem_BoomStick);     Renas.set('current', Current_Renas);     Spendedenas.set('current', Current_Spendedenas);     HP.set('current', Current_HP);   } log (getCharacterAttribute(selectedTokenID))                                   }); if i enter in the chat: !magiccharges it will work. if i use it alone in a macro. it will work. if i use it in combination with other code in a macro it will fail and crush the api console with error " TypeError: Cannot read property '0' of undefined" also. how can i add an argument in the command. i need to in the current code the argument "ChargeItem_BoomStick" is build in. but i need to have different  ones. please some help. thank you!
1591278593
The Aaron
Roll20 Production Team
API Scripter
I suspect the issue is you don't have a token selected when you're running it from the macro.  This is the only line I see that would give that specific error: var selectedTokenID = msg.selected[0]._id When no tokens are selected, msg.selected will be undefined, so referencing the 0 index on it (the 0 property) will be an error. As for passing arguments, everything you type into chat will go to the API: !magiccharges BroomStick At which point you'd just need to match and parse the msg.content a little differently.
1591278741
GiGs
Pro
Sheet Author
API Scripter
What do you mean by this? if i use it in combination with other code in a macro it will fail Can you give an example?
Thanks for replying. I had selected a token. Also in a macro I added first the ! magiccharges and then a ! power cards. If I had only the power card's it works. If only the !magiccharges. It works. Together they fail.
1591280555
GiGs
Pro
Sheet Author
API Scripter
It would help to see the exact commands, exactly as you sent them. With code help, we cant go off guesswork.
I'll send them layer. I'm on my phone now as we have a landline problem...
1591281192
The Aaron
Roll20 Production Team
API Scripter
If you had any @{target|...} references in your macro, the API would not receive selected tokens even if you had them selected at the time you issued the command.  It's a known bug I've been trying to get fixed for years...
thank you! the api code is: //----------------------------------------------------------------------- on("chat:message", function(msg) { var cmdName = "!magiccharges"; var msgTxt = msg.content; var cmdNamePortion = msgTxt.slice (0, cmdName.length); if (msg.type !== 'api') return; if (cmdNamePortion !== cmdName) return; sendChat(msg.who, "/w gm !!!"); var selectedTokenID = msg.selected[0]._id var getCharacterAttribute = function (tokenID) {     var token = findObjs({type: 'graphic', _id: tokenID })[0];     var character = findObjs({type: 'character', _id: token.get('represents')})[0];     var attribute = findObjs({ type: 'attribute', characterid: character.id, name: 'Will'   })[0];     var ChargeItem_BoomStick = findObjs({ type: 'attribute', characterid: character.id, name: 'ChargeItem_BoomStick'   })[0];     var Current_ChargeItem_BoomStick = ChargeItem_BoomStick.get('current');     var Renas = findObjs({ type: 'attribute', characterid: character.id, name: 'Renas'   })[0];     var Current_Renas = Renas.get('current');     var Spendedenas = findObjs({ type: 'attribute', characterid: character.id, name: 'Spendedenas'   })[0];     var Current_Spendedenas = Spendedenas.get('current');     var HP = findObjs({ type: 'attribute', characterid: character.id, name: 'hp_total'   })[0];     var Current_HP = HP.get('current');          if(Current_ChargeItem_BoomStick>1)         {             Current_ChargeItem_BoomStick--;              }     else         {             if(Current_Renas>10)             {                 Current_Renas=Current_Renas - 10;                 Current_Spendedenas = Current_Spendedenas - 10 + 20;             }             else             {                 Current_HP=Current_HP-10;             }         }     ChargeItem_BoomStick.set('current', Current_ChargeItem_BoomStick);     Renas.set('current', Current_Renas);     Spendedenas.set('current', Current_Spendedenas);     HP.set('current', Current_HP);   } log (getCharacterAttribute(selectedTokenID))    }); //----------------------------------------------------------------------- and the macro code is: !magiccharges !power {{  --bgcolor| #914b31  --corners|10  --name| @{selected|character_name} attacked with his "Boom Stick"   --leftsub|Range --rightsub|Piercing / Force / Thunder  --Target| @{target||token_name}  --Attack|[[ [$Atk] ?{Advantage? | Normal, 1d20 + @{selected|Weapon_rank_57} +@{selected|dexterity_total_bonus} - @{selected|Wounds}[wounds]  | Advantage, 2d20kh1 + @{selected|Weapon_rank_57} +@{selected|dexterity_total_bonus} - @{selected|Wounds}[wounds]  | Disadvantage, 2d20kl1 + @{selected|Weapon_rank_57} +@{selected|dexterity_total_bonus} - @{selected|Wounds}[wounds] } ]] vs **AC** [[@{target||AC}]]  --?? $Atk < @{target||AC} ?? !Missed|**You missed!**   --?? $Atk.base <> 1 AND $Atk >= @{target||AC} AND $Atk.base <> 20 ?? Hit - Piercing Damage|  [[ [$Dmg]  (1d6)*(0*@{target||Acid} + 0*@{target||Force} + 0*@{target||Lightning} + 0*@{target||Necrotic} + 0*@{target||Poison} + 0*@{target||Psychic} + 0*@{target||Radiant} + 0*@{target||Thunder} + 0*@{target||Cold} + 0*@{target||Fire} + 1*@{target||Piercing} + 0*@{target||Bludgeoning} + 0*@{target||Slashing} )]]  --?? $Atk.base <> 1 AND $Atk >= @{target||AC} AND $Atk.base <> 20 ?? alterbar1|_target|@{target||token_id} _bar|1 _amount|-[^Dmg] _show|all   --?? $Atk.base <> 1 AND $Atk >= @{target||AC} AND $Atk.base <> 20 ?? Hit - Force Damage|  [[ [$Dmg2]  (1d4)*(0*@{target||Acid} + 1*@{target||Force} + 0*@{target||Lightning} + 0*@{target||Necrotic} + 0*@{target||Poison} + 0*@{target||Psychic} + 0*@{target||Radiant} + 0*@{target||Thunder} + 0*@{target||Cold} + 0*@{target||Fire} + 0*@{target||Piercing} + 0*@{target||Bludgeoning} + 0*@{target||Slashing} )]]  --?? $Atk.base <> 1 AND $Atk >= @{target||AC} AND $Atk.base <> 20 ?? alterbar2|_target|@{target||token_id} _bar|1 _amount|-[^Dmg2] _show|all   --?? $Atk.base <> 1 AND $Atk >= @{target||AC} AND $Atk.base <> 20 ?? Hit - Thunder Damage|  [[ [$Dmg3]  (1d2)*(0*@{target||Acid} + 0*@{target||Force} + 0*@{target||Lightning} + 0*@{target||Necrotic} + 0*@{target||Poison} + 0*@{target||Psychic} + 0*@{target||Radiant} + 1*@{target||Thunder} + 0*@{target||Cold} + 0*@{target||Fire} + 0*@{target||Piercing} + 0*@{target||Bludgeoning} + 0*@{target||Slashing} )]]  --?? $Atk.base <> 1 AND $Atk >= @{target||AC} AND $Atk.base <> 20 ?? alterbar3|_target|@{target||token_id} _bar|1 _amount|-[^Dmg3] _show|all   --?? $Atk.base <> 1 AND $Atk.base == 20 ?? Critical - Piercing Damage| [[ [[ [$Crit]  (2d6)*(0*@{target||Acid} + 0*@{target||Force} + 0*@{target||Lightning} + 0*@{target||Necrotic} + 0*@{target||Poison} + 0*@{target||Psychic} + 0*@{target||Radiant} + 0*@{target||Thunder} + 0*@{target||Cold} + 0*@{target||Fire} + 1*@{target||Piercing} + 0*@{target||Bludgeoning} + 0*@{target||Slashing} )]]  --?? $Atk.base <> 1 AND $Atk.base == 20 ?? alterbar4|_target|@{target||token_id} _bar|1 _amount|-[^Crit] _show|all   --?? $Atk.base <> 1 AND $Atk.base == 20 ?? Critical - Force Damage| [[ [[ [$Crit2]  (2d4)*(0*@{target||Acid} + 1*@{target||Force} + 0*@{target||Lightning} + 0*@{target||Necrotic} + 0*@{target||Poison} + 0*@{target||Psychic} + 0*@{target||Radiant} + 0*@{target||Thunder} + 0*@{target||Cold} + 0*@{target||Fire} + 0*@{target||Piercing} + 0*@{target||Bludgeoning} + 0*@{target||Slashing} )]]  --?? $Atk.base <> 1 AND $Atk.base == 20 ?? alterbar5|_target|@{target||token_id} _bar|1 _amount|-[^Crit2] _show|all   --?? $Atk.base <> 1 AND $Atk.base == 20 ?? Critical - Thunder Damage| [[ [[ [$Crit3]  (2d2)*(0*@{target||Acid} + 0*@{target||Force} + 0*@{target||Lightning} + 0*@{target||Necrotic} + 0*@{target||Poison} + 0*@{target||Psychic} + 0*@{target||Radiant} + 1*@{target||Thunder} + 0*@{target||Cold} + 0*@{target||Fire} + 0*@{target||Piercing} + 0*@{target||Bludgeoning} + 0*@{target||Slashing} )]]  --?? $Atk.base <> 1 AND $Atk.base == 20 ?? alterbar6|_target|@{target||token_id} _bar|1 _amount|-[^Crit3] _show|all   --?? $Atk.base == 1 ?? !|**Fumble!**  }}  the !power macro alone works. the !magiccharges alone works. together i get the error: " Your scripts are currently disabled due to an error that was detected. Please make appropriate changes to your scripts and click the "Save Script" button and we'll attempt to start running them again.  More info... For reference, the error message generated was:  TypeError: Cannot read property '0' of undefined TypeError: Cannot read property '0' of undefined at apiscript.js:7207:35 at eval (eval at <anonymous> (/home/node/d20-api-server/api.js:154:1), <anonymous>:65:16) at Object.publish (eval at <anonymous> (/home/node/d20-api-server/api.js:154:1), <anonymous>:70:8) at /home/node/d20-api-server/api.js:1648:12 at /home/node/d20-api-server/node_modules/firebase/lib/firebase-node.js:93:560 at hc (/home/node/d20-api-server/node_modules/firebase/lib/firebase-node.js:39:147) at Kd (/home/node/d20-api-server/node_modules/firebase/lib/firebase-node.js:93:546) at Id.Mb (/home/node/d20-api-server/node_modules/firebase/lib/firebase-node.js:93:489) at Zd.Ld.Mb (/home/node/d20-api-server/node_modules/firebase/lib/firebase-node.js:94:425) at /home/node/d20-api-server/node_modules/firebase/lib/firebase-node.js:111:400 " thank you for your help!  
@  The Aaron if this is the case any suggestion how to bypass this problem. i am trying to learn this javascript and it is very difficult for me  although my background is from assembly / microprocessors to c++. i feel lost with java...
1591284792
The Aaron
Roll20 Production Team
API Scripter
If you're looking at Java, I can understand how you'd feel lost.  This is Java script .  It's completely unrelated to Java. The only way to bypass this issue is to pass the selected ID into your script on the command line, something akin to: !magiccharges BroomStick --ids @{selected|token_id} You'll need to parse it out of the command line in your script, then use the id to lookup whatever you're after.
Thank you for your time 😊.  Can you give me some more help with this. Plz... I don't know how to parse it...
1591286931
The Aaron
Roll20 Production Team
API Scripter
I can give you more help this evening, for now the best I can offer is an example: on('ready',()=>{ on('chat:message',msg=>{ if('api'===msg.type && /^!cmd(\b\s|$)/i.test(msg.content)){ let args = msg.content.split(/\s+/); let args2 = msg.content.split(/\s+--/).map(p=>p.split(/\s+/)); let argsTxt = `<div><div>Args split on whitespace:</div><ul><li>${args.join('</li><li>')}</li></ul></div>`; let args2Txt = `<div><div>Args split on -- and then whitespace:</div><ul><li>${args2.map(a=>`<ul><li>${a.join('</li><li>')}</li></ul>`).join('</li><li>')}</li></ul></div>`; sendChat('Arg Splitting',`${argsTxt}${args2Txt}`); } }); }); install that script, then run !cmd with whatever args you like, it will show you how they get parsed in the two most common ways I use (split on whitespace, and split on -- and then whitespace).  here are some examples to start you off: !cmd foo bar baz !cmd --foo bar --baz
You are a god. Thank you. My player's will hate you for the knowledge that you gave me. For the horrors that I'll anlish to them... 
1591289320
The Aaron
Roll20 Production Team
API Scripter
HAHAHAHAHA!
tested. it wont return me anything ... i added and a log() in your script to see if it will fire but no it does not fire... i use the  !cmd foo bar baz or the other but nothing...
1591293191
The Aaron
Roll20 Production Team
API Scripter
If you copy/pasted it, try just typing it.  The forum has a nasty habit of injecting invisible text that prevents copied commands from being correctly interpreted.
nop... typed it. tried again.. wont fire at all...
1591293563
The Aaron
Roll20 Production Team
API Scripter
If you want to PM me an invite to your game and GM me, I'll come check it out.
hello,   i changed your script to: on('chat:message', function(msg) { if('api'===msg.type && /^!test2(\b\s|$)/i.test(msg.content)){ let args = msg.content.split(/\s+/); let args2 = msg.content.split(/\s+--/).map(p=>p.split(/\s+/)); let argsTxt = `<div><div>Args split on whitespace:</div><ul><li>${args.join('</li><li>')}</li></ul></div>`; let args2Txt = `<div><div>Args split on -- and then whitespace:</div><ul><li>${args2.map(a=>`<ul><li>${a.join('</li><li>')}</li></ul>`).join('</li><li>')}</li></ul></div>`; sendChat('Arg Splitting',`${argsTxt}${args2Txt}`); } }); and it worked. ! thanks ill try now to "Decipher Script" it!
1591297911
The Aaron
Roll20 Production Team
API Scripter
Did you paste it as it's own script, or paste it inside an existing on('ready') block?  The latter would result in it never working.
on its own it did not work. then i edit it like above and it worked. no i am trying to understand how to use the parts (split arguments) how to get them to use them. thanks again for the help.
1591299216
The Aaron
Roll20 Production Team
API Scripter
np
:) i finally made it !!! thanks Lord Aaron! (needs lots of error proofing but it works )
1591309710
The Aaron
Roll20 Production Team
API Scripter
Yeah!!!  =D