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

Calling one API script from another

December 24 (11 years ago)
I have a API script where I am trying to call one script from another depending on certain conditions, I tried sending the !command using sendChat but it just puts it in chat without it actually triggering is there a different command I should be using?
December 24 (11 years ago)

Edited December 24 (11 years ago)
Stephen S.
Pro
Marketplace Creator
Sheet Author
API Scripter
on("chat:message", function(msg) {  
	if (msg.type == "api" && msg.content.indexOf("!myapicommand") !== -1){
myFirstFuction();
};
});
myFirstFuction = function() {
var myRandomNumber = Math.floor(Math.random() * 100) + 1
if(myRandomNumber <= 50){
sendChat("API", "All you get is this.")
return //Return without doing anything else
}else{
sendChat("API", "All you get is this....and...")
mySecondFuction();
};
};
mySecondFuction = function() {
sendChat("API", "...you get this.");
};
December 24 (11 years ago)
Stephen S.
Pro
Marketplace Creator
Sheet Author
API Scripter
Maybe errors in that.... but hopefully you get the idea... you can split these into as many "script tabs" as you like as long as each event and function is all contained on one script tab.... those tabs only exist to aid building and debugging code (it functions as one really long script.)

I have an event tab... a declarations tab... a common tab (for small things) and then tabs for larger more stand alone stuff as needed.
December 24 (11 years ago)

Edited December 24 (11 years ago)
what if I have things in another script tab, can I send a command to call it like I would if I was typing it, like I saw you used sendChat("API" so I tried that with my message and it did not send it through, like I am currently in the !aoe script but I want to send a function to the !attack script I am trying to do it by
var attackmessage = "!attack " + targetid + " " + sourceid + "AC 1d12 @{tohit} @{damage} 12 0";
var testname = selected.get('name')
sendChat("API", attackmessage);

but in chat I just get
!attack -JAyYgq1cQj3C6llfbGk-JAzE-XiFoCzmwTXiy_c AC 1d12 tohit damage 12 0
so it is just typing it out in the chat window instead of the !attack api picking it up as it would if that command was written out in a macro, even if I copy and paste that line back into the chat window it will trigger the other api script.
December 24 (11 years ago)
Stephen S.
Pro
Marketplace Creator
Sheet Author
API Scripter
I use sendChat("API", "whatever is to be said") so everyone can see it came from an API script and not a player.

Be careful not to use sendChat with an "!" command... you can create an infinite loop.

Below would be BAD.

//DO NOT USE THIS

on("chat:message", function(msg) {  
if (msg.type == "api"){
myFirstFuction();
};
});
mySecondFuction = function() {
sendChat("API", "!anAPIcommand");
};
December 24 (11 years ago)
It doesn't work though it just types the command out in chat but it is not picked up by the other script as a chat event for whatever reason, it will just put !anAPIcommand in the chat window without triggering !anAPIcommand
December 24 (11 years ago)
Stephen S.
Pro
Marketplace Creator
Sheet Author
API Scripter
Shrug... perhaps Riley nerfed it (which is likely a good thing.)

Post your code and lets have a look.
December 24 (11 years ago)
as far as I know, you cannot trigger an API command with an output to chat from the api. what you need to do is call the applicable function directly within your script, not have it post the API command string to chat.
December 24 (11 years ago)

Edited December 24 (11 years ago)
What this is supposed to do is grab targets in an certain range/parameters then pass those targets onto a api scipt that I currently use for attacks called !attack, it grabs the targets correctly but just spams
!attack -JAzE17QA98xuD8azSA_ -JAzE-XiFoCzmwTXiy_c AC 1d12 tohit damage 12 0
!attack -JAyYgq1cQj3C6llfbGk -JAzE-XiFoCzmwTXiy_c AC 1d12 tohit damage 12 0
into the chat, I would like to send those commands to chat in a way that the !attack api will notice them as chat events and do its thing with the passed along info
https://gist.github.com/stephenhudson/8118636
December 24 (11 years ago)
Stephen S.
Pro
Marketplace Creator
Sheet Author
API Scripter

Michael H. said:

as far as I know, you cannot trigger an API command with an output to chat from the api. what you need to do is call the applicable function directly within your script, not have it post the API command string to chat.

Oh I know I have done it in that past... Riley must have nerfed it.
December 24 (11 years ago)
So I am using

on("chat:message", function(msg) {
if(msg.type == "api" && msg.content.toLowerCase().indexOf('!attack') !== -1) {

to call my !attack message, it looks from your sample code that I need to somehow declare that as it's own separate function then make a new script when they do !attack to send it back to the original function?
December 25 (11 years ago)

Edited December 25 (11 years ago)
Stephen S.
Pro
Marketplace Creator
Sheet Author
API Scripter
var roll20API = roll20API || {};


roll20API.apiCommands = [
{description: "Area Of Effect.", long: "aoe", short: "aoe", process: "processAOE"},
{description: "Attack.", long: "attack", short: "atk", process: "processATK"},
];
on("chat:message", function(msg) {
if(msg.type != "api"){return;};
//Return and do nothing if its not an API command.
if(msg.content.indexOf(' ') == -1){
roll20API.contentMessage = null;
roll20API.contentCommand = msg.content.toLowerCase().substr(1, msg.content.toLowerCase().length);
}else{
roll20API.contentMessage = msg.content.toLowerCase().substr(msg.content.toLowerCase().indexOf(' ') + 1);
roll20API.contentCommand = msg.content.toLowerCase().substr(1, msg.content.toLowerCase().indexOf(' ') - 1);
};
//get the API command as roll20API.contentCommand
//get any message with the API command as roll20API.contentMessage
var processToCall = undefined;
_.each(roll20API.apiCommands, function(indexCommands) {
if(indexCommands.long == roll20API.contentCommand || indexCommands.short == roll20API.contentCommand){
processToCall = indexCommands.process;
};
});
//Loop through all the valid commands to find see what was given is found
if(processToCall == undefined){return;};
var functionString = eval(processToCall);
functionString();
});
processAOE = function() {
sendChat("API", "processAOE triggered by \"!" + roll20API.contentCommand + "\" and using \"" + roll20API.contentMessage + "\"")
};
processATK = function() {
sendChat("API", "processATK triggered by \"!" + roll20API.contentCommand + "\" and using \"" + roll20API.contentMessage + "\"")
};
December 25 (11 years ago)

Edited December 25 (11 years ago)
Stephen S.
Pro
Marketplace Creator
Sheet Author
API Scripter
That is pretty expandable.. and fairly clear (for me at least<What I mean by that is I understand it... John has another way I don't "get")

The array has a description, long and short version of the command and the process to call... add as many as you like.

See if that gets you closer to what you want to do.
December 25 (11 years ago)
Could not get that to work like I wanted it to but I did find a clunky way of doing it for now I added an or statement to my !attack api looking for a type whisper or type api then I have the sendchat command start with a /w to me and it picks it up. I can figure something better out later but for now it will work.

if(msg.type == "whisper" && msg.content.toLowerCase().indexOf('!attack') !== -1 || msg.type == "api" && msg.content.toLowerCase().indexOf('!attack') !== -1) {