Scripts
JavaScript your Run code steps share, written once and loaded by name.
A script is JavaScript kept with the bot under a name, on the Scripts page. Any Run code step, and any other script, loads it with require("name"). Write a helper once there instead of copying it into every step that needs it.
Writing one
A script is the body of a function. Share what other code needs on module.exports, or return it at the end:
const tiers = ["Bronze", "Silver", "Gold"];
function rank(points) {
return tiers[Math.min(tiers.length - 1, Math.floor(points / 100))];
}
module.exports = { rank };The same script written with return:
const tiers = ["Bronze", "Silver", "Gold"];
return { rank: (points) => tiers[Math.min(tiers.length - 1, Math.floor(points / 100))] };Loading one
const scoring = require("scoring");
return { rank: scoring.rank(Number(ctx.var.points)) };A script can load other scripts the same way.
Names
A script's name is lowercase letters, numbers and underscores, starting with a letter, such as poll_results. Renaming a script on its page changes the steps and scripts that load it too.
Notes
- A script has no
ctx. Pass its functions the values they need. - It runs once each time a step loads it, however many times that step asks for it. Nothing it keeps carries over to the next run.
- It counts towards the step's one second, like the rest of the step's code.
- A step that loads a script that doesn't exist, or a script that loads itself, is shown as a problem before you deploy.
- The builder's AI, and AI apps connected to Buildcord, can write scripts too.
Related
Was this page helpful?