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

[Help] Get a set of arguments from a !apichat command...

on("chat:message", function(msg) {   if(msg.type == "api" && msg.content.indexOf("!mark ") !== -1) {     sendChat(msg.who, "/me marks a target.");   } }); Here's what I have so far. I would like to give players the ability to mark targets themselves with the dots by typing: !mark blue DisplayName So, I need a way to get the blue and the displayname into an argument to use inside the if statement. I'll eventually change it to a case statement to add more commands, but I just want to get this done to have something to learn from.
1367403807
Alex L.
Pro
Sheet Author
HoneyBadger said: on("chat:message", function(msg) {   if(msg.type == "api" && msg.content.indexOf("!mark ") !== -1) {     sendChat(msg.who, "/me marks a target.");   } }); Here's what I have so far. I would like to give players the ability to mark targets themselves with the dots by typing: !mark blue DisplayName So, I need a way to get the blue and the displayname into an argument to use inside the if statement. I'll eventually change it to a case statement to add more commands, but I just want to get this done to have something to learn from. Look up the function split, that would be a good start.
on("chat:message", function(msg) {   if(msg.type == "api" && msg.content.indexOf("!mark ") !== -1) {     var n = msg.content.split(" ", 3)     var Color = n[1].toLowerCase();     var Target = n[2];     var TargetToken = findObjs({_type: "graphic", name: Target})[0];     sendChat(msg.who, "/w GM " + msg.who + " has marked " + Target + " with a " + Color + " dot.");     TargetToken.set("status_" + Color + "marker", true);   } }); Woot! It works. :D
1367410694
Alex L.
Pro
Sheet Author
Congrats
on("chat:message", function(msg) { // Adds a dot to the target   if(msg.type == "api" && msg.content.indexOf("!mark ") !== -1) {     var n = msg.content.split(" ", 3)     var Color = n[1].toLowerCase();     var Target = n[2];     var TargetToken = findObjs({_type: "graphic", name: Target})[0];     sendChat(msg.who, "/w GM " + msg.who + " has marked " + Target + " with a " + Color + " dot.");     TargetToken.set("status_" + Color + "marker", true);   } // Removes a dot from the target   if(msg.type == "api" && msg.content.indexOf("!rmark ") !== -1) {     var n = msg.content.split(" ", 3)     var Color = n[1].toLowerCase();     var Target = n[2];     var TargetToken = findObjs({_type: "graphic", name: Target})[0];     sendChat(msg.who, "/w GM " + msg.who + " has removed the " + Color + " dot from " + Target + ".");     TargetToken.set("status_" + Color + "marker", false);   } }); And updated with the ability to remove the dot. I should probably add a flag to turn on/off the message to the GM.
Hrm, weird. I can't mark player tokens this way. Just npc tokens.
1367411755
Alex L.
Pro
Sheet Author
Tested it and it works fine for me.
It doesn't appear to work if the token is attached to a journal entry and then dragged onto the map. Hrm, scratch that. I can't figure out why it's not working.
1367412369
Alex L.
Pro
Sheet Author
No again it works fine for me, remember you cant have spaces in the names if you are going to use split on space.
No, this is targeting tokens with single word names. I dunno. I'll keep poking at it.
Keep in mind that right now you're searching across all pages for the first token it finds by that name. So especially with PC tokens where they might be on multiple pages, you might just be marking the wrong token on the wrong page. You can change it to be: findObjs({_type: "graphic", name: Target, _pageid: Campaign().get("playerpageid")})[0]; And that will only find tokens on the currently active page, which is probably what you want.
Aha! Thanks.
I should have a library ready by this weekend that will handle command parsing. Right now it it takes a command and a set of required and optional arguments. It also will create an automated !help call.  The final api will look something like this  chat.command('mark').option('-t, --target <target>').option('-c, --color [color]').action(function(msg) {  //do your message here });  Your arguments would be on msg.args.target and msg.args.color (color being an optional variable in this example)
Jams said: I should have a library ready by this weekend that will handle command parsing. Right now it it takes a command and a set of required and optional arguments. It also will create an automated !help call.  The final api will look something like this  chat.command('mark').option('-t, --target <target>').option('-c, --color [color]').action(function(msg) {  //do your message here });  Your arguments would be on msg.args.target and msg.args.color (color being an optional variable in this example) Sounds like a cool idea! :-)
var TargetName = n[1]; findObjs({_type: "graphic", name: TargetName, _pageid: Campaign().get("playerpageid")})[0]; So I'm using these to find the target of a chat command, but in Roll20, BOB is not the same as bob, but I want them to be for purposes of using this api chat !command. Is there an efficient way to do this or would I have to create an array of all objects on the playerpageid, lower case the names, and then sort through the list for the right token, get the unique id that way and then make that the object?
1367572745
Alex L.
Pro
Sheet Author
You are correct you would have to get all graphics then iterate through them using toLowerCase()
Ugh. I think I'll just add something about the target names being case sensitive, lol.
1367587166
Alex L.
Pro
Sheet Author
Sounds wise.
I'll add an option in the next update so that findObjs() can do case-insensitive searches on strings.
Case-insensitive filtering is now live:&nbsp; <a href="https://wiki.roll20.net/API:Objects#Finding.2FFiltering_Objects" rel="nofollow">https://wiki.roll20.net/API:Objects#Finding.2FFiltering_Objects</a>
Cool, thanks! That'll save a lot of trouble for scripters.
Riley D. said: Case-insensitive filtering is now live:&nbsp; <a href="https://wiki.roll20.net/API:Objects#Finding.2FFiltering_Objects" rel="nofollow">https://wiki.roll20.net/API:Objects#Finding.2FFiltering_Objects</a> var TargetToken = findObjs({_type: "graphic", name: TargetName, _pageid: Campaign().get("playerpageid")}, {caseInsensitive: true})[0]; If I have a token named Grunt2 and type !mark grunt2 blue in my script, it is not finding Grunt2 even though caseInsensitive is set to true. Never mind... I had only put it into one spot and not both spots.